-
Notifications
You must be signed in to change notification settings - Fork 267
Expand file tree
/
Copy pathresponse.js
More file actions
171 lines (150 loc) · 5.62 KB
/
response.js
File metadata and controls
171 lines (150 loc) · 5.62 KB
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
var t = require('assert')
var response = require('../lib/response')
var sign = (...args) => args.map((arg, index) => index < 2
? Buffer.from(JSON.stringify(arg)).toString('base64')
.replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_')
: arg).join('.')
describe('response', () => {
it('concur', () => {
var provider = {concur: true}
var input = {}
var output =
'<Access_Token>\r\n' +
' <Instance_Url>https://www.concursolutions.com/</Instance_Url>\r\n' +
' <Token>q962LLopjMgTOeTn3fRN+5uABCg=</Token>\r\n' +
' <Expiration_date>9/25/2016 1:36:50 PM</Expiration_date>\r\n' +
' <Refresh_Token>AXvRqWeb77Lq9F2WK6TXLCSTuxpwZO6</Refresh_Token>\r\n' +
'</Access_Token>'
t.deepEqual(response.data({provider, input, output}).output, {
access_token: 'q962LLopjMgTOeTn3fRN+5uABCg=',
refresh_token: 'AXvRqWeb77Lq9F2WK6TXLCSTuxpwZO6',
raw: output
})
})
it('getpocket', () => {
var provider = {getpocket: true}
var input = {}
var output = {access_token: 'token'}
t.deepEqual(response.data({provider, input, output}).output,
{access_token: 'token', raw: {access_token: 'token'}}
)
})
it('yammer', () => {
var provider = {yammer: true}
var input = {}
var output = {access_token: {token: 'token'}}
t.deepEqual(response.data({provider, input, output}).output,
{access_token: 'token', raw: {access_token: {token: 'token'}}}
)
})
it('oauth1', () => {
var provider = {oauth: 1}
var input = {}
var output = {oauth_token: 'token', oauth_token_secret: 'secret'}
t.deepEqual(response.data({provider, input, output}).output, {
access_token: 'token', access_secret: 'secret',
raw: {oauth_token: 'token', oauth_token_secret: 'secret'}
})
})
it('oauth2', () => {
var provider = {oauth: 2}
var input = {session: {}}
var output = {
id_token: sign({typ: 'JWT'}, {hey: 'hi'}, 'signature'),
access_token: 'token', refresh_token: 'refresh'
}
t.deepEqual(response.data({provider, input, output}).output, {
id_token: 'eyJ0eXAiOiJKV1QifQ.eyJoZXkiOiJoaSJ9.signature',
access_token: 'token',
refresh_token: 'refresh',
raw: {
id_token: 'eyJ0eXAiOiJKV1QifQ.eyJoZXkiOiJoaSJ9.signature',
access_token: 'token',
refresh_token: 'refresh'
}
})
})
describe('id_token', () => {
it('invalid format', () => {
var provider = {oauth: 2, response: ['jwt']}
var input = {}
var output = {id_token: sign('a', 'b')}
t.deepEqual(response.data({provider, input, output}).output, {
error: 'Grant: OpenID Connect invalid id_token format'
})
})
it('error decoding', () => {
var provider = {oauth: 2, response: ['jwt']}
var input = {}
var output = {id_token: 'a.b.c'}
t.deepEqual(response.data({provider, input, output}).output, {
error: 'Grant: OpenID Connect error decoding id_token'
})
})
it('invalid audience - string', () => {
var provider = {oauth: 2, key: 'simov', response: ['jwt']}
var input = {}
var output = {id_token: sign({}, {aud: 'grant'}, 'c')}
t.deepEqual(response.data({provider, input, output}).output, {
error: 'Grant: OpenID Connect invalid id_token audience'
})
})
it('invalid audience - array', () => {
var provider = {oauth: 2, key: 'simov', response: ['jwt']}
var input = {}
var output = {id_token: sign({}, {aud: ['grant']}, 'c')}
t.deepEqual(response.data({provider, input, output}).output, {
error: 'Grant: OpenID Connect invalid id_token audience'
})
})
it('nonce mismatch', () => {
var provider = {oauth: 2, key: 'grant', response: ['jwt']}
var input = {session: {nonce: 'bar'}}
var output = {id_token: sign({}, {aud: 'grant', nonce: 'foo'}, 'c')}
t.deepEqual(response.data({provider, input, output}).output, {
error: 'Grant: OpenID Connect nonce mismatch'
})
})
it('valid jwt', () => {
var provider = {oauth: 2, key: 'grant', response: ['tokens', 'jwt']}
var input = {session: {nonce: 'foo'}}
var output = {id_token: sign({typ: 'JWT'}, {aud: 'grant', nonce: 'foo'}, 'signature')}
t.deepEqual(response.data({provider, input, output}).output, {
id_token: 'eyJ0eXAiOiJKV1QifQ.eyJhdWQiOiJncmFudCIsIm5vbmNlIjoiZm9vIn0.signature',
jwt: {
id_token: {
header: {typ: 'JWT'},
payload: {aud: 'grant', nonce: 'foo'},
signature: 'signature'
}
}
})
})
it('valid jwt - audience array', () => {
var provider = {oauth: 2, key: 'grant', response: ['tokens', 'jwt']}
var input = {session: {nonce: 'foo'}}
var output = {id_token: sign({typ: 'JWT'}, {aud: ['grant'], nonce: 'foo'}, 'signature')}
t.deepEqual(response.data({provider, input, output}).output, {
id_token: 'eyJ0eXAiOiJKV1QifQ.eyJhdWQiOlsiZ3JhbnQiXSwibm9uY2UiOiJmb28ifQ.signature',
jwt: {
id_token: {
header: {typ: 'JWT'},
payload: {aud: ['grant'], nonce: 'foo'},
signature: 'signature'
}
}
})
})
})
describe('transport', () => {
it('callback with existing query string', () => {
var provider = {callback: 'https://example.com/callback?foo=bar', transport: 'querystring'}
var input = {params: {override: 'callback'}, state: {}, session: {}}
var output = {access_token: 'token'}
t.equal(
response.transport({provider, input, output}).location,
'https://example.com/callback?foo=bar&access_token=token'
)
})
})
})