diff --git a/src/__tests__/api-client.test.ts b/src/__tests__/api-client.test.ts index 67f72ac0..efea1633 100644 --- a/src/__tests__/api-client.test.ts +++ b/src/__tests__/api-client.test.ts @@ -131,7 +131,6 @@ describe('With correct API results', () => { body: JSON.stringify({ saml_session_id: samlSessionId }), signal: expect.any(Object), headers: { - 'X-Auth-Token': token, 'Content-Type': 'application/json', Accept: 'application/json', }, @@ -140,6 +139,27 @@ describe('With correct API results', () => { }); }); + describe('samlLogIn mobile test', () => { + it('should retrieve user token', async () => { + const samlSessionId = 'a1b2C3d4'; + const result = await client.auth.samlLogIn(samlSessionId, { mobile: true }); + + expect(result).toBeInstanceOf(Session); + expect(result?.token).toBe(1); + expect(global.fetch).toBeCalledWith(`https://${server}/api/auth/${authVersion}/token`, { + method: 'post', + body: JSON.stringify({ saml_session_id: samlSessionId }), + signal: expect.any(Object), + headers: { + 'Content-Type': 'application/json', + 'Wazo-Session-Type': 'mobile', + Accept: 'application/json', + }, + agent: null, + }); + }); + }); + describe('logOut test', () => { it('should delete the specified token', async () => { const oldToken = 123; diff --git a/src/api/auth.ts b/src/api/auth.ts index 1061b894..41075f0f 100644 --- a/src/api/auth.ts +++ b/src/api/auth.ts @@ -32,7 +32,7 @@ export interface AuthD { authenticate: (token: Token) => Promise; logIn(params: LoginParams): Promise ; logOut: (token: Token) => Promise; - samlLogIn: (samlSessionId: string) => Promise; + samlLogIn: (samlSessionId: string, options? : { mobile?: boolean }) => Promise; initiateIdpAuthentication(domain: string, redirectUrl: string): Promise; refreshToken: (refreshToken: string, backend: string, expiration: number, isMobile?: boolean, tenantId?: string, domainName?: string) => Promise; deleteRefreshToken: (clientId: string) => Promise; @@ -116,7 +116,13 @@ export default ((client: ApiRequester, baseUrl: string): AuthD => ({ }, logOut: (token: Token): Promise => client.delete(`${baseUrl}/token/${token}`, null, {}, ApiRequester.successResponseParser), - samlLogIn: async (samlSessionId: string): Promise => { + + samlLogIn: async (samlSessionId: string, options : { mobile?: boolean } = {}): Promise => { + const headers: Record = { + Accept: 'application/json', + 'Content-Type': 'application/json', + }; + const body: SamlLoginBody = { saml_session_id: samlSessionId, }; @@ -126,8 +132,13 @@ export default ((client: ApiRequester, baseUrl: string): AuthD => ({ body.client_id = client.clientId; } - return client.post(`${baseUrl}/token`, body).then(Session.parse); + if (options.mobile) { + headers['Wazo-Session-Type'] = 'mobile'; + } + + return client.post(`${baseUrl}/token`, body, headers).then(Session.parse); }, + initiateIdpAuthentication: async (domain: string, redirectUrl: string) => { const body = { domain,