@@ -4,6 +4,7 @@ import vibe.data.json;
4
4
5
5
// import fastjwt.stringbuf;
6
6
import stringbuffer;
7
+ import std.base64 : Base64Impl, Base64;
7
8
8
9
version (unittest ) {
9
10
import std.stdio ;
@@ -16,13 +17,12 @@ enum JWTAlgorithm {
16
17
HS512
17
18
}
18
19
20
+ alias URLSafeBase64 = Base64Impl! (' -' , ' _' , Base64.NoPadding);
21
+
19
22
void hash (ref StringBuffer buf, string data, string secret, JWTAlgorithm alg) {
20
23
import std.digest.hmac ;
21
24
import std.digest.sha ;
22
25
import std.string : representation;
23
- import std.base64 : Base64Impl;
24
-
25
- alias URLSafeBase64 = Base64Impl! (' -' , ' _' , Base64.NoPadding);
26
26
27
27
final switch (alg) {
28
28
case JWTAlgorithm.HS256 :
@@ -45,13 +45,11 @@ void hash(ref StringBuffer buf, string data, string secret, JWTAlgorithm alg) {
45
45
}
46
46
}
47
47
48
- import std.base64 ;
49
-
50
48
const base64HeaderStrings = [
51
- Base64 .encode(cast (ubyte [])" {\" alg\" :\" none\" ,\" typ\" :\" JWT\" }" ),
52
- Base64 .encode(cast (ubyte [])" {\" alg\" :\" HS256\" ,\" typ\" :\" JWT\" }" ),
53
- Base64 .encode(cast (ubyte [])" {\" alg\" :\" HS384\" ,\" typ\" :\" JWT\" }" ),
54
- Base64 .encode(cast (ubyte [])" {\" alg\" :\" HS512\" ,\" typ\" :\" JWT\" }" )
49
+ URLSafeBase64 .encode(cast (ubyte [])" {\" alg\" :\" none\" ,\" typ\" :\" JWT\" }" ),
50
+ URLSafeBase64 .encode(cast (ubyte [])" {\" alg\" :\" HS256\" ,\" typ\" :\" JWT\" }" ),
51
+ URLSafeBase64 .encode(cast (ubyte [])" {\" alg\" :\" HS384\" ,\" typ\" :\" JWT\" }" ),
52
+ URLSafeBase64 .encode(cast (ubyte [])" {\" alg\" :\" HS512\" ,\" typ\" :\" JWT\" }" )
55
53
];
56
54
57
55
void headerBase64 (Out)(const JWTAlgorithm alg, ref Out output) {
@@ -72,7 +70,7 @@ void payloadToBase64(Out)(ref Out output, const(Json) payload) {
72
70
Base64.encode(jsonString.getData! (ubyte [])(), output.writer());
73
71
}
74
72
75
- void payloadToBase64 (Out,Args... )(ref Out output, Args args)
73
+ void payloadToBase64 (Out,Args... )(ref Out output, Args args)
76
74
if (args.length > 0 && args.length % 2 == 0 && ! is (args[0 ] == Json))
77
75
{
78
76
import std.format : formattedWrite;
@@ -91,7 +89,7 @@ void payloadToBase64(Out,Args...)(ref Out output, Args args)
91
89
} else static if (is (S == bool )) {
92
90
formattedWrite(loutput, " \" %s\" :%s" , t, s);
93
91
}
94
-
92
+
95
93
static if (args.length > 0 ) {
96
94
impl(loutput, false , args);
97
95
}
@@ -102,12 +100,11 @@ void payloadToBase64(Out,Args...)(ref Out output, Args args)
102
100
w.put(" {" );
103
101
impl(w, true , args);
104
102
w.put(" }" );
105
-
106
- Base64.encode(jsonString.getData! (ubyte [])(), output.writer());
103
+ URLSafeBase64.encode(jsonString.getData! (ubyte [])(), output.writer());
107
104
}
108
105
109
106
unittest {
110
- Json j1 = Json([" field1" : Json(" foo" ), " field2" : Json(42 ),
107
+ Json j1 = Json([" field1" : Json(" foo" ), " field2" : Json(42 ),
111
108
" field3" : Json(true )]
112
109
);
113
110
@@ -189,8 +186,8 @@ Params:
189
186
190
187
Returns: 0 if everything is ok, everything means the token is not ok
191
188
*/
192
- int decodeJWTToken (string encodedToken, string secret,
193
- JWTAlgorithm algo, ref StringBuffer header, ref StringBuffer payload)
189
+ int decodeJWTToken (string encodedToken, string secret,
190
+ JWTAlgorithm algo, ref StringBuffer header, ref StringBuffer payload)
194
191
{
195
192
import std.algorithm.iteration : splitter;
196
193
import std.string : indexOf;
@@ -215,8 +212,8 @@ int decodeJWTToken(string encodedToken, string secret,
215
212
return 3 ;
216
213
}
217
214
218
- Base64 .decode(encodedToken[0 .. dots[0 ]], header.writer());
219
- Base64 .decode(encodedToken[dots[0 ] + 1 .. dots[1 ]], payload.writer());
215
+ URLSafeBase64 .decode(encodedToken[0 .. dots[0 ]], header.writer());
216
+ URLSafeBase64 .decode(encodedToken[dots[0 ] + 1 .. dots[1 ]], payload.writer());
220
217
221
218
return 0 ;
222
219
}
0 commit comments