@wazo/sdk 0.43.1 → 0.43.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/api/auth.ts CHANGED
@@ -27,6 +27,7 @@ export interface AuthD {
27
27
  logIn(params: LoginParams): Promise<Session | null | undefined> ;
28
28
  logOut: (token: Token) => Promise<LogoutResponse>;
29
29
  samlLogIn: (samlSessionId: string) => Promise<Session | null | undefined>;
30
+ initiateIdpAuthentication(domain: string, redirectUrl: string): Promise<any>;
30
31
  refreshToken: (refreshToken: string, backend: string, expiration: number, isMobile?: boolean, tenantId?: string, domainName?: string) => Promise<Session | null | undefined>;
31
32
  deleteRefreshToken: (clientId: string) => Promise<boolean>;
32
33
  updatePassword: (userUuid: UUID, oldPassword: string, newPassword: string) => Promise<boolean>;
@@ -116,6 +117,19 @@ export default ((client: ApiRequester, baseUrl: string): AuthD => ({
116
117
 
117
118
  return client.post(`${baseUrl}/token`, body).then(Session.parse);
118
119
  },
120
+ initiateIdpAuthentication: async (domain: string, redirectUrl: string) => {
121
+ const body = {
122
+ domain,
123
+ redirect_url: redirectUrl,
124
+ };
125
+
126
+ const headers = {
127
+ Accept: 'application/json',
128
+ 'Content-Type': 'application/json',
129
+ };
130
+
131
+ return client.post(`${baseUrl}/saml/sso`, body, headers, (response: any) => response);
132
+ },
119
133
  refreshToken: (refreshToken: string, backend: string, expiration: number, isMobile?: boolean, tenantId?: string, domainName?: string): Promise<Session | null | undefined> => {
120
134
  const body: Record<string, any> = {
121
135
  backend: backend || DEFAULT_BACKEND_USER,
@@ -24,6 +24,8 @@ export class InvalidSubscription extends Error {}
24
24
  export class InvalidAuthorization extends Error {}
25
25
  export class NoTenantIdError extends Error {}
26
26
  export class NoDomainNameError extends Error {}
27
+ export class NoSamlRouteError extends Error {}
28
+ export class SamlConfigError extends Error {}
27
29
 
28
30
  const logger = IssueReporter.loggerFor('simple-auth');
29
31
 
@@ -165,6 +167,31 @@ export class Auth {
165
167
  return this._onAuthenticated(rawSession as Session);
166
168
  }
167
169
 
170
+ async initiateIdpAuthentication(domain: string, redirectUrl: string): Promise<{ location: string, saml_session_id: string } | undefined> {
171
+ let response;
172
+ try {
173
+ response = await getApiClient().auth.initiateIdpAuthentication(domain, redirectUrl);
174
+ } catch (e: any) {
175
+ logger.error('Error during IdP authentication initiation:', e.message);
176
+ return;
177
+ }
178
+
179
+ if (!response.ok) {
180
+ if (response.status === 404) {
181
+ throw new NoSamlRouteError('No route found for saml sso');
182
+ }
183
+
184
+ if (response.status === 500) {
185
+ throw new SamlConfigError('SAML/server configuration error');
186
+ }
187
+
188
+ const error = await response.json();
189
+ throw new Error(error.message || error.reason);
190
+ }
191
+
192
+ return response.json();
193
+ }
194
+
168
195
  async logInViaRefreshToken(refreshToken: string): Promise<Session | null> {
169
196
  const rawSession = await getApiClient().auth.refreshToken(refreshToken, '', this.expiration, this.mobile);
170
197
  return this._onAuthenticated(rawSession as Session);
@@ -120,4 +120,127 @@ describe('simple/Auth', () => {
120
120
  expect(Auth.onSetUsingEdgeServer).toHaveBeenCalledWith(false);
121
121
  });
122
122
  });
123
+
124
+ describe('initiateIdpAuthentication', () => {
125
+ const validResponse = { location: 'idp.com/auth', saml_session_id: 'a1b2C3d4' };
126
+
127
+ it('should return a `location` and `saml_session_id` if the response is ok', async () => {
128
+ const initiateIdpAuthentication = jest.fn(() =>
129
+ Promise.resolve({
130
+ ok: true,
131
+ status: 303,
132
+ json: () => Promise.resolve(validResponse),
133
+ }));
134
+
135
+ (getApiClient as any).mockImplementation(() => ({
136
+ auth: {
137
+ initiateIdpAuthentication,
138
+ },
139
+ }));
140
+
141
+ const response = await Auth.initiateIdpAuthentication('example.com', 'https://myapp.xyz');
142
+ expect(response).toBe(validResponse);
143
+ });
144
+ it('should return a `NoSamlRouteError` if the response is a 404', async () => {
145
+ const initiateIdpAuthentication = jest.fn(() =>
146
+ Promise.resolve({
147
+ ok: false,
148
+ status: 404,
149
+ }));
150
+
151
+ (getApiClient as any).mockImplementation(() => ({
152
+ auth: {
153
+ initiateIdpAuthentication,
154
+ },
155
+ }));
156
+
157
+ await expect(Auth.initiateIdpAuthentication('example.com', 'https://myapp.xyz')).rejects.toThrow(
158
+ 'No route found for saml sso',
159
+ );
160
+ });
161
+
162
+ it('should return an error `message` if the response is NOT ok', async () => {
163
+ const initiateIdpAuthentication = jest.fn(() =>
164
+ Promise.resolve({
165
+ ok: false,
166
+ json: () => Promise.resolve({ message: 'an error message', reason: 'an error reason' }),
167
+ }));
168
+
169
+ (getApiClient as any).mockImplementation(() => ({
170
+ auth: {
171
+ initiateIdpAuthentication,
172
+ },
173
+ }));
174
+
175
+ await expect(Auth.initiateIdpAuthentication('example.com', 'https://myapp.xyz')).rejects.toThrow(
176
+ 'an error message',
177
+ );
178
+ });
179
+
180
+ it('should return an error `reason` if there is no `message` and the response is NOT ok', async () => {
181
+ const initiateIdpAuthentication = jest.fn(() =>
182
+ Promise.resolve({
183
+ ok: false,
184
+ json: () => Promise.resolve({ reason: 'an error reason' }),
185
+ }));
186
+
187
+ (getApiClient as any).mockImplementation(() => ({
188
+ auth: {
189
+ initiateIdpAuthentication,
190
+ },
191
+ }));
192
+
193
+ await expect(Auth.initiateIdpAuthentication('example.com', 'https://myapp.xyz')).rejects.toThrow(
194
+ 'an error reason',
195
+ );
196
+ });
197
+
198
+ it('should return an error with a null message if there is no `message` and `reason`', async () => {
199
+ const initiateIdpAuthentication = jest.fn(() =>
200
+ Promise.resolve({
201
+ ok: false,
202
+ json: () => Promise.resolve({}),
203
+ }));
204
+
205
+ (getApiClient as any).mockImplementation(() => ({
206
+ auth: {
207
+ initiateIdpAuthentication,
208
+ },
209
+ }));
210
+
211
+ await expect(Auth.initiateIdpAuthentication('example.com', 'https://myapp.xyz')).rejects.toThrow('');
212
+ });
213
+
214
+ it('should return a NoSAMLRouteError if the response is a 404', async () => {
215
+ const initiateIdpAuthentication = jest.fn(() =>
216
+ Promise.resolve({
217
+ ok: false,
218
+ status: 404,
219
+ }));
220
+
221
+ (getApiClient as any).mockImplementation(() => ({
222
+ auth: {
223
+ initiateIdpAuthentication,
224
+ },
225
+ }));
226
+
227
+ await expect(Auth.initiateIdpAuthentication('example.com', 'https://myapp.xyz')).rejects.toThrow('No route found for saml sso');
228
+ });
229
+
230
+ it('should return a SamlConfigError if the response is a 500', async () => {
231
+ const initiateIdpAuthentication = jest.fn(() =>
232
+ Promise.resolve({
233
+ ok: false,
234
+ status: 500,
235
+ }));
236
+
237
+ (getApiClient as any).mockImplementation(() => ({
238
+ auth: {
239
+ initiateIdpAuthentication,
240
+ },
241
+ }));
242
+
243
+ await expect(Auth.initiateIdpAuthentication('example.com', 'https://myapp.xyz')).rejects.toThrow('SAML/server configuration error');
244
+ });
245
+ });
123
246
  });
@@ -1,6 +1,6 @@
1
1
  /* eslint-disable import/no-named-as-default */
2
2
  /* global window */
3
- import Auth, { InvalidSubscription, InvalidAuthorization, NoTenantIdError, NoDomainNameError } from './Auth';
3
+ import Auth, { InvalidSubscription, InvalidAuthorization, NoTenantIdError, NoDomainNameError, NoSamlRouteError, SamlConfigError } from './Auth';
4
4
  import Phone from './Phone';
5
5
  import Websocket from './Websocket';
6
6
  import Room from './room/Room';
@@ -138,6 +138,8 @@ const Wazo = {
138
138
  SFUNotAvailableError,
139
139
  NoTenantIdError,
140
140
  NoDomainNameError,
141
+ NoSamlRouteError,
142
+ SamlConfigError,
141
143
  };
142
144
 
143
145
  if (typeof window !== 'undefined') {