-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
96accd9
commit 5f2da71
Showing
7 changed files
with
97 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 39 additions & 26 deletions
65
packages/pn-personagiuridica-webapp/src/api/auth/Auth.api.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,44 @@ | ||
import { User } from '../../models/User'; | ||
import { TokenExchangeBody, User } from '../../models/User'; | ||
import { authClient } from '../apiClients'; | ||
import { AUTH_TOKEN_EXCHANGE } from './auth.routes'; | ||
|
||
export const AuthApi = { | ||
exchangeToken: (spidToken: string): Promise<User> => | ||
authClient | ||
.post<User>(AUTH_TOKEN_EXCHANGE(), { authorizationToken: spidToken }) | ||
.then((response) => ({ | ||
sessionToken: response.data.sessionToken, | ||
email: response.data.email, | ||
name: response.data.name, | ||
family_name: response.data.family_name, | ||
uid: response.data.uid, | ||
fiscal_number: response.data.fiscal_number, | ||
from_aa: response.data.from_aa, | ||
aud: response.data.aud, | ||
level: response.data.level, | ||
iat: response.data.iat, | ||
exp: response.data.exp, | ||
iss: response.data.iss, | ||
jti: response.data.jti, | ||
organization: response.data.organization, | ||
desired_exp: response.data.desired_exp, | ||
hasGroup: Boolean( | ||
response.data.organization && | ||
response.data.organization.groups && | ||
response.data.organization.groups.length > 0 | ||
), | ||
})), | ||
exchangeToken: async (spidToken: string, aar?: string): Promise<User> => { | ||
const body: TokenExchangeBody = { authorizationToken: spidToken }; | ||
if (aar) { | ||
// eslint-disable-next-line functional/immutable-data | ||
body.source = { | ||
type: 'QR', | ||
id: aar, | ||
}; | ||
} | ||
const response = await authClient.post<User>(AUTH_TOKEN_EXCHANGE(), body); | ||
const user: User = { | ||
sessionToken: response.data.sessionToken, | ||
email: response.data.email, | ||
name: response.data.name, | ||
family_name: response.data.family_name, | ||
uid: response.data.uid, | ||
fiscal_number: response.data.fiscal_number, | ||
from_aa: response.data.from_aa, | ||
aud: response.data.aud, | ||
level: response.data.level, | ||
iat: response.data.iat, | ||
exp: response.data.exp, | ||
iss: response.data.iss, | ||
jti: response.data.jti, | ||
organization: response.data.organization, | ||
desired_exp: response.data.desired_exp, | ||
hasGroup: Boolean( | ||
response.data.organization && | ||
response.data.organization.groups && | ||
response.data.organization.groups.length > 0 | ||
), | ||
}; | ||
if (aar && response.data.source) { | ||
/* eslint-disable-next-line functional/immutable-data */ | ||
user.source = response.data.source; | ||
} | ||
return user; | ||
}, | ||
}; |
25 changes: 21 additions & 4 deletions
25
packages/pn-personagiuridica-webapp/src/api/auth/__test__/Auth.api.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,35 @@ | ||
import MockAdapter from 'axios-mock-adapter'; | ||
|
||
import { userResponse } from '../../../__mocks__/Auth.mock'; | ||
import { userResponse, userResponseWithSource } from '../../../__mocks__/Auth.mock'; | ||
import { authClient } from '../../apiClients'; | ||
import { AuthApi } from '../Auth.api'; | ||
import { AUTH_TOKEN_EXCHANGE } from '../auth.routes'; | ||
|
||
describe('Auth api tests', () => { | ||
let mock: MockAdapter; | ||
|
||
beforeAll(() => { | ||
mock = new MockAdapter(authClient); | ||
}); | ||
|
||
afterEach(() => { | ||
mock.reset(); | ||
}); | ||
|
||
it('exchangeToken', async () => { | ||
const token = 'mocked-token'; | ||
const mock = new MockAdapter(authClient); | ||
mock.onPost(AUTH_TOKEN_EXCHANGE(), { authorizationToken: token }).reply(200, userResponse); | ||
const res = await AuthApi.exchangeToken(token); | ||
expect(res).toStrictEqual(userResponse); | ||
mock.reset(); | ||
mock.restore(); | ||
}); | ||
|
||
it('exchangeToken with aar', async () => { | ||
const token = 'mocked-token'; | ||
const aar = 'mock-aar'; | ||
mock | ||
.onPost(AUTH_TOKEN_EXCHANGE(), { authorizationToken: token, source: { type: 'QR', id: aar } }) | ||
.reply(200, userResponseWithSource); | ||
const res = await AuthApi.exchangeToken(token, aar); | ||
expect(res).toStrictEqual(userResponseWithSource); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters