-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathauthentication-route_test.js
227 lines (198 loc) · 7.03 KB
/
authentication-route_test.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
import querystring from 'node:querystring';
import { NON_OIDC_IDENTITY_PROVIDERS } from '../../../../src/identity-access-management/domain/constants/identity-providers.js';
import { decodeIfValid, tokenService } from '../../../../src/shared/domain/services/token-service.js';
import { createServer, databaseBuilder, expect, knex } from '../../../test-helper.js';
describe('Acceptance | Controller | authentication-controller', function () {
describe('POST /api/token-from-external-user', function () {
let server;
beforeEach(async function () {
server = await createServer();
});
describe('when user has a reconciled Pix account, then connect to Pix from GAR', function () {
it('should return an 200 with accessToken', async function () {
// given
const password = 'Pix123';
const userAttributes = {
firstName: 'saml',
lastName: 'jackson',
samlId: 'SAMLJACKSONID',
};
const user = databaseBuilder.factory.buildUser.withRawPassword({
username: 'saml.jackson1234',
rawPassword: password,
});
const expectedExternalToken = tokenService.createIdTokenForUserReconciliation(userAttributes);
const options = {
method: 'POST',
url: '/api/token-from-external-user',
headers: {
'x-forwarded-proto': 'https',
'x-forwarded-host': 'app.pix.fr',
},
payload: {
data: {
attributes: {
username: user.username,
password: password,
'external-user-token': expectedExternalToken,
'expected-user-id': user.id,
},
type: 'external-user-authentication-requests',
},
},
};
await databaseBuilder.commit();
// when
const response = await server.inject(options);
// then
expect(response.statusCode).to.equal(200);
expect(response.result.data.attributes['access-token']).to.exist;
const decodedAccessToken = await decodeIfValid(response.result.data.attributes['access-token']);
expect(decodedAccessToken).to.include({
aud: 'https://app.pix.fr',
});
});
it('should add GAR authentication method', async function () {
// given
const password = 'Pix123';
const userAttributes = {
firstName: 'saml',
lastName: 'jackson',
samlId: 'SAMLJACKSONID',
};
const user = databaseBuilder.factory.buildUser.withRawPassword({
username: 'saml.jackson1234',
rawPassword: password,
});
const expectedExternalToken = tokenService.createIdTokenForUserReconciliation(userAttributes);
const options = {
method: 'POST',
url: '/api/token-from-external-user',
headers: {
'x-forwarded-proto': 'https',
'x-forwarded-host': 'app.pix.fr',
},
payload: {
data: {
attributes: {
username: user.username,
password: password,
'external-user-token': expectedExternalToken,
'expected-user-id': user.id,
},
type: 'external-user-authentication-requests',
},
},
};
await databaseBuilder.commit();
// when
await server.inject(options);
// then
const authenticationMethods = await knex('authentication-methods').where({
identityProvider: NON_OIDC_IDENTITY_PROVIDERS.GAR.code,
userId: user.id,
externalIdentifier: 'SAMLJACKSONID',
});
expect(authenticationMethods).to.have.lengthOf(1);
expect(authenticationMethods[0].authenticationComplement).to.deep.equal({
firstName: 'saml',
lastName: 'jackson',
});
});
});
});
describe('POST /api/application/token', function () {
let server;
let options;
const OSMOSE_CLIENT_ID = 'test-apimOsmoseClientId';
const OSMOSE_CLIENT_SECRET = 'test-apimOsmoseClientSecret';
const SCOPE = 'organizations-certifications-result';
beforeEach(async function () {
server = await createServer();
options = {
method: 'POST',
url: '/api/application/token',
headers: {
'content-type': 'application/x-www-form-urlencoded',
'x-forwarded-proto': 'https',
'x-forwarded-host': 'app.pix.fr',
},
};
databaseBuilder.factory.buildClientApplication({
name: 'osmose',
clientId: OSMOSE_CLIENT_ID,
clientSecret: OSMOSE_CLIENT_SECRET,
scopes: [SCOPE],
});
await databaseBuilder.commit();
});
it('should return an 200 with accessToken when clientId, client secret and scope are registred', async function () {
// given
options.payload = querystring.stringify({
grant_type: 'client_credentials',
client_id: OSMOSE_CLIENT_ID,
client_secret: OSMOSE_CLIENT_SECRET,
scope: SCOPE,
});
// when
const response = await server.inject(options);
// then
expect(response.statusCode).to.equal(200);
const result = response.result;
expect(result.token_type).to.equal('bearer');
expect(result.access_token).to.exist;
expect(result.client_id).to.equal(OSMOSE_CLIENT_ID);
});
it('should return an 401 when clientId is not registred', async function () {
// given
options.payload = querystring.stringify({
grant_type: 'client_credentials',
client_id: 'NOT REGISTRED',
client_secret: OSMOSE_CLIENT_SECRET,
scope: SCOPE,
});
// when
const response = await server.inject(options);
// then
expect(response.result.errors[0]).to.deep.equal({
title: 'Unauthorized',
detail: 'The client ID is invalid.',
status: '401',
});
});
it('should return an 401 when client secret is not valid', async function () {
// given
options.payload = querystring.stringify({
grant_type: 'client_credentials',
client_id: OSMOSE_CLIENT_ID,
client_secret: 'invalid secret',
scope: SCOPE,
});
// when
const response = await server.inject(options);
// then
expect(response.result.errors[0]).to.deep.equal({
title: 'Unauthorized',
detail: 'The client secret is invalid.',
status: '401',
});
});
it('should return an 403 when scope is not allowed', async function () {
// given
options.payload = querystring.stringify({
grant_type: 'client_credentials',
client_id: OSMOSE_CLIENT_ID,
client_secret: OSMOSE_CLIENT_SECRET,
scope: 'invalid scope',
});
// when
const response = await server.inject(options);
// then
expect(response.result.errors[0]).to.deep.equal({
title: 'Forbidden',
detail: 'The scope is not allowed.',
status: '403',
});
});
});
});