From 4cf2bc8901cf1e49cba33e20561e037f76578b85 Mon Sep 17 00:00:00 2001 From: Myeong-kook Kim Date: Wed, 20 Sep 2017 19:03:56 +0900 Subject: [PATCH 1/4] Add function to decode(JWTDecode) and verify(JWTVerify). and utils(SafeBase64ToBase64) --- jwt.asp | 29 ++++++++++++++++++++++++++++- utils.asp | 11 +++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/jwt.asp b/jwt.asp index 0ce921d..4c3c441 100644 --- a/jwt.asp +++ b/jwt.asp @@ -6,7 +6,12 @@ Function JWTEncode(dPayload, sSecret) Dim sPayload, sHeader, sBase64Payload, sBase64Header Dim sSignature, sToken - sPayload = DictionaryToJSONString(dPayload) + If Typename(dPayload) = "Dictionary" Then + sPayload = DictionaryToJSONString(dPayload) + Else + sPayload = dPayload + End If + sHeader = JWTHeaderDictionary() sBase64Payload = SafeBase64Encode(sPayload) @@ -42,4 +47,26 @@ Function JWTHeaderDictionary() JWTHeaderDictionary = DictionaryToJSONString(dOut) End Function + +' Returns decoded payload (not verify) +Function JWTDecode(token) + Dim tokenSplited, sPayload + tokenSplited = Split(token, ".") + If UBound(tokenSplited) <> 2 Then + JWTDecode = "Invalid token" + Else + sPayload = tokenSplited(1) + sPayload = SafeBase64ToBase64(sPayload) + JWTDecode = Base64Decode(sPayload) + End If +End Function + +' Returns if token is valid +Function JWTVerify(token, sKey) + Dim jsonPayload, reEncodingToken, tokenPayload + tokenPayload = JWTDecode(token) + reEncodingToken = JWTEncode(tokenPayload, sKey) + + JWTVerify = (token = reEncodingToken) +End Function %> diff --git a/utils.asp b/utils.asp index 16438aa..5a3a525 100644 --- a/utils.asp +++ b/utils.asp @@ -24,6 +24,17 @@ Function Base64ToSafeBase64(sIn) Base64ToSafeBase64 = sOut End Function +' change safe base64 to original base64 +Function SafeBase64ToBase64(sIn) + Dim removedEqualityLen + removedEqualityLen = 4 - Len(sIn) mod 4 + If removedEqualityLen = 4 Then removedEqualityLen = 0 + sOut = Replace(sIn,"-","+") + sOut = Replace(sOut,"_","/") + sOut = sOut & Replace(Space(removedEqualityLen)," ","=") + SafeBase64ToBase64 = sOut +End Function + ' Converts an ASP dictionary to a JSON string Function DictionaryToJSONString(dDictionary) Set oJSONpayload = New aspJSON From 45cf54aae4f4d3d2b9292de8b8cfa34f68761b6f Mon Sep 17 00:00:00 2001 From: mika kim Date: Wed, 20 Sep 2017 19:06:11 +0900 Subject: [PATCH 2/4] Update README.md --- README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b8755b8..3abf319 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ A JWT implementation in Classic ASP, currently only supports `JWTEncode(dictiona ```asp <% -Dim sKey, dAttributes, sToken +Dim sKey, dAttributes, sToken, decodedPayload sKey = "Shared Secret" Set dAttributes=Server.CreateObject("Scripting.Dictionary") @@ -19,6 +19,13 @@ dAttributes.Add "name", "Roger" dAttributes.Add "email", "roger@example.com" sToken = JWTEncode(dAttributes, sKey) + +' You can decode JWT token string and get payload. (WARNING : Not verify) +decodedPayload = JWTDecode(sToken) +%> + +' You can verify JWT String. (Returns Boolean) +decodedPayload = JWTVerify(sToken, sKey) %> ``` From f5ec4b8db75166c1cc3aff8a4981c524bd324f45 Mon Sep 17 00:00:00 2001 From: mika kim Date: Wed, 20 Sep 2017 19:06:34 +0900 Subject: [PATCH 3/4] Update README.md --- README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3abf319..cee8426 100644 --- a/README.md +++ b/README.md @@ -20,11 +20,10 @@ dAttributes.Add "email", "roger@example.com" sToken = JWTEncode(dAttributes, sKey) -' You can decode JWT token string and get payload. (WARNING : Not verify) +' Decode JWT token string and get payload. (WARNING : Not verify) decodedPayload = JWTDecode(sToken) -%> -' You can verify JWT String. (Returns Boolean) +' Verify JWT String. (Returns Boolean) decodedPayload = JWTVerify(sToken, sKey) %> ``` From 1ecc16e504ac3dd429941c8894847ac7aa03db50 Mon Sep 17 00:00:00 2001 From: mika kim Date: Wed, 20 Sep 2017 19:06:56 +0900 Subject: [PATCH 4/4] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cee8426..c186658 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ A JWT implementation in Classic ASP, currently only supports `JWTEncode(dictiona ```asp <% -Dim sKey, dAttributes, sToken, decodedPayload +Dim sKey, dAttributes, sToken, decodedPayload, isValidJWT sKey = "Shared Secret" Set dAttributes=Server.CreateObject("Scripting.Dictionary") @@ -24,7 +24,7 @@ sToken = JWTEncode(dAttributes, sKey) decodedPayload = JWTDecode(sToken) ' Verify JWT String. (Returns Boolean) -decodedPayload = JWTVerify(sToken, sKey) +isValidJWT = JWTVerify(sToken, sKey) %> ```