Skip to content

Commit

Permalink
Merge pull request #193 from sharetribe/auth-with-idp-params
Browse files Browse the repository at this point in the history
Fix auth with IdP params
  • Loading branch information
lyyder authored Feb 15, 2024
2 parents 33c2db4 + 4c5f475 commit fe524f4
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 89 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased] - xxxx-xx-xx

### Fixed

- Prevent unnecessary params from being sent on `loginWithIdp` invocations. [#193](https://github.com/sharetribe/flex-sdk-js/pull/193)

### Changed

- Remove references to Flex in documentation. [190](https://github.com/sharetribe/flex-sdk-js/pull/190)
Expand Down
16 changes: 12 additions & 4 deletions src/fake/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,15 @@ export const multitenantClientData = (config, resolve, reject) => {

export const authWithIdp = (config, resolve, reject, fakeTokenStore) => {
const formData = parseFormData(config.data);
const { idpId, idpClientId, idpToken } = formData;

/* eslint-disable camelcase */
const { idp_id, idp_client_id, idp_token } = formData;
/* eslint-enable camelcase */

let res;

if (formData.client_id === '08ec69f6-d37e-414d-83eb-324e94afddf0') {
res = fakeTokenStore.createTokenWithIdp(idpId, idpClientId, idpToken);
res = fakeTokenStore.createTokenWithIdp(idp_id, idp_client_id, idp_token);
}

if (res) {
Expand All @@ -174,7 +178,11 @@ export const authWithIdp = (config, resolve, reject, fakeTokenStore) => {

export const multitenantAuthWithIdpData = (config, resolve, reject, fakeTokenStore) => {
const formData = parseFormData(config.data);
const { idpId, idpClientId, idpToken } = formData;

/* eslint-disable camelcase */
const { idp_id, idp_client_id, idp_token } = formData;
/* eslint-enable camelcase */

let success;
let error = {
status: 401,
Expand All @@ -185,7 +193,7 @@ export const multitenantAuthWithIdpData = (config, resolve, reject, fakeTokenSto
const hostname = hostnameFromToken(formData.client_secret, 'valid-secret');

if (hostname === 'valid.example.com') {
success = fakeTokenStore.createTokenWithIdp(idpId, idpClientId, idpToken);
success = fakeTokenStore.createTokenWithIdp(idp_id, idp_client_id, idp_token);
} else if (hostname === 'invalid.example.com') {
error = {
...error,
Expand Down
17 changes: 0 additions & 17 deletions src/interceptors/add_idp_client_id_to_params.js

This file was deleted.

17 changes: 0 additions & 17 deletions src/interceptors/add_idp_id_to_params.js

This file was deleted.

17 changes: 0 additions & 17 deletions src/interceptors/add_idp_token_to_params.js

This file was deleted.

30 changes: 30 additions & 0 deletions src/interceptors/rename_idp_params_for_auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
Renames IdP related auth parameters to use snake case.
Changes to `ctx`:
Update following param names, if they exist:
- change `ctx.params.idpId` to `ctx.params.idp_id`
- change `ctx.params.idpClientId` to `ctx.params.idp_client_id`
- change `ctx.params.idpToken` to `ctx.params.idp_token`
*/
export default class RenameIdpParamsForAuth {
enter({ params, ...ctx }) {
const { idpId, idpClientId, idpToken, ...rest } = params;

const idpIdObj = idpId ? { idp_id: idpId } : null;
const idpClientIdObj = idpClientId ? { idp_client_id: idpClientId } : null;
const idpTokenObj = idpToken ? { idp_token: idpToken } : null;

return {
...ctx,
params: {
...idpIdObj,
...idpClientIdObj,
...idpTokenObj,
...rest,
},
};
}
}
8 changes: 2 additions & 6 deletions src/multitenant_sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import FormatHttpResponse from './interceptors/format_http_response';
import FormatMultitenantHttpResponse from './interceptors/format_multitenant_http_response';
import endpointRequest from './interceptors/endpoint_request';
import AddMultitenantAuthHeader from './interceptors/add_multitenant_auth_header';
import AddIdpClientIdToParams from './interceptors/add_idp_client_id_to_params';
import AddIdpIdToParams from './interceptors/add_idp_id_to_params';
import AddIdpTokenToParams from './interceptors/add_idp_token_to_params';
import RenameIdpParamsForAuth from './interceptors/rename_idp_params_for_auth';
import AddMultitenantAuthWithIdpResponse from './interceptors/add_multitenant_auth_with_idp_response';
import createSdkFnContextRunner from './sdk_context_runner';
import memoryStore from './memory_store';
Expand Down Expand Up @@ -115,9 +113,7 @@ const authWithIdpInterceptors = authApiEndpointInterceptors => [
new FormatHttpResponse(),
new AddMultitenantClientSecretTokenToCtx(),
new AddMultitenantClientSecretToParams(),
new AddIdpClientIdToParams(),
new AddIdpIdToParams(),
new AddIdpTokenToParams(),
new RenameIdpParamsForAuth(),
new SaveToken(),
new AddMultitenantAuthWithIdpResponse(),
..._.get(authApiEndpointInterceptors, 'authWithIdp'),
Expand Down
56 changes: 34 additions & 22 deletions src/multitenant_sdk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,17 +315,23 @@ describe('new MultitenantSharetribeSdk', () => {
hostname: 'valid.example.com',
});
return report(
sdk.loginWithIdp().catch(e => {
expect(e).toBeInstanceOf(Error);
expect(e).toEqual(
expect.objectContaining({
status: 401,
statusText: 'Unauthorized',
data: 'Unauthorized',
})
);
expect(sdkTokenStore.getToken()).toBeUndefined();
})
sdk
.loginWithIdp({
idpId: 'facebook',
idpClientId: 'idp-client-id',
idpToken: 'idp-token',
})
.catch(e => {
expect(e).toBeInstanceOf(Error);
expect(e).toEqual(
expect.objectContaining({
status: 401,
statusText: 'Unauthorized',
data: 'Unauthorized',
})
);
expect(sdkTokenStore.getToken()).toBeUndefined();
})
);
});

Expand All @@ -335,17 +341,23 @@ describe('new MultitenantSharetribeSdk', () => {
hostname: 'invalid.example.com',
});
return report(
sdk.loginWithIdp().catch(e => {
expect(e).toBeInstanceOf(Error);
expect(e).toEqual(
expect.objectContaining({
status: 404,
statusText: 'Not Found',
data: 'Not Found',
})
);
expect(sdkTokenStore.getToken()).toBeUndefined();
})
sdk
.loginWithIdp({
idpId: 'facebook',
idpClientId: 'idp-client-id',
idpToken: 'idp-token',
})
.catch(e => {
expect(e).toBeInstanceOf(Error);
expect(e).toEqual(
expect.objectContaining({
status: 404,
statusText: 'Not Found',
data: 'Not Found',
})
);
expect(sdkTokenStore.getToken()).toBeUndefined();
})
);
});
});
Expand Down
8 changes: 2 additions & 6 deletions src/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ import FetchAuthTokenFromApi from './interceptors/fetch_auth_token_from_api';
import FetchAuthTokenFromStore from './interceptors/fetch_auth_token_from_store';
import AddClientIdToParams from './interceptors/add_client_id_to_params';
import AddClientSecretToParams from './interceptors/add_client_secret_to_params';
import AddIdpClientIdToParams from './interceptors/add_idp_client_id_to_params';
import AddIdpIdToParams from './interceptors/add_idp_id_to_params';
import AddIdpTokenToParams from './interceptors/add_idp_token_to_params';
import AddSubjectTokenToParams from './interceptors/add_subject_token_to_params';
import AddGrantTypeToParams from './interceptors/add_grant_type_to_params';
import AddTokenExchangeGrantTypeToParams from './interceptors/add_token_exchange_grant_type_to_params';
Expand All @@ -29,6 +26,7 @@ import MultipartRequest from './interceptors/multipart_request';
import TransitRequest from './interceptors/transit_request';
import TransitResponse from './interceptors/transit_response';
import FormatHttpResponse from './interceptors/format_http_response';
import RenameIdpParamsForAuth from './interceptors/rename_idp_params_for_auth';
import endpointRequest from './interceptors/endpoint_request';
import { createDefaultTokenStore } from './token_store';
import createSdkFnContextRunner from './sdk_context_runner';
Expand Down Expand Up @@ -149,9 +147,7 @@ const exchangeTokenInterceptors = [
const authWithIdpInterceptors = [
new AddClientIdToParams(),
new AddClientSecretToParams(),
new AddIdpClientIdToParams(),
new AddIdpIdToParams(),
new AddIdpTokenToParams(),
new RenameIdpParamsForAuth(),
new SaveToken(),
new AddAuthTokenResponse(),
];
Expand Down

0 comments on commit fe524f4

Please sign in to comment.