Skip to content

Commit 24dedbb

Browse files
authored
Merge pull request #8 from ba-st/master
Fix JWS base 64 encoding
2 parents a7d05c1 + 1d1a8c3 commit 24dedbb

File tree

18 files changed

+121
-11
lines changed

18 files changed

+121
-11
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
I am encoder/decoder for base 64 using an alphabet that's URL and filename safe according to https://tools.ietf.org/html/rfc4648#page-5.
2+
3+
Base64 encoding is a technique to encode binary data as a string of characters that can be safely transported over various protocols.
4+
Basically, every 3 bytes are encoded using 4 characters from an alphabet of 64. Each encoded character represents 6 bits.
5+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class initialization
2+
initialize
3+
4+
URLSafeAlphabet := String withAll: ( $A to: $Z ) , ( $a to: $z ) , ( $0 to: $9 ) , #($- $_)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
converting
2+
decode: string
3+
4+
^ encoder decode: string
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
converting
2+
encode: byteArray
3+
4+
^ encoder encode: byteArray
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
initialization
2+
initialize
3+
4+
super initialize.
5+
encoder := ZnBase64Encoder new.
6+
encoder alphabet: URLSafeAlphabet
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"commentStamp" : "GOC 12/4/2019 13:05",
3+
"super" : "Object",
4+
"category" : "JSONWebToken-Core-Serialization",
5+
"classinstvars" : [ ],
6+
"pools" : [ ],
7+
"classvars" : [
8+
"URLSafeAlphabet"
9+
],
10+
"instvars" : [
11+
"encoder"
12+
],
13+
"name" : "Base64UrlEncoder",
14+
"type" : "normal"
15+
}
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
reading
22
materialize: aString key: aKeyString checkSignature: checkSignature
3-
| parts header jws |
4-
parts := $. split: aString.
5-
header := JWSHeader fromJson: (parts at: 1) base64Padded base64Decoded asString.
3+
4+
| parts header jws |
5+
6+
parts := $. split: aString.
7+
header := JWSHeader fromJson: ( self base64Decoded: parts first ) asString.
68
jws := JsonWebSignature new
79
key: aKeyString;
810
setProtectedHeader: header.
9-
checkSignature ifTrue: [
10-
jws checkSignature: parts ].
11-
^ jws payload: (JWTClaimsSet fromJson: parts second base64Padded base64Decoded asString)
11+
checkSignature
12+
ifTrue: [ jws checkSignature: parts ].
13+
^ jws payload: ( JWTClaimsSet fromJson: ( self base64Decoded: parts second ) asString )
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
conversion
2+
base64Decoded: aBase64String
3+
4+
"According to https://tools.ietf.org/html/rfc7519#page-4 the encoding needs to be base64Url.
5+
The padding can be stripped as it is not used for transport in JWT, so we need to pad it just in case"
6+
7+
^ Base64UrlEncoder new decode: aBase64String base64Padded
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
conversion
22
base64Encoded: aByteArray
3-
"base64 encode and strip padding as it is not used for transport
4-
in JWT"
5-
^ aByteArray base64Encoded copyWithoutAll: '='
3+
4+
"According to https://tools.ietf.org/html/rfc7519#page-4 the encoding needs to be base64Url.
5+
The padding is stripped as it is not used for transport in JWT"
6+
7+
^ ( Base64UrlEncoder new encode: aByteArray ) copyWithoutAll: '='
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
private
2+
base64Decoded: aBase64String
3+
4+
^ Base64UrlEncoder new decode: aBase64String base64Padded

0 commit comments

Comments
 (0)