Skip to content

Commit 3dd600e

Browse files
committed
refactor, create base64url.js
1 parent 511f6a0 commit 3dd600e

3 files changed

Lines changed: 39 additions & 31 deletions

File tree

‎docs/base64url.js‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Base64 decode url safe
3+
* https://tools.ietf.org/html/rfc7515#appendix-C
4+
* https://tools.ietf.org/html/rfc4648#section-5
5+
*/
6+
function atobUrlSafe(text) {
7+
if (text == null) {
8+
return null;
9+
}
10+
text = text
11+
.replace(/\s+/g, "") // removes whitespace such as linefeeds from input encoded string
12+
.replace(/-/g, "+") // replace '-' with '+'
13+
.replace(/_/g, "/"); // replace '_' with '/'
14+
switch(text.length % 4) {
15+
case 2: text += "=="; break;
16+
case 3: text += "="; break;
17+
default: break; // if padding is wrong then eventually the atob call below fails
18+
}
19+
return atob(text);
20+
}
21+
22+
/*
23+
* Base64 encode url safe
24+
* https://tools.ietf.org/html/rfc7515#appendix-C
25+
* https://tools.ietf.org/html/rfc4648#section-5
26+
*/
27+
function btoaUrlSafe(text) {
28+
if (text == null) {
29+
return null;
30+
}
31+
text = btoa(text)
32+
.replace(/\+/g, "-") // replace '+' with '-'
33+
.replace(/\//g, "_") // replace '/' with '_'
34+
.replace(/=+$/, ""); // remove trailing padding characters
35+
return text;
36+
}

‎docs/id_token.html‎

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<!DOCTYPE html>
22

3-
<script type="text/javascript" src="//code.jquery.com/jquery-3.3.1.js"></script>
3+
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
4+
<script type="text/javascript" src="base64url.js"></script>
45
<script type="text/javascript">
56

67
var issuer = "https://login.example.ubidemo.com/uas";
@@ -22,21 +23,6 @@
2223
});
2324
}
2425

25-
function atobUrlSafe(text) {
26-
if (text == null) {
27-
return null;
28-
}
29-
text = text
30-
.replace(/\s+/g, "")
31-
.replace(/_/g, "/")
32-
.replace(/-/g, "+");
33-
if (text.length === 0) {
34-
return "";
35-
}
36-
text += "==".substr(0, text.length % 4);
37-
return atob(text);
38-
}
39-
4026
function decodeJWT(jwks, jwt) {
4127

4228
var jws = jwt.split(".");

‎docs/spa.html‎

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<head>
44
<meta name="viewport" content="initial-scale=1"></meta>
55
<script type="text/javascript" src="//code.jquery.com/jquery-3.3.1.js"></script>
6+
<script type="text/javascript" src="base64url.js"></script>
67
<script type="text/javascript">
78

89
var issuer = "https://login.example.ubidemo.com/uas";
@@ -33,21 +34,6 @@
3334
);
3435
}
3536

36-
function atobUrlSafe(text) {
37-
if (text == null) {
38-
return null;
39-
}
40-
text = text
41-
.replace(/\s+/g, "")
42-
.replace(/_/g, "/")
43-
.replace(/-/g, "+");
44-
if (text.length === 0) {
45-
return "";
46-
}
47-
text += "==".substr(0, (4 - text.length % 4) % 4);
48-
return atob(text);
49-
}
50-
5137
function matchParam(qs, name, mapper = null) {
5238
return new Promise(resolve => {
5339
if (qs.startsWith("?")) {

0 commit comments

Comments
 (0)