@@ -4,7 +4,7 @@ import _ from 'lodash';
4
4
import sinon from 'sinon' ;
5
5
import sinonChai from 'sinon-chai' ;
6
6
7
- import publicKeyDownloadInjector from
7
+ import publicKeyDownloadInjector from
8
8
'inject-loader!../../../src/editor/public-key-download.js' ;
9
9
10
10
chai . use ( chaiAsPromised ) ;
@@ -26,44 +26,54 @@ describe('Public key downloader', function() {
26
26
27
27
const jwks = {
28
28
keys : [ {
29
- kid : 1 ,
30
- x5c : [ 'test-x5c-key' ]
29
+ kty : 'RSA' ,
30
+ kid : '1' ,
31
+ e : 'AQAB' ,
32
+ n : '1GPz-Er5h7PCk4v3pSlnaLYNYrp4sVc6Tx7FVz9d8m4zIS2qzcTM_6dRbMgZ4hBdD35NpYzU4z-d8lN27-J_jOzHnCiMdkY-w52dCofAkICh6ftkFlG9bFQyH8Jz5UtpVkZyy1dxCRz_sbRAzUdjUYsGvrKXg-3UYCL5SBCnt0ycrvr3iKX9k8IlMrFRB8lBJ6eQVzkzGsuivPaThXjVZ_OpY7W-XsDjut7cFgPKIc843tW4CNaDJ6j3afm-RFOok__xLQH5uA7HXS_yqfEchvzXfYfMxJY2d-Eqw4xTurm3TT07RnwJuN9slDJUrTH9EKkJkjZ7dn7fZtGjGTpaDQ' ,
33
+ x5c : [ 'test-x5c-key' ] ,
31
34
} ]
32
35
} ;
33
36
37
+ const keyAsPEM = `-----BEGIN PUBLIC KEY-----\r\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1GPz+Er5h7PCk4v3pSln\r\naLYNYrp4sVc6Tx7FVz9d8m4zIS2qzcTM/6dRbMgZ4hBdD35NpYzU4z+d8lN27+J/\r\njOzHnCiMdkY+w52dCofAkICh6ftkFlG9bFQyH8Jz5UtpVkZyy1dxCRz/sbRAzUdj\r\nUYsGvrKXg+3UYCL5SBCnt0ycrvr3iKX9k8IlMrFRB8lBJ6eQVzkzGsuivPaThXjV\r\nZ/OpY7W+XsDjut7cFgPKIc843tW4CNaDJ6j3afm+RFOok//xLQH5uA7HXS/yqfEc\r\nhvzXfYfMxJY2d+Eqw4xTurm3TT07RnwJuN9slDJUrTH9EKkJkjZ7dn7fZtGjGTpa\r\nDQIDAQAB\r\n-----END PUBLIC KEY-----\r\n` ;
38
+
34
39
function httpGetMock ( data ) {
35
40
return ( url ) => data ? Promise . resolve ( data ) : Promise . reject ( ) ;
36
41
}
37
42
38
43
it ( 'Finds keys in iss + .well-known URL' , function ( done ) {
39
44
const decodedToken = _ . defaultsDeep ( { } , decodedBaseToken , {
40
45
header : {
41
- kid : 1
46
+ kid : '1'
42
47
} ,
43
48
payload : {
44
49
iss : baseUrl
45
50
}
46
51
} ) ;
47
52
48
- const httpGetStub = sinon . stub ( ) . resolves ( JSON . stringify ( jwks ) ) ;
53
+ const httpGetStub = sinon . stub ( )
54
+ . onCall ( 0 ) . resolves ( JSON . stringify ( { jwks_uri : '/.well-known/jwks.json' } ) )
55
+ . onCall ( 1 ) . resolves ( JSON . stringify ( jwks ) ) ;
56
+
49
57
const downloadPublicKeyIfPossible = publicKeyDownloadInjector ( {
50
58
'../utils.js' : {
51
59
httpGet : httpGetStub
52
60
}
53
61
} ) . downloadPublicKeyIfPossible ;
54
62
55
63
downloadPublicKeyIfPossible ( decodedToken )
56
- . should . eventually . include ( jwks . keys [ 0 ] . x5c [ 0 ] )
64
+ . should . eventually . include ( keyAsPEM )
57
65
. then ( ( ) => {
66
+ httpGetStub . should . have . been
67
+ . calledWith ( baseUrl + '.well-known/openid-configuration' ) ;
58
68
httpGetStub . should . have . been
59
69
. calledWith ( baseUrl + '.well-known/jwks.json' ) ;
60
- } ) . should . notify ( done ) ;
70
+ } ) . should . notify ( done ) ;
61
71
} ) ;
62
72
63
73
it ( 'Finds keys in jwk header claim' , function ( done ) {
64
74
const decodedToken = _ . defaultsDeep ( { } , decodedBaseToken , {
65
75
header : {
66
- kid : 1 ,
76
+ kid : '1' ,
67
77
jwk : jwks . keys [ 0 ]
68
78
}
69
79
} ) ;
@@ -79,13 +89,13 @@ describe('Public key downloader', function() {
79
89
. should . eventually . include ( jwks . keys [ 0 ] . x5c [ 0 ] )
80
90
. then ( ( ) => {
81
91
httpGetStub . should . have . callCount ( 0 ) ;
82
- } ) . should . notify ( done ) ;
92
+ } ) . should . notify ( done ) ;
83
93
} ) ;
84
94
85
95
it ( 'Finds keys in jku header claim' , function ( done ) {
86
96
const decodedToken = _ . defaultsDeep ( { } , decodedBaseToken , {
87
97
header : {
88
- kid : 1 ,
98
+ kid : '1' ,
89
99
jku : baseUrl
90
100
}
91
101
} ) ;
@@ -101,7 +111,7 @@ describe('Public key downloader', function() {
101
111
. should . eventually . include ( jwks . keys [ 0 ] . x5c [ 0 ] )
102
112
. then ( ( ) => {
103
113
httpGetStub . should . have . been . calledWith ( baseUrl ) ;
104
- } ) . should . notify ( done ) ;
114
+ } ) . should . notify ( done ) ;
105
115
} ) ;
106
116
107
117
it ( 'Finds keys in x5u header claim' , function ( done ) {
@@ -122,7 +132,7 @@ describe('Public key downloader', function() {
122
132
. should . eventually . include ( jwks . keys [ 0 ] . x5c [ 0 ] )
123
133
. then ( ( ) => {
124
134
httpGetStub . should . have . been . calledWith ( baseUrl ) ;
125
- } ) . should . notify ( done ) ;
135
+ } ) . should . notify ( done ) ;
126
136
} ) ;
127
137
128
138
it ( 'Finds keys in x5c string header claim' , function ( done ) {
@@ -143,7 +153,7 @@ describe('Public key downloader', function() {
143
153
. should . eventually . include ( jwks . keys [ 0 ] . x5c [ 0 ] )
144
154
. then ( ( ) => {
145
155
httpGetStub . should . have . callCount ( 0 ) ;
146
- } ) . should . notify ( done ) ;
156
+ } ) . should . notify ( done ) ;
147
157
} ) ;
148
158
149
159
it ( 'Finds keys in x5c array header claim' , function ( done ) {
@@ -164,13 +174,13 @@ describe('Public key downloader', function() {
164
174
. should . eventually . include ( jwks . keys [ 0 ] . x5c [ 0 ] )
165
175
. then ( ( ) => {
166
176
httpGetStub . should . have . callCount ( 0 ) ;
167
- } ) . should . notify ( done ) ;
177
+ } ) . should . notify ( done ) ;
168
178
} ) ;
169
179
170
180
it ( 'Rejects the promise when HTTP request fails' , function ( done ) {
171
181
const decodedToken = _ . defaultsDeep ( { } , decodedBaseToken , {
172
182
header : {
173
- kid : 1 ,
183
+ kid : '1' ,
174
184
jku : baseUrl
175
185
}
176
186
} ) ;
@@ -186,14 +196,14 @@ describe('Public key downloader', function() {
186
196
. should . be . rejected
187
197
. then ( ( ) => {
188
198
httpGetStub . should . have . been . calledWith ( baseUrl ) ;
189
- } ) . should . notify ( done ) ;
199
+ } ) . should . notify ( done ) ;
190
200
} ) ;
191
201
192
- describe ( 'Rejects the promise when invalid data ' +
202
+ describe ( 'Rejects the promise when invalid data ' +
193
203
'is in jku claim URL' , function ( ) {
194
204
const decodedToken = _ . defaultsDeep ( { } , decodedBaseToken , {
195
205
header : {
196
- kid : 1 ,
206
+ kid : '1' ,
197
207
jku : baseUrl
198
208
}
199
209
} ) ;
@@ -216,9 +226,9 @@ describe('Public key downloader', function() {
216
226
httpGetStub . should . have . been . calledWith ( baseUrl ) ;
217
227
} ) . should . notify ( done ) ;
218
228
} ) ;
219
-
229
+
220
230
it ( 'when the keys object does not exist' , function ( done ) {
221
- httpGetStub = sinon . stub ( ) . resolves ( {
231
+ httpGetStub = sinon . stub ( ) . resolves ( {
222
232
} ) ;
223
233
224
234
downloadPublicKeyIfPossible ( decodedToken )
@@ -227,7 +237,7 @@ describe('Public key downloader', function() {
227
237
httpGetStub . should . have . been . calledWith ( baseUrl ) ;
228
238
} ) . should . notify ( done ) ;
229
239
} ) ;
230
-
240
+
231
241
it ( 'when there is no kid' , function ( done ) {
232
242
httpGetStub = sinon . stub ( ) . resolves ( {
233
243
keys : [ {
@@ -241,11 +251,11 @@ describe('Public key downloader', function() {
241
251
httpGetStub . should . have . been . calledWith ( baseUrl ) ;
242
252
} ) . should . notify ( done ) ;
243
253
} ) ;
244
-
254
+
245
255
it ( 'when there are no x5u or x5c claims' , function ( done ) {
246
256
httpGetStub = sinon . stub ( ) . resolves ( {
247
257
keys : [ {
248
- kid : 1
258
+ kid : '1'
249
259
} ]
250
260
} ) ;
251
261
0 commit comments