From 35e370b29f470d2908a96a61abbd90b3671bcd9f Mon Sep 17 00:00:00 2001 From: Johan Brichau Date: Tue, 23 Jan 2024 10:37:44 +0100 Subject: [PATCH] code style cleanups --- .../JWT.class/instance/signatureOf..st | 2 -- .../instance/verifySignature.forMessage..st | 3 ++- .../instance/testBase64UrlEncodeDecode.st | 12 +++++------- .../instance/testDefaultEncodeDecode.st | 14 ++++++-------- .../instance/testEncodingDecodingNonASCII.st | 11 +++++------ 5 files changed, 18 insertions(+), 24 deletions(-) diff --git a/repository/Json-WebToken.package/JWT.class/instance/signatureOf..st b/repository/Json-WebToken.package/JWT.class/instance/signatureOf..st index 3c19f3a..b50221d 100644 --- a/repository/Json-WebToken.package/JWT.class/instance/signatureOf..st +++ b/repository/Json-WebToken.package/JWT.class/instance/signatureOf..st @@ -3,9 +3,7 @@ signatureOf: aString | algo | algo := headerfields at: 'alg'. - algo = 'none' ifTrue: [ ^ '' ]. - algo = 'HS256' ifTrue: [ ^ (SHA256 new hmac key: key asByteArray; diff --git a/repository/Json-WebToken.package/JWT.class/instance/verifySignature.forMessage..st b/repository/Json-WebToken.package/JWT.class/instance/verifySignature.forMessage..st index d3325a6..63c19fb 100644 --- a/repository/Json-WebToken.package/JWT.class/instance/verifySignature.forMessage..st +++ b/repository/Json-WebToken.package/JWT.class/instance/verifySignature.forMessage..st @@ -1,4 +1,5 @@ private verifySignature: signatureString forMessage: messageString + signatureString asByteArray = (self signatureOf: messageString) asByteArray - ifFalse:[self error: 'Signature verification failed'] \ No newline at end of file + ifFalse:[ self error: 'Signature verification failed' ] \ No newline at end of file diff --git a/repository/Json-WebToken.package/JWTTestCase.class/instance/testBase64UrlEncodeDecode.st b/repository/Json-WebToken.package/JWTTestCase.class/instance/testBase64UrlEncodeDecode.st index ae643d2..f3dd875 100644 --- a/repository/Json-WebToken.package/JWTTestCase.class/instance/testBase64UrlEncodeDecode.st +++ b/repository/Json-WebToken.package/JWTTestCase.class/instance/testBase64UrlEncodeDecode.st @@ -2,11 +2,9 @@ testing jwt encoding testBase64UrlEncodeDecode | testString encodedString | - - testString := 'Testing the base64 encoding'. - encodedString := JWT new base64UrlEncoded: testString. - self assert: (JWT new base64UrlDecoded: encodedString) = testString. - - self assert: (JWT new base64UrlEncoded: (ByteArray withAll: #(16rFB 16rFF 16rFE))) = '-__-'. - self assert: (JWT new base64UrlDecoded: '-__-') asByteArray = (ByteArray withAll: #(16rFB 16rFF 16rFE)). + testString := 'Testing the base64 encoding'. + encodedString := JWT new base64UrlEncoded: testString. + self assert: (JWT new base64UrlDecoded: encodedString) equals: testString. + self assert: (JWT new base64UrlEncoded: (ByteArray withAll: #(16rFB 16rFF 16rFE))) equals: '-__-'. + self assert: (JWT new base64UrlDecoded: '-__-') asByteArray equals: (ByteArray withAll: #(16rFB 16rFF 16rFE)) \ No newline at end of file diff --git a/repository/Json-WebToken.package/JWTTestCase.class/instance/testDefaultEncodeDecode.st b/repository/Json-WebToken.package/JWTTestCase.class/instance/testDefaultEncodeDecode.st index f73c64b..53348ba 100644 --- a/repository/Json-WebToken.package/JWTTestCase.class/instance/testDefaultEncodeDecode.st +++ b/repository/Json-WebToken.package/JWTTestCase.class/instance/testDefaultEncodeDecode.st @@ -1,15 +1,13 @@ testing jwt encoding testDefaultEncodeDecode + | encodedString key payload encodedJWT decodedJWT | key := 'secret'. payload := Dictionary new add: ('some' -> 'payload'); yourself. - - encodedJWT := JWT new - key: key; - payload: payload. + + encodedJWT := JWT new key: key; payload: payload. encodedString := encodedJWT encodedString. - decodedJWT := JWT new key: key; fromEncodedString: encodedString. - - self assert: decodedJWT headerfields = encodedJWT headerfields. - self assert: decodedJWT payload = encodedJWT payload. + + self assert: decodedJWT headerfields equals: encodedJWT headerfields. + self assert: decodedJWT payload equals: encodedJWT payload \ No newline at end of file diff --git a/repository/Json-WebToken.package/JWTTestCase.class/instance/testEncodingDecodingNonASCII.st b/repository/Json-WebToken.package/JWTTestCase.class/instance/testEncodingDecodingNonASCII.st index 371e07e..c84f788 100644 --- a/repository/Json-WebToken.package/JWTTestCase.class/instance/testEncodingDecodingNonASCII.st +++ b/repository/Json-WebToken.package/JWTTestCase.class/instance/testEncodingDecodingNonASCII.st @@ -2,23 +2,22 @@ testing jwt encoding testEncodingDecodingNonASCII | key token1 token2 token3 token4 token5 | - key := 'secret'. "Tests decoding of a known example of a JSON web token with the following payload string (including the spaces) which contains a non-ASCII character: '{ ""name"" : ""Ægir"" }' " token1 := JWT new key: key; fromEncodedString: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyAibmFtZSIgOiAiw4ZnaXIiIH0.YMQaox/praqN23QnH/RYPyPoRvEr/IDshYCjc3XvMWw'. - self assert: token1 payload = (WAJsonParser parse: '{ "name" : "Ægir" }'). + self assert: token1 payload equals: (WAJsonParser parse: '{ "name" : "Ægir" }'). "Tests whether encoding and decoding works for a payload containing a non-ASCII character (note that the #encodedString for these tokens may or may not be the same as that of token1, depending on how the payload object is converted to a string by #asJson):" token2 := JWT new key: key; payload: (Dictionary with: 'name' -> 'Ægir'); yourself. - token3 := JWT new key: key; fromEncodedString: token2 encodedString. - self assert: token3 payload = token2 payload. - + token3 := JWT new key: key; fromEncodedString: token2 encodedString. + self assert: token3 payload equals: token2 payload. + "Tests whether encoding and decoding works for a payload containing non-Basic Multilingual Plane characters using the name 'LOGI' written out using the squared latin capital letters of code points U+1F130 through U+1F149:" token4 := JWT new key: key; payload: (Dictionary with: 'name' -> (String withAll: (#(16r1F13B 16r1F13E 16r1F136 16r1F138) collect: [ :cp | Character codePoint: cp ]))). token5 := JWT new key: key; fromEncodedString: token4 encodedString. - self assert: token4 payload = token5 payload. \ No newline at end of file + self assert: token4 payload equals: token5 payload \ No newline at end of file