Skip to content

Commit

Permalink
code style cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
Johan Brichau committed Jan 23, 2024
1 parent ee445dc commit 35e370b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ signatureOf: aString

| algo |
algo := headerfields at: 'alg'.

algo = 'none' ifTrue: [ ^ '' ].

algo = 'HS256' ifTrue: [
^ (SHA256 new hmac
key: key asByteArray;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
private
verifySignature: signatureString forMessage: messageString

signatureString asByteArray = (self signatureOf: messageString) asByteArray
ifFalse:[self error: 'Signature verification failed']
ifFalse:[ self error: 'Signature verification failed' ]
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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.
self assert: token4 payload equals: token5 payload

0 comments on commit 35e370b

Please sign in to comment.