forked from anjuthomas/nodeOIDCMsg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
265 lines (212 loc) · 12.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* Main */
var clockTimestamp = 1000000000;
var BasicIdToken = require('./node_modules/src/models/tokenProfiles/basicIdToken');
//var GoogleIdToken = require('./node_modules/src/models/tokenProfiles/googleIdToken');
//var RefreshToken = require('./node_modules/src/models/tokenProfiles/refreshToken');
var clockTimestamp = 1000000000;
var expect = require('chai').expect;
var atob = require('atob');
var assert = require('chai').assert;
function b64_to_utf8 (str) {
return decodeURIComponent(escape(atob( str )));
}
var conv = require('binstring');
/*
console.dir(conv('hello', { in:'binary' })); // No output encoding specified, defaults to Buffer; output: Buffer([104,101,108,108,111])
console.dir(conv([104,101,108,108,111], { out:'hex' })); // No input encoding specified, auto-detected as Byte Array; output: 68656c6c6f
console.dir(conv('hello', { in:'binary', out:'hex' }));
*/
var clockTimestamp = 1511783267;
var basicIdToken = new BasicIdToken('issuer','subject', clockTimestamp, "jwtid");
basicIdToken.addNonStandardClaims({"foo": 'bar', "aud" : "audience"});
basicIdToken.setNoneAlgorithm(true);
var token = basicIdToken.toJWT("shh", {algorithm: 'HS256' });
var urlEncodedVal = basicIdToken.toUrlEncoded();
var decodedVal = basicIdToken.fromUrlEncoded(urlEncodedVal);
var jsonStr = basicIdToken.toJSON();
var decodedJson = basicIdToken.fromJSON(jsonStr);
//token = "EU3UEJJSGJQWYZZFGIZCKM2BEUZDESCTGI2TMJJSGISTEQZFGIZHI6LQEUZDEJJTIESTEMSKK5KCKMRSEU3UI===.EU3UEJJSGJZXKYRFGIZCKM2BEU2UEJJSGJZXKYTKMVRXIJJSGISTKRBFGJBSKMRSNFZXGJJSGISTGQJFGVBCKMRSNFZXG5LFOISTEMRFGVCCKMSDEUZDEYLVMQSTEMRFGNASKMRSMF2WI2LFNZRWKJJSGISTEQZFGIZGM33PEUZDEJJTIESTEMTCMFZCKMRSEUZEGJJSGJUWC5BFGIZCKM2BFUYTQNBTG44DKNRXGQSTORA=.XNWVIY4JRZPCQHMEPHKGIG7YK63FY262FAQOURN5ABDSKVJ2TSQQ====";
try{
var decoded = basicIdToken.fromJWT(token, "shh", {"foo": "bar","aud" : "audience", "iss" : "issuer", "sub": "subject", 'maxAge': '3s', 'clockTolerance' : 10, "jti": "jwtid"}, {"clockTimestamp" : clockTimestamp, jwtid: 'jwtid' });
}catch(err){
assert.isNotNull(decoded);
assert.isNull(err);
}
/*
var clockTimestamp = 1511783267;
var basicIdToken = new BasicIdToken('issuer','subject', clockTimestamp, "jti");
basicIdToken.addNonStandardClaims({"foo": 'bar', "aud": 'urn:foo'});
basicIdToken.setNoneAlgorithm(true);
var token = basicIdToken.toJWT("shh", {algorithm: 'HS256'});
try{
var decoded = basicIdToken.fromJWT(token, "shh", {"foo": "bar", "aud" : /urn:f[o]{2}/ , "iss" : "issuer", "sub": "subject", 'maxAge': '3s', 'clockTolerance' : 10, "jti": "jti"}, {"clockTimestamp" : clockTimestamp});
}catch(err){
console.log(err);
assert.isUndefined(decoded);
assert.isNotNull(err);
assert.equal(err.name, 'JsonWebTokenError');
}
try{
var decoded = basicIdToken.fromJWT(token, "shh", {"foo": "bar","aud" : 'urn:wrong', "iss" : "issuer", "sub": "subject", 'maxAge': '3s', 'clockTolerance' : 10, "jti": "jti"}, {"clockTimestamp" : clockTimestamp });
}catch(err){
console.log(err);
assert.isUndefined(decoded);
assert.isNotNull(err);
assert.equal(err.name, 'JsonWebTokenError');
} */
/*
var clockTimestamp = 1520000000;
//token = jwt.sign({ foo: 'bar' }, priv, { algorithm: algorithm, notBefore: 0 });
var basicIdToken = new BasicIdToken('issuer','subject', clockTimestamp, "jti");
basicIdToken.addNonStandardClaims({"foo": 'bar', "aud" : "audience"});
basicIdToken.setNoneAlgorithm(true);
var token = basicIdToken.toJWT("shh", {notBefore: 0, algorithm: 'HS256' });
try{
var decoded = basicIdToken.fromJWT(token, "shh", {"foo": "bar", "iss" : "issuer", "sub": "subject", "aud" : "audience", 'maxAge': '3s', 'clockTolerance' : 10, "jti": "jti"}, {"clockTimestamp" : clockTimestamp});
}catch(err){
assert.isNull(err);
assert.isNotNull(decoded);
done();
}
/*
var basicIdToken = new BasicIdToken('issuer','subject', clockTimestamp, "jti");
basicIdToken.addNonStandardClaims({"foo": 'bar', "aud" : "audience"});
basicIdToken.setNoneAlgorithm(true);
var token = basicIdToken.toJWT("shh", { algorithm: "HS256"});
try{
var decoded = basicIdToken.fromJWT(token, "shh", {"foo": "bar", "iss" : "issuer", "sub": "subject", "aud" : "audience", 'maxAge': '3s', 'clockTolerance' : 10, "jti": "jti"}, {"clockTimestamp" : clockTimestamp});
assert.ok(decoded.issuer);
assert.equal('issuer', decoded.issuer);
done();
}catch(err){
console.log(err);
}*/
/*
var clockTimestamp = 1437018582;
//var token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmb28iOiJiYXIifQ.0MBPd4Bru9-fK_HY3xmuDAc6N_embknmNuhdb9bKL_U';
var options = {algorithms: ['HS256'], clockTimestamp: clockTimestamp, maxAge: '1s'};
var key = "secret";
var refreshToken = new RefreshToken('refreshToken','accessToken');
var token = refreshToken.toJWT(key, {noTimestamp : true});
try{
var result = refreshToken.fromJWT(token, key, {"refresh_token" : "refreshToken", "access_token": "accessToken", 'maxAge': '1s'}, options);
console.log(result);
console.log("Result : error messages")
}catch(err){
assert.equal(err.name, 'JsonWebTokenError');
assert.equal(err.message, 'iat required when maxAge is specified');
assert.isUndefined(result);
done();
}*/
/*
var options = {algorithms: ['HS256'], clockTimestamp : 1437018587000};
var key = "secret";
var basicIdToken = new BasicIdToken('issuer','subject',1437018587000 - 5, "jti");
basicIdToken.addNonStandardClaims({ 'foo': 'bar' , "aud" : "audience", "exp" : 1437018587000 + 5});
basicIdToken.setNoneAlgorithm(true);
var token = basicIdToken.toJWT(key);
try{
var result = basicIdToken.fromJWT(token, key, {"iss" : "issuer", "sub": "subject", "aud" : "audience", 'maxAge': '3s', 'clockTolerance' : 0.001, "jti": "jti"}, options);
}catch(err){
assert.equal(err.name, 'TokenExpiredError');
assert.equal(err.message, 'maxAge exceeded');
assert.equal(err.expiredAt.constructor.name, 'Date');
assert.equal(Number(err.expiredAt), 1437018586998000);
assert.isUndefined(result);
}
try{
var secret = "secret";
var basicIdToken = new BasicIdToken('issuer','subject', clockTimestamp, "jti");
basicIdToken.addNonStandardClaims({ 'foo': 'bar' , "aud" : "audience", "nbf" : clockTimestamp + 2, "exp" : 1});
basicIdToken.setNoneAlgorithm(true);
var token = basicIdToken.toJWT(secret, {notBefore: {}});
}catch(err){
expect(err).to.be.ok();
}*/
/*
var clockTimestamp = Math.floor(Date.now() / 1000);
var basicIdToken = new BasicIdToken('issuer','subject', clockTimestamp, "jti");
basicIdToken.addNonStandardClaims({"foo": "123", "aud" : "audience"});
basicIdToken.setNoneAlgorithm(true);
var signedJWT = basicIdToken.toJWT('123', {expiresIn: '5m' , noTimestamp: true });
var result = basicIdToken.fromJWT(signedJWT, '123', {"iss" : "issuer", "sub": "subject", "aud" : "audience", 'maxAge': '1d', 'clockTolerance' : 10, "jti": "jti"}, {'clockTimestamp' : clockTimestamp});
console.log("RESULTTTTTT!!!!!");
console.log(result);
expect(result.exp).to.be.closeTo(Math.floor(Date.now() / 1000) + (5*60), 0.5);*/
/*
var secret = "shhhh";
var basicIdToken = new BasicIdToken('issuer','subject', clockTimestamp, "jti");
basicIdToken.addNonStandardClaims({ "foo": "bar", "aud" : "audience", "nbf" : clockTimestamp + 2, "exp" : 1});
basicIdToken.setNoneAlgorithm(true);
var token = basicIdToken.toJWT(secret, { algorithm: 'HS256' });
var malformedToken = token + ' '; // corrupt the token by adding a space
var decoded = basicIdToken.fromJWT(malformedToken, secret, {"iss" : "issuer", "sub": "subject", "aud" : "audience", 'maxAge': '1d', 'clockTolerance' : 10, "jti": "jti"},{'clockTimestamp' : clockTimestamp, algorithm: 'HS256', ignoreExpiration: true});
console.log("NO ERRRRRRRRRRROR")*/
/*
var verificationClaims = {'iss': 'issuer','sub' : 'subject', 'aud' : 'audience', 'maxAge': '1d', 'jti': 'jti'};
var decodedPayload = basicIdToken.fromJWT(signedJWT, 'shhhh', verificationClaims, {algorithms: ['HS256']});
expect(decodedPayload.exp).to.be.closeTo(iat + two_days_in_secs, 0.2);
var basicIdToken = new BasicIdToken('issuer','subject', clockTimestamp, "jti");
basicIdToken.addNonStandardClaims({ "foo": "bar", "aud" : "audience", "nbf" : clockTimestamp + 2, "exp" : clockTimestamp + 3});
basicIdToken.setNoneAlgorithm(true);
var token = basicIdToken.toJWT(secret, { algorithm: 'HS256' });*/
/*
var expected = 'José';
var basicIdToken = new BasicIdToken('issuer','subject', clockTimestamp, "jti");
basicIdToken.addNonStandardClaims({"name" : expected, "aud" : "audience", "nbf" : clockTimestamp + 2, "exp" : clockTimestamp + 3});
basicIdToken.setNoneAlgorithm(true);
var signedJWT = basicIdToken.toJWT('shhhh', { encoding: 'binary' });
var decoded_name = JSON.parse(atob(signedJWT.split('.')[1])).name;
expect(decoded_name).to.equal(expected);*/
/*
var basicIdToken2 = new BasicIdToken('issuer','subject', clockTimestamp, "jti");
basicIdToken2.addNonStandardClaims({"nbf" : clockTimestamp + 2, "exp" : clockTimestamp + 3});
basicIdToken2.setNoneAlgorithm(true);
var signedJWT = basicIdToken2.toJWT('shhhh');
var decodedPayload = basicIdToken2.fromJWT(signedJWT, 'shhhh', {"iss" : "issuer", "sub": "subject", "aud" : "audience", 'maxAge': '1d', 'clockTolerance' : 10, "jti": "jti"}, {'clockTimestamp' : clockTimestamp});
*/
/*
var fs = require('fs');
var path = require('path');
var pub = fs.readFileSync(path.join(__dirname, 'pub.pem'), 'utf8');
var clockTimestamp = 1000000000;
var refreshToken = new RefreshToken('refreshToken', 'accessToken');
refreshToken.addNonStandardClaims({"foo": "123"});
refreshToken.setNoneAlgorithm(true);
var signedJWT = refreshToken.toJWT('123', {expiresIn: '5m' , noTimestamp: true });
var result = refreshToken.fromJWT(signedJWT, '123', {"refresh_token" : "refreshToken", "access_token": "accessToken"}); */
/*
var basicIdToken = new BasicIdToken('issuer','subject', clockTimestamp, "jti");
basicIdToken.addNonStandardClaims({"foo": "123", "aud" : "audience"});
basicIdToken.setNoneAlgorithm(true);
var signedJWT = basicIdToken.toJWT('123', {expiresIn: '5m' , noTimestamp: true });
var result = basicIdToken.fromJWT(signedJWT, '123', {"iss" : "issuer", "sub": "subject", "aud" : "audience", 'maxAge': '1d', 'clockTolerance' : 10, "jti": "jti"});
*/
/*
var basicIdToken = new BasicIdToken('issuer','subject', clockTimestamp, "jti");
basicIdToken.addNonStandardClaims({"aud" : "audience", "nbf" : clockTimestamp + 2, "exp" : clockTimestamp + 3});
basicIdToken.setNoneAlgorithm(true);
var signedJWT = basicIdToken.toJWT(pub, {algorithm : 'HS256'});
//var decodedPayload = basicIdToken.fromJWT(signedJWT, pub, {"iss" : "issuer", "sub": "subject", "aud" : "audience", 'maxAge': '1d', 'clockTolerance' : 10, "jti": "jti"},{'clockTimestamp' : clockTimestamp});
*/
/*
var basicIdToken2 = new BasicIdToken('issuer','subject', clockTimestamp, "jti");
basicIdToken2.addNonStandardClaims({"aud" : "audience", "nbf" : clockTimestamp + 2, "exp" : clockTimestamp + 3});
basicIdToken2.setNoneAlgorithm(true);
var signedJWT = basicIdToken2.toJWT('shhhh');
var decodedPayload = basicIdToken2.fromJWT(signedJWT, 'shhhh', {"iss" : "issuer", "sub": "subject", "aud" : "audience", 'maxAge': '1d', 'clockTolerance' : 10, "jti": "jti"}, {'clockTimestamp' : clockTimestamp});
var googleIdToken = new GoogleIdToken('name', '[email protected]', '/pathToPic', 'issuer','subject', clockTimestamp);
googleIdToken.addNonStandardClaims({"aud" : "audience", "exp" : clockTimestamp + 3});
googleIdToken.setNoneAlgorithm(true);
var standardClaims = googleIdToken.getStandardClaims();
var nonStandardClaims = googleIdToken.getNonStandardClaims();
var basicIdToken2 = new BasicIdToken('issuer','subject', clockTimestamp, "jti");
basicIdToken2.addNonStandardClaims({"aud" : "audience", "nbf" : clockTimestamp + 2, "exp" : clockTimestamp + 3});
basicIdToken2.setNoneAlgorithm(true);
var nonStandardClaims = basicIdToken2.getNonStandardClaims();
var googleIdToken = new GoogleIdToken('name', '[email protected]', '/pathToPic', 'issuer','subject', clockTimestamp);
googleIdToken.setNoneAlgorithm(true);
var signedJWT = googleIdToken.toJWT('shhhh');
var decodedPayload = googleIdToken.fromJWT(signedJWT, 'shhhh', {"name":"name", "email": "[email protected]", "picture":"/pathToPic", "iss" : "issuer", "sub": "subject", 'maxAge': '1d'}, {'clockTimestamp' : clockTimestamp});
console.log(decodedPayload);
*/