-
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.
- Loading branch information
1 parent
e76d5e9
commit 1c5f9f4
Showing
14 changed files
with
146 additions
and
58 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
74 changes: 53 additions & 21 deletions
74
packages/pn-personafisica-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,24 +1,56 @@ | ||
import { User } from "../../redux/auth/types"; | ||
import { authClient } from "../apiClients"; | ||
import { AUTH_TOKEN_EXCHANGE } from "./auth.routes"; | ||
/* eslint-disable functional/immutable-data */ | ||
import { AppRouteParams } from '@pagopa-pn/pn-commons'; | ||
|
||
import { User } from '../../redux/auth/types'; | ||
import { authClient } from '../apiClients'; | ||
import { AUTH_TOKEN_EXCHANGE } from './auth.routes'; | ||
|
||
interface TokenExchangeBody { | ||
authorizationToken: string; | ||
retrievalId?: string; | ||
aarQRCodeValue?: string; | ||
} | ||
|
||
export interface TokenExchangeRequest { | ||
spidToken: string; | ||
rapidAccess?: [AppRouteParams, string]; | ||
} | ||
|
||
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, | ||
mobile_phone: response.data.mobile_phone, | ||
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 | ||
})) | ||
exchangeToken: async ({ spidToken, rapidAccess }: TokenExchangeRequest): Promise<User> => { | ||
const body: TokenExchangeBody = { authorizationToken: spidToken }; | ||
if (rapidAccess) { | ||
const [param, value] = rapidAccess; | ||
if (param === AppRouteParams.AAR) { | ||
body.aarQRCodeValue = value; | ||
} | ||
if (param === AppRouteParams.RETRIEVAL_ID) { | ||
body.retrievalId = value; | ||
} | ||
} | ||
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, | ||
mobile_phone: response.data.mobile_phone, | ||
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, | ||
}; | ||
if (response.data.retrievalId) { | ||
user.retrievalId = response.data.retrievalId; | ||
} | ||
if (response.data.tppId) { | ||
user.tppId = response.data.tppId; | ||
} | ||
return user; | ||
}, | ||
}; |
36 changes: 29 additions & 7 deletions
36
packages/pn-personafisica-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,40 @@ | ||
import MockAdapter from 'axios-mock-adapter'; | ||
|
||
import { userResponse } from '../../../__mocks__/Auth.mock'; | ||
import { AppRouteParams } from '@pagopa-pn/pn-commons'; | ||
|
||
import { userResponse, userResponseWithRetrievalId } from '../../../__mocks__/Auth.mock'; | ||
import { authClient } from '../../apiClients'; | ||
import { AuthApi } from '../Auth.api'; | ||
import { AUTH_TOKEN_EXCHANGE } from '../auth.routes'; | ||
|
||
describe('Auth api tests', () => { | ||
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); | ||
|
||
let mock: MockAdapter; | ||
|
||
beforeAll(() => { | ||
mock = new MockAdapter(authClient); | ||
}); | ||
|
||
afterEach(() => { | ||
mock.reset(); | ||
}); | ||
|
||
afterAll(() => { | ||
mock.restore(); | ||
}); | ||
|
||
it('exchangeToken', async () => { | ||
const spidToken = 'mocked-token'; | ||
mock.onPost(AUTH_TOKEN_EXCHANGE(), { authorizationToken: spidToken }).reply(200, userResponse); | ||
const res = await AuthApi.exchangeToken({ spidToken }); | ||
expect(res).toStrictEqual(userResponse); | ||
}); | ||
|
||
it('exchangeToken with rapidAccess', async () => { | ||
const spidToken = 'mocked-token'; | ||
const rapidAccess: [AppRouteParams, string] = [AppRouteParams.AAR, 'mocked-qr-code']; | ||
mock.onPost(AUTH_TOKEN_EXCHANGE(), { authorizationToken: spidToken }).reply(200, userResponseWithRetrievalId); | ||
const res = await AuthApi.exchangeToken({ spidToken, rapidAccess }); | ||
expect(res).toStrictEqual(userResponseWithRetrievalId); | ||
}); | ||
}); |
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
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
11 changes: 10 additions & 1 deletion
11
packages/pn-personafisica-webapp/src/utility/MixpanelUtils/Strategies/TechStrategy.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
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