-
Notifications
You must be signed in to change notification settings - Fork 291
/
Copy path_authorization-server-mock.js
199 lines (180 loc) · 5.16 KB
/
_authorization-server-mock.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
'use strict';
const Hoek = require('@hapi/hoek');
const Boom = require('@hapi/boom');
const { HttpResponse } = require('msw');
const msw = require('./msw-matcher/_index');
const accessToken = {
access_token: '5683E74C-7514-4426-B64F-CF0C24223F69',
refresh_token: '8D175C5F-AE24-4333-8795-332B3BDA8FE3',
token_type: 'bearer',
expires_in: '240000',
};
function createAuthorizationServer(authorizationServerUrl) {
function tokenSuccessWithCustomPath(path, scopeOptions, params) {
return msw
.scope(authorizationServerUrl)
.post(path)
.matchBody(params)
.matchHeaders(scopeOptions.reqheaders)
.handler(() => HttpResponse.json(accessToken, {
status: 200,
}));
}
function tokenSuccess(scopeOptions, params) {
return msw
.scope(authorizationServerUrl)
.post('/oauth/token')
.matchBody(params)
.matchHeaders(scopeOptions.reqheaders)
.handler(() => HttpResponse.json(accessToken, {
status: 200,
}));
}
function tokenError(scopeOptions, params) {
return msw
.scope(authorizationServerUrl)
.post('/oauth/token')
.matchBody(params)
.matchHeaders(scopeOptions.reqheaders)
.handler(() => HttpResponse.json(Boom.badImplementation(), {
status: 500,
}));
}
function tokenAuthorizationError(scopeOptions, params) {
return msw
.scope(authorizationServerUrl)
.post('/oauth/token')
.matchBody(params)
.matchHeaders(scopeOptions.reqheaders)
.handler(() => HttpResponse.json(Boom.unauthorized(), {
status: 401,
}));
}
function tokenRevokeSuccess(scopeOptions, params) {
return msw
.scope(authorizationServerUrl)
.post('/oauth/revoke')
.matchBody(params)
.matchHeaders(scopeOptions.reqheaders)
.handler(() => new HttpResponse(null, {
status: 204,
headers: {
'Content-Type': 'application/json',
},
}));
}
function tokenRevokeSuccessWithCustomPath(path, scopeOptions, params) {
return msw
.scope(authorizationServerUrl)
.post(path)
.matchBody(params)
.matchHeaders(scopeOptions.reqheaders)
.handler(() => new HttpResponse(null, {
status: 204,
headers: {
'Content-Type': 'application/json',
},
}));
}
function tokenRevokeError(scopeOptions, params) {
return msw
.scope(authorizationServerUrl)
.post('/oauth/revoke')
.matchBody(params)
.matchHeaders(scopeOptions.reqheaders)
.handler(() => HttpResponse.json(Boom.badImplementation(), {
status: 500,
}));
}
function tokenRevokeAllSuccess(scopeOptions, accessTokenParams, refreshTokenParams) {
return msw
.scope(authorizationServerUrl)
.post('/oauth/revoke')
.matchBody(accessTokenParams)
.matchHeaders(scopeOptions.reqheaders)
.handler(() => new HttpResponse(null, {
status: 204,
headers: {
'Content-Type': 'application/json',
},
}))
.post('/oauth/revoke')
.matchBody(refreshTokenParams)
.matchHeaders(scopeOptions.reqheaders)
.handler(() => new HttpResponse(null, {
status: 204,
headers: {
'Content-Type': 'application/json',
},
}));
}
function tokenSuccessWithNonJSONContent(scopeOptions, params) {
return msw
.scope(authorizationServerUrl)
.post('/oauth/token')
.matchBody(params)
.matchHeaders(scopeOptions.reqheaders)
.handler(() => new HttpResponse('<html>Sorry for not responding with a json response</html>', {
status: 200,
headers: {
'Content-Type': 'text/html',
},
}));
}
function tokenSuccessWithoutRefreshToken(scopeOptions, params) {
return msw
.scope(authorizationServerUrl)
.post('/oauth/token')
.matchBody(params)
.matchHeaders(scopeOptions.reqheaders)
.handler(() => HttpResponse.json({ ...accessToken, refresh_token: undefined }, {
status: 200,
}));
}
return {
tokenError,
tokenAuthorizationError,
tokenRevokeError,
tokenRevokeSuccess,
tokenRevokeAllSuccess,
tokenRevokeSuccessWithCustomPath,
tokenSuccessWithNonJSONContent,
tokenSuccessWithCustomPath,
tokenSuccess,
tokenSuccessWithoutRefreshToken,
};
}
function getAccessToken() {
return accessToken;
}
function getJSONEncodingScopeOptions(options = {}) {
return Hoek.applyToDefaults({
reqheaders: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
}, options);
}
function getFormEncodingScopeOptions(options = {}) {
return Hoek.applyToDefaults({
reqheaders: {
Accept: 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
},
}, options);
}
function getHeaderCredentialsScopeOptions(options = {}) {
return Hoek.applyToDefaults({
reqheaders: {
Accept: 'application/json',
Authorization: 'Basic dGhlK2NsaWVudCtpZDp0aGUrY2xpZW50K3NlY3JldA==',
},
}, options);
}
module.exports = {
getAccessToken,
createAuthorizationServer,
getJSONEncodingScopeOptions,
getFormEncodingScopeOptions,
getHeaderCredentialsScopeOptions,
};