Skip to content

Commit

Permalink
chore(jwt): replace custom base64 handling
Browse files Browse the repository at this point in the history
exist 5.3.0 adds a utility function to encode values in base64 url-safe.
Remove all custom base64 handling code with that.
  • Loading branch information
line-o committed Jun 30, 2021
1 parent 7888215 commit 6e111ed
Showing 1 changed file with 2 additions and 33 deletions.
35 changes: 2 additions & 33 deletions src/content/jwt.xqm
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ declare function jwt:read ($token as xs:string, $secret as xs:string, $lifetime

declare function jwt:sign ($data as xs:string, $secret as xs:string) as xs:string {
crypto:hmac($data, $secret, "HMAC-SHA-256", "base64")
=> jwt:base64-url-safe()
=> util:base64-encode-url-safe()
};

(:~
Expand All @@ -112,43 +112,12 @@ declare
function jwt:encode ($data as item()) as xs:string {
$data
=> serialize(map { "method": "json" })
=> util:base64-encode(true())
=> jwt:base64-url-safe()
=> util:base64-encode-url-safe()
};

declare
function jwt:decode ($base64 as xs:string) as item()? {
$base64
(: base64-decode might to be able to handle url-safe encoded data :)
=> translate('-_', '/+')
=> jwt:base64-pad()
=> util:base64-decode()
=> parse-json()
};

(:~
: add padding (= or ==) otherwise util:base64-decode() throws an error
:)
declare %private
function jwt:base64-pad ($data as xs:string) as xs:string {
let $mod4 := string-length($data) mod 4
let $pad :=
switch ($mod4)
case 2 return "=="
case 3 return "="
default return ""

return
$data || $pad
};

(:~
: convert base64 string to url-safe without padding
: replace / and + with - and _
: omit padding (=)
: @see https://tools.ietf.org/html/rfc4648
:)
declare %private
function jwt:base64-url-safe ($base64 as xs:string) as xs:string {
$base64 => translate('+/=', '-_')
};

0 comments on commit 6e111ed

Please sign in to comment.