Skip to content

Commit

Permalink
Allow to pass mobile header in SAML login route
Browse files Browse the repository at this point in the history
  • Loading branch information
manuquentin committed Jul 18, 2024
1 parent 38685d4 commit b718084
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
22 changes: 21 additions & 1 deletion src/__tests__/api-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
Expand All @@ -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;
Expand Down
17 changes: 14 additions & 3 deletions src/api/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface AuthD {
authenticate: (token: Token) => Promise<Session | null | undefined>;
logIn(params: LoginParams): Promise<Session | null | undefined> ;
logOut: (token: Token) => Promise<LogoutResponse>;
samlLogIn: (samlSessionId: string) => Promise<Session | null | undefined>;
samlLogIn: (samlSessionId: string, options? : { mobile?: boolean }) => Promise<Session | null | undefined>;
initiateIdpAuthentication(domain: string, redirectUrl: string): Promise<any>;
refreshToken: (refreshToken: string, backend: string, expiration: number, isMobile?: boolean, tenantId?: string, domainName?: string) => Promise<Session | null | undefined>;
deleteRefreshToken: (clientId: string) => Promise<boolean>;
Expand Down Expand Up @@ -116,7 +116,13 @@ export default ((client: ApiRequester, baseUrl: string): AuthD => ({
},

logOut: (token: Token): Promise<LogoutResponse> => client.delete(`${baseUrl}/token/${token}`, null, {}, ApiRequester.successResponseParser),
samlLogIn: async (samlSessionId: string): Promise<Session | null | undefined> => {

samlLogIn: async (samlSessionId: string, options : { mobile?: boolean } = {}): Promise<Session | null | undefined> => {
const headers: Record<string, any> = {
Accept: 'application/json',
'Content-Type': 'application/json',
};

const body: SamlLoginBody = {
saml_session_id: samlSessionId,
};
Expand All @@ -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,
Expand Down

0 comments on commit b718084

Please sign in to comment.