Skip to content

Commit

Permalink
Add a new native action and state
Browse files Browse the repository at this point in the history
  • Loading branch information
itaihanski committed Sep 30, 2024
1 parent 88cc3c7 commit 3640dc5
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
8 changes: 8 additions & 0 deletions packages/sdks/core-js-sdk/src/sdk/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ type RedirectAuth = {
codeChallenge: string;
};

type NativeOptions = {
platform: string;
nativeOAuthProvider?: string;
};

type AuthMethod =
| 'magiclink'
| 'enchantedlink'
Expand Down Expand Up @@ -276,6 +281,8 @@ export type FlowResponse = {
options: string;
create: boolean;
};
// native data - if the action is 'native'
native?: string;
// an error that occurred during flow execution, used for debugging / integrating
error?: {
code: string;
Expand Down Expand Up @@ -311,6 +318,7 @@ export type Options = {
locale?: string;
oidcPrompt?: string;
oidcErrorRedirectUri?: string;
nativeOptions?: NativeOptions;
};

export type ResponseData = Record<string, any>;
Expand Down
1 change: 1 addition & 0 deletions packages/sdks/web-component/src/lib/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const RESPONSE_ACTIONS = {
poll: 'poll',
webauthnCreate: 'webauthnCreate',
webauthnGet: 'webauthnGet',
native: 'native',
loadForm: 'loadForm',
};

Expand Down
42 changes: 41 additions & 1 deletion packages/sdks/web-component/src/lib/descope-wc/DescopeWc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
} from '../types';
import BaseDescopeWc from './BaseDescopeWc';
import loadSdkScript, { getScriptResultPath } from './sdkScripts';
import { platform } from 'os';

// this class is responsible for WC flow execution
class DescopeWc extends BaseDescopeWc {
Expand Down Expand Up @@ -95,6 +96,16 @@ class DescopeWc extends BaseDescopeWc {
}
}

updateNativeState(
native: string,
originOverride: string,
nativeOAuthProvider: string,
) {
this.flowState.update({ native, originOverride, nativeOAuthProvider });
}

nativeComplete = async (_: any) => {};

async loadSdkScripts() {
const flowConfig = await this.getFlowConfig();
const scripts = flowConfig.sdkScripts;
Expand Down Expand Up @@ -216,6 +227,9 @@ class DescopeWc extends BaseDescopeWc {
samlIdpResponseUrl,
samlIdpResponseSamlResponse,
samlIdpResponseRelayState,
native,
nativeResponse,
nativeOAuthProvider,
...ssoQueryParams
} = currentState;

Expand All @@ -234,6 +248,9 @@ class DescopeWc extends BaseDescopeWc {
backupCallbackUri: redirectAuthBackupCallbackUri,
}
: undefined;
const nativeOptions = native
? { platform: native, nativeOAuthProvider }
: undefined;

// if there is no execution id we should start a new flow
if (!executionId) {
Expand Down Expand Up @@ -276,6 +293,7 @@ class DescopeWc extends BaseDescopeWc {
lastAuth: getLastAuth(loginId),
abTestingKey,
locale: getUserLocale(locale).locale,
nativeOptions,
},
conditionInteractionId,
'',
Expand Down Expand Up @@ -420,6 +438,24 @@ class DescopeWc extends BaseDescopeWc {
this.#handleSdkResponse(sdkResp);
}

if (action === RESPONSE_ACTIONS.native) {
// prepare a callback to receive the response and failure from the Android layer
this.nativeComplete = async (input: any) => {
const sdkResp = await this.sdk.flow.next(
executionId,
stepId,
CUSTOM_INTERACTIONS.submit,
flowConfig.version,
projectConfig.componentsVersion,
input,
);
this.#handleSdkResponse(sdkResp);
};
// notify the Android layer that a native action is requested
this.#dispatch('native', nativeResponse);
return;
}

this.#handlePollingResponse(
executionId,
stepId,
Expand Down Expand Up @@ -502,6 +538,7 @@ class DescopeWc extends BaseDescopeWc {
client: this.client,
...(redirectUrl && { redirectUrl }),
locale: getUserLocale(locale).locale,
nativeOptions,
},
conditionInteractionId,
interactionId,
Expand Down Expand Up @@ -652,6 +689,7 @@ class DescopeWc extends BaseDescopeWc {
webauthn,
error,
samlIdpResponse,
native,
} = sdkResp.data;

if (action === RESPONSE_ACTIONS.poll) {
Expand Down Expand Up @@ -688,6 +726,7 @@ class DescopeWc extends BaseDescopeWc {
samlIdpResponseUrl: samlIdpResponse?.url,
samlIdpResponseSamlResponse: samlIdpResponse?.samlResponse,
samlIdpResponseRelayState: samlIdpResponse?.relayState,
nativeResponse: native,
});
};

Expand Down Expand Up @@ -997,7 +1036,8 @@ class DescopeWc extends BaseDescopeWc {
...eleDescopeAttrs,
...formData,
// 'origin' is required to start webauthn. For now we'll add it to every request
origin: window.location.origin,
origin:
this.flowState.current.originOverride || window.location.origin,
};

const flowConfig = await this.getFlowConfig();
Expand Down
4 changes: 4 additions & 0 deletions packages/sdks/web-component/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ export type FlowState = {
samlIdpResponseUrl: string;
samlIdpResponseSamlResponse: string;
samlIdpResponseRelayState: string;
native: string;
nativeResponse: string;
nativeOAuthProvider: string;
originOverride: string;
} & SSOQueryParams;

export type StepState = {
Expand Down
1 change: 1 addition & 0 deletions packages/sdks/web-js-sdk/src/sdk/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Options = Pick<
| 'locale'
| 'oidcPrompt'
| 'oidcErrorRedirectUri'
| 'nativeOptions'
> & {
lastAuth?: Omit<CoreSdkFlowStartArgs[1]['lastAuth'], 'loginId' | 'name'>;
};
Expand Down

0 comments on commit 3640dc5

Please sign in to comment.