From 87e03631f591cd13fbf0585dafca23c0d87e7687 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Fri, 9 Apr 2021 09:23:28 -0500 Subject: [PATCH] base64: Add a version of url-base64 that trims trailing === for jwt. See https://tools.ietf.org/html/rfc7519 and https://stackoverflow.com/questions/49082844/how-could-firebase-send-a-jwt-token-which-payload-contains-an-underscore-charact --- basis/base64/base64-tests.factor | 6 ++++++ basis/base64/base64.factor | 3 +++ 2 files changed, 9 insertions(+) diff --git a/basis/base64/base64-tests.factor b/basis/base64/base64-tests.factor index 8821f9654c..3593fe2c7d 100644 --- a/basis/base64/base64-tests.factor +++ b/basis/base64/base64-tests.factor @@ -47,3 +47,9 @@ kernel sequences splitting strings tools.test ; { "01a-b_cd" } [ "\xd3V\xbeo\xf7\x1d" >urlsafe-base64 "" like ] unit-test { "\xd3V\xbeo\xf7\x1d" } [ "01a-b_cd" urlsafe-base64> "" like ] unit-test + +{ "eyJhIjoiYmNkIn0" } +[ "{\"a\":\"bcd\"}" >urlsafe-base64-jwt >string ] unit-test + +{ "{\"a\":\"bcd\"}" } +[ "{\"a\":\"bcd\"}" >urlsafe-base64-jwt urlsafe-base64> >string ] unit-test \ No newline at end of file diff --git a/basis/base64/base64.factor b/basis/base64/base64.factor index e9b9b62d13..a85768aafe 100644 --- a/basis/base64/base64.factor +++ b/basis/base64/base64.factor @@ -146,6 +146,9 @@ PRIVATE> { CHAR: / CHAR: _ } } substitute ; +: >urlsafe-base64-jwt ( seq -- base64 ) + >urlsafe-base64 [ CHAR: = = ] trim-tail ; + : urlsafe-base64> ( base64 -- seq ) H{ { CHAR: - CHAR: + } -- 2.34.1