Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jp okta 476833 #1143

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 2 additions & 70 deletions lib/OktaAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import {
IdxAPI,
SignoutRedirectUrlOptions,
HttpAPI,
FlowIdentifier,
GetWithRedirectAPI,
ParseFromUrlInterface,
GetWithRedirectFunction,
Expand Down Expand Up @@ -84,8 +83,6 @@ import {
verifyToken,
prepareTokenParams,
exchangeCodeForTokens,
isInteractionRequiredError,
isInteractionRequired,
} from './oidc';
import { isBrowser } from './features';
import * as features from './features';
Expand All @@ -106,35 +103,10 @@ import { AuthStateManager } from './AuthStateManager';
import { StorageManager } from './StorageManager';
import TransactionManager from './TransactionManager';
import { buildOptions } from './options';
import {
interact,
introspect,
authenticate,
cancel,
poll,
proceed,
register,
recoverPassword,
unlockAccount,
startTransaction,
handleInteractionCodeRedirect,
canProceed,
handleEmailVerifyCallback,
isEmailVerifyCallback,
parseEmailVerifyCallback,
isEmailVerifyCallbackError
} from './idx';
import { IdxClient } from './idx';
import { createGlobalRequestInterceptor, setGlobalRequestInterceptor } from './idx/headers';
import { OktaUserAgent } from './OktaUserAgent';
import { parseOAuthResponseFromUrl } from './oidc/parseFromUrl';
import {
getSavedTransactionMeta,
createTransactionMeta,
getTransactionMeta,
saveTransactionMeta,
clearTransactionMeta,
isTransactionMetaValid
} from './idx/transactionMeta';
// @ts-ignore
// Do not use this type in code, so it won't be emitted in the declaration output
import Emitter from 'tiny-emitter';
Expand Down Expand Up @@ -294,47 +266,7 @@ class OktaAuth implements OktaAuthInterface, SigninAPI, SignoutAPI {
});

// IDX
const boundStartTransaction = startTransaction.bind(null, this);
this.idx = {
interact: interact.bind(null, this),
introspect: introspect.bind(null, this),
authenticate: authenticate.bind(null, this),
register: register.bind(null, this),
start: boundStartTransaction,
startTransaction: boundStartTransaction, // Use `start` instead. `startTransaction` will be removed in 7.0
poll: poll.bind(null, this),
proceed: proceed.bind(null, this),
cancel: cancel.bind(null, this),
recoverPassword: recoverPassword.bind(null, this),

// oauth redirect callback
handleInteractionCodeRedirect: handleInteractionCodeRedirect.bind(null, this),

// interaction required callback
isInteractionRequired: isInteractionRequired.bind(null, this),
isInteractionRequiredError,

// email verify callback
handleEmailVerifyCallback: handleEmailVerifyCallback.bind(null, this),
isEmailVerifyCallback,
parseEmailVerifyCallback,
isEmailVerifyCallbackError,

getSavedTransactionMeta: getSavedTransactionMeta.bind(null, this),
createTransactionMeta: createTransactionMeta.bind(null, this),
getTransactionMeta: getTransactionMeta.bind(null, this),
saveTransactionMeta: saveTransactionMeta.bind(null, this),
clearTransactionMeta: clearTransactionMeta.bind(null, this),
isTransactionMetaValid,
setFlow: (flow: FlowIdentifier) => {
this.options.flow = flow;
},
getFlow: (): FlowIdentifier | undefined => {
return this.options.flow;
},
canProceed: canProceed.bind(null, this),
unlockAccount: unlockAccount.bind(null, this),
};
this.idx = new IdxClient(this);

setGlobalRequestInterceptor(createGlobalRequestInterceptor(this)); // to pass custom headers to IDX endpoints

Expand Down
197 changes: 185 additions & 12 deletions lib/idx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,194 @@
*/


export { authenticate } from './authenticate';
export { cancel } from './cancel';
export {
import { OktaAuth } from '..';
import { httpRequest } from '../http';
import { authenticate } from './authenticate';
import { cancel } from './cancel';
import {
handleEmailVerifyCallback,
isEmailVerifyCallback,
parseEmailVerifyCallback,
isEmailVerifyCallbackError,
} from './emailVerify';
export { interact } from './interact';
export { introspect } from './introspect';
export { poll } from './poll';
export { proceed, canProceed } from './proceed';
export { register } from './register';
export { recoverPassword } from './recoverPassword';
export { handleInteractionCodeRedirect } from './handleInteractionCodeRedirect';
export { startTransaction } from './startTransaction';
export { unlockAccount } from './unlockAccount';
import { interact, InteractOptions, InteractResponse } from './interact';
import { introspect, IntrospectOptions } from './introspect';
import { poll } from './poll';
import { proceed, canProceed } from './proceed';
import { register } from './register';
import { recoverPassword } from './recoverPassword';
import { handleInteractionCodeRedirect } from './handleInteractionCodeRedirect';
import { startTransaction } from './startTransaction';
import { unlockAccount } from './unlockAccount';
import { isInteractionRequired, isInteractionRequiredError } from '../oidc';
import {
getSavedTransactionMeta,
createTransactionMeta,
getTransactionMeta,
saveTransactionMeta,
clearTransactionMeta,
isTransactionMetaValid
} from './transactionMeta';
import {
AuthenticationOptions,
RegistrationOptions as IdxRegistrationOptions,
PasswordRecoveryOptions,
AccountUnlockOptions,
ProceedOptions,
CancelOptions,
IdxOptions,
IdxTransaction,
IdxTransactionMeta,
EmailVerifyCallbackResponse,
FlowIdentifier,
IdxPollOptions
} from './types';
import { IdxResponse } from './types/idx-js';
import { TransactionMetaOptions } from '../types'

export * from './transactionMeta';
export { poll } from './poll';

export interface IdxClientInterface {
// lowest level api
interact: (options?: InteractOptions) => Promise<InteractResponse>;
introspect: (options?: IntrospectOptions) => Promise<IdxResponse>;

// flow entrypoints
authenticate: (options?: AuthenticationOptions) => Promise<IdxTransaction>;
register: (options?: IdxRegistrationOptions) => Promise<IdxTransaction>;
recoverPassword: (options?: PasswordRecoveryOptions) => Promise<IdxTransaction>;
unlockAccount: (options?: AccountUnlockOptions) => Promise<IdxTransaction>;
poll: (options?: IdxPollOptions) => Promise<IdxTransaction>;

// flow control
start: (options?: IdxOptions) => Promise<IdxTransaction>;
canProceed(options?: { state?: string }): boolean;
proceed: (options?: ProceedOptions) => Promise<IdxTransaction>;
cancel: (options?: CancelOptions) => Promise<IdxTransaction>;
getFlow(): FlowIdentifier | undefined;
setFlow(flow: FlowIdentifier): void;

// call `start` instead of `startTransaction`. `startTransaction` will be removed in next major version (7.0)
startTransaction: (options?: IdxOptions) => Promise<IdxTransaction>;

// redirect callbacks
isInteractionRequired: (hashOrSearch?: string) => boolean;
isInteractionRequiredError: (error: Error) => boolean;
handleInteractionCodeRedirect: (url: string) => Promise<void>;
isEmailVerifyCallback: (search: string) => boolean;
parseEmailVerifyCallback: (search: string) => EmailVerifyCallbackResponse;
handleEmailVerifyCallback: (search: string) => Promise<IdxTransaction | undefined>;
isEmailVerifyCallbackError: (error: Error) => boolean;

// transaction meta
getSavedTransactionMeta: (options?: TransactionMetaOptions) => IdxTransactionMeta | undefined;
createTransactionMeta: (options?: TransactionMetaOptions) => Promise<IdxTransactionMeta>;
getTransactionMeta: (options?: TransactionMetaOptions) => Promise<IdxTransactionMeta>;
saveTransactionMeta: (meta: unknown) => void;
clearTransactionMeta: () => void;
isTransactionMetaValid: (meta: unknown) => boolean;
}

export class IdxClient implements IdxClientInterface {
private sdk: OktaAuth;
private httpClient: Function;

constructor(sdk: OktaAuth) {
this.sdk = sdk;
this.httpClient = httpRequest.bind(this, this.sdk);
}

// lowest level api
interact (options?: InteractOptions) {
return interact(this.sdk, options);
}
introspect (options?: IntrospectOptions) {
return introspect(this.sdk, options);
}

// flow entrypoints
authenticate (options?: AuthenticationOptions) {
return authenticate(this.sdk, options);
}
register (options?: IdxRegistrationOptions) {
return register(this.sdk, options);
}
recoverPassword (options?: PasswordRecoveryOptions) {
return recoverPassword(this.sdk, options);
}
unlockAccount (options?: AccountUnlockOptions) {
return unlockAccount(this.sdk, options);
}
poll (options?: IdxPollOptions) {
return poll(this.sdk, options);
}

// flow control
start (options?: IdxOptions) {
return startTransaction(this.sdk, options);
}
canProceed(options?: { state?: string }) {
return canProceed(this.sdk, options);
}
proceed (options?: ProceedOptions) {
return proceed(this.sdk, options);
}
cancel (options?: CancelOptions) {
return cancel(this.sdk, options);
}
getFlow () {
return this.sdk.options.flow;
}
setFlow (flow: FlowIdentifier) {
this.sdk.options.flow = flow;
}

// call `start` instead of `startTransaction`. `startTransaction` will be removed in next major version (7.0)
startTransaction (options?: IdxOptions) { return this.start(options); }

// redirect callbacks
isInteractionRequired (hashOrSearch?: string) {
return isInteractionRequired(this.sdk, hashOrSearch);
}
isInteractionRequiredError (error: Error) {
return isInteractionRequiredError(error);
}
handleInteractionCodeRedirect (url: string) {
return handleInteractionCodeRedirect(this.sdk, url);
}

// email verify callbacks
isEmailVerifyCallback (search: string) {
return isEmailVerifyCallback(search);
}
parseEmailVerifyCallback (search: string) {
return parseEmailVerifyCallback(search);
}
handleEmailVerifyCallback (search: string) {
return handleEmailVerifyCallback(this.sdk, search);
}
isEmailVerifyCallbackError (error: Error) {
return isEmailVerifyCallbackError(error);
}

// transaction meta
getSavedTransactionMeta (options?: TransactionMetaOptions) {
return getSavedTransactionMeta(this.sdk, options);
}
createTransactionMeta (options?: TransactionMetaOptions) {
return createTransactionMeta(this.sdk, options);
}
getTransactionMeta (options?: TransactionMetaOptions) {
return getTransactionMeta(this.sdk, options);
}
saveTransactionMeta (meta: unknown) {
return saveTransactionMeta(this.sdk, meta);
}
clearTransactionMeta () {
return clearTransactionMeta(this.sdk);
}
isTransactionMetaValid (meta: unknown) {
return isTransactionMetaValid(meta);
}
}
1 change: 1 addition & 0 deletions lib/idx/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export enum AuthenticatorKey {

export type Input = {
name: string;
key?: string;
type?: string;
label?: string;
value?: string | {form: IdxForm};
Expand Down
Loading