|
1 |
| -import lodash from 'lodash'; |
2 |
| - |
3 | 1 | import { config } from '../../../src/shared/config.js';
|
4 | 2 | import {
|
5 | 3 | ApplicationScopeNotAllowedError,
|
6 | 4 | ApplicationWithInvalidClientIdError,
|
7 | 5 | ApplicationWithInvalidClientSecretError,
|
8 | 6 | } from '../../../src/shared/domain/errors.js';
|
9 |
| -const { apimRegisterApplicationsCredentials, jwtConfig } = config; |
10 |
| - |
11 |
| -const { find } = lodash; |
12 | 7 |
|
13 |
| -const authenticateApplication = async function ({ clientId, clientSecret, scope, tokenService }) { |
14 |
| - const application = find(apimRegisterApplicationsCredentials, { clientId }); |
15 |
| - _checkClientId(application, clientId); |
16 |
| - _checkClientSecret(application, clientSecret); |
| 8 | +const { authentication } = config; |
| 9 | + |
| 10 | +export async function authenticateApplication({ |
| 11 | + clientId, |
| 12 | + clientSecret, |
| 13 | + scope, |
| 14 | + tokenService, |
| 15 | + clientApplicationRepository, |
| 16 | + cryptoService, |
| 17 | +}) { |
| 18 | + const application = await clientApplicationRepository.findByClientId(clientId); |
| 19 | + _checkApplication(application, clientId); |
| 20 | + await _checkClientSecret(application, clientSecret, cryptoService); |
17 | 21 | _checkAppScope(application, scope);
|
18 | 22 |
|
19 | 23 | return tokenService.createAccessTokenFromApplication(
|
20 | 24 | clientId,
|
21 |
| - application.source, |
| 25 | + application.name, |
22 | 26 | scope,
|
23 |
| - jwtConfig[application.source].secret, |
24 |
| - jwtConfig[application.source].tokenLifespan, |
| 27 | + authentication.secret, |
| 28 | + authentication.accessTokenLifespanMs, |
25 | 29 | );
|
26 |
| -}; |
| 30 | +} |
27 | 31 |
|
28 |
| -function _checkClientId(application, clientId) { |
29 |
| - if (!application || application.clientId !== clientId) { |
| 32 | +function _checkApplication(application) { |
| 33 | + if (!application) { |
30 | 34 | throw new ApplicationWithInvalidClientIdError('The client ID is invalid.');
|
31 | 35 | }
|
32 | 36 | }
|
33 | 37 |
|
34 |
| -function _checkClientSecret(application, clientSecret) { |
35 |
| - if (application.clientSecret !== clientSecret) { |
| 38 | +async function _checkClientSecret(application, clientSecret, cryptoService) { |
| 39 | + try { |
| 40 | + await cryptoService.checkPassword({ password: clientSecret, passwordHash: application.clientSecret }); |
| 41 | + } catch { |
36 | 42 | throw new ApplicationWithInvalidClientSecretError('The client secret is invalid.');
|
37 | 43 | }
|
38 | 44 | }
|
39 | 45 |
|
40 | 46 | function _checkAppScope(application, scope) {
|
41 |
| - if (application.scope !== scope) { |
| 47 | + if (!application.scopes.includes(scope)) { |
42 | 48 | throw new ApplicationScopeNotAllowedError('The scope is invalid.');
|
43 | 49 | }
|
44 | 50 | }
|
45 |
| - |
46 |
| -export { authenticateApplication }; |
|
0 commit comments