Skip to content

Commit

Permalink
♻️ Remove deprecated @okta/configuration-validation and integrate ess…
Browse files Browse the repository at this point in the history
…ential functions
  • Loading branch information
vero1024 authored and rajdeepnanua-okta committed Dec 16, 2024
1 parent 8a4868c commit b51ff5d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 24 deletions.
97 changes: 75 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/

import { NativeModules, Platform, NativeEventEmitter } from 'react-native';
import { assertIssuer, assertClientId, assertRedirectUri } from '@okta/configuration-validation';
import { OktaAuth } from '@okta/okta-auth-js';
import Url from 'url-parse';
import { version, peerDependencies } from './package.json';
Expand Down Expand Up @@ -54,12 +53,66 @@ class OktaStatusError extends Error {
}
}

class ConfigurationValidationError extends Error {}
const findDomainURL = 'https://bit.ly/finding-okta-domain';
const findAppCredentialsURL = 'https://bit.ly/finding-okta-app-credentials';
const copyCredentialsMessage = 'You can copy it from the Okta Developer Console ' +
'in the details for the Application you created. ' +
`Follow these instructions to find it: ${findAppCredentialsURL}`;

const isHttps = new RegExp('^https://');
const hasDomainAdmin = /-admin.(okta|oktapreview|okta-emea).com/;

function assertIssuer(issuer, testing = {}){
const copyMessage = 'You can copy your domain from the Okta Developer ' +
'Console. Follow these instructions to find it: ' + findDomainURL;

if (testing.disableHttpsCheck) {
const httpsWarning = 'Warning: HTTPS check is disabled. ' +
'This allows for insecure configurations and is NOT recommended for production use.';
/* eslint-disable-next-line no-console */
console.warn(httpsWarning);
}

if (!issuer) {
throw new ConfigurationValidationError('Your Okta URL is missing. ' + copyMessage);
} else if (!testing.disableHttpsCheck && !issuer.match(isHttps)) {
throw new ConfigurationValidationError(
'Your Okta URL must start with https. ' +
`Current value: ${issuer}. ${copyMessage}`
);
} else if (issuer.match(/{yourOktaDomain}/)) {
throw new ConfigurationValidationError('Replace {yourOktaDomain} with your Okta domain. ' + copyMessage);
} else if (issuer.match(hasDomainAdmin)) {
throw new ConfigurationValidationError(
'Your Okta domain should not contain -admin. ' +
`Current value: ${issuer}. ${copyMessage}`
);
}
}

function assertClientId(clientId){
if (!clientId) {
throw new ConfigurationValidationError('Your client ID is missing. ' + copyCredentialsMessage);
} else if (clientId.match(/{clientId}/)) {
throw new ConfigurationValidationError('Replace {clientId} with the client ID of your Application. ' + copyCredentialsMessage);
}
}

function assertRedirectUri(redirectUri){
if (!redirectUri) {
throw new ConfigurationValidationError('Your redirect URI is missing.');
} else if (redirectUri.match(/{redirectUri}/)) {
throw new ConfigurationValidationError('Replace {redirectUri} with the redirect URI of your Application.');
}
}

/* eslint-disable max-params */
export function createConfigWithCallbacks(
issuer,
clientId,
redirectUri,
endSessionRedirectUri,
redirectUri,
endSessionRedirectUri,
discoveryUri,
scopes,
requireHardwareBackedKeyStore,
Expand All @@ -84,18 +137,18 @@ export function createConfigWithCallbacks(
token: {
storageProvider: storageProvider
}
},
},
issuer: issuer || origin,
clientId,
redirectUri,
scopes
};

authClient = new OktaAuth(oktaAuthConfig);

const reactNativeVersion = peerDependencies['react-native'];
const userAgentTemplate = `okta-react-native/${version} $UPSTREAM_SDK react-native/${reactNativeVersion} ${Platform.OS}/${Platform.Version}`;

if (authClient._oktaUserAgent) {
authClient._oktaUserAgent.addEnvironment(userAgentTemplate.replace('$UPSTREAM_SDK ', ''));
}
Expand Down Expand Up @@ -123,7 +176,7 @@ export function createConfigWithCallbacks(
httpConnectionTimeout,
httpReadTimeout,
};

NativeModules.OktaSdkBridge.createConfig(
clientId,
redirectUri,
Expand All @@ -145,8 +198,8 @@ export function createConfigWithCallbacks(
export const createConfig = async({
issuer,
clientId,
redirectUri,
endSessionRedirectUri,
redirectUri,
endSessionRedirectUri,
discoveryUri,
scopes,
requireHardwareBackedKeyStore,
Expand All @@ -160,8 +213,8 @@ export const createConfig = async({
createConfigWithCallbacks(
issuer,
clientId,
redirectUri,
endSessionRedirectUri,
redirectUri,
endSessionRedirectUri,
discoveryUri,
scopes,
requireHardwareBackedKeyStore,
Expand All @@ -178,12 +231,12 @@ export const createConfig = async({
}
);
});
};
};

export const getAuthClient = () => {
if (!authClient) {
throw new OktaAuthError(
'-100',
'-100',
'OktaOidc client isn\'t configured, check if you have created a configuration with createConfig'
);
}
Expand All @@ -198,10 +251,10 @@ export const signIn = async(options) => {
const { status, sessionToken } = transaction;
if (status !== 'SUCCESS') {
throw new OktaStatusError(
'Transaction status other than "SUCCESS" has been returned. Check transaction.status and handle accordingly.',
'Transaction status other than "SUCCESS" has been returned. Check transaction.status and handle accordingly.',
status
);
}
}

return authenticate({ sessionToken });
})
Expand All @@ -222,8 +275,8 @@ export const signIn = async(options) => {
};

export const signInWithBrowser = async(options = {}) => {
if (typeof options.noSSO === 'boolean') {
options.noSSO = options.noSSO.toString();
if (typeof options.noSSO === 'boolean') {
options.noSSO = options.noSSO.toString();
}

return NativeModules.OktaSdkBridge.signIn(options);
Expand Down Expand Up @@ -282,23 +335,23 @@ export const revokeRefreshToken = async() => {
};

export const introspectAccessToken = async() => {
return NativeModules.OktaSdkBridge.introspectAccessToken();
return NativeModules.OktaSdkBridge.introspectAccessToken();
};

export const introspectIdToken = async() => {
return NativeModules.OktaSdkBridge.introspectIdToken();
return NativeModules.OktaSdkBridge.introspectIdToken();
};

export const introspectRefreshToken = async() => {
return NativeModules.OktaSdkBridge.introspectRefreshToken();
return NativeModules.OktaSdkBridge.introspectRefreshToken();
};

export const refreshTokens = async() => {
return NativeModules.OktaSdkBridge.refreshTokens();
return NativeModules.OktaSdkBridge.refreshTokens();
};

export const clearTokens = async() => {
return NativeModules.OktaSdkBridge.clearTokens();
return NativeModules.OktaSdkBridge.clearTokens();
};

export const EventEmitter = new NativeEventEmitter(NativeModules.OktaSdkBridge);
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@
},
"dependencies": {
"@babel/plugin-transform-async-to-generator": "^7.24.7",
"@okta/configuration-validation": "^1.1.0",
"@okta/okta-auth-js": "7.7.0",
"@okta/okta-auth-js": "7.8.1",
"jscodeshift": "^0.15.2",
"jwt-decode": "^4.0.0",
"url-parse": "^1.5.10"
Expand Down

0 comments on commit b51ff5d

Please sign in to comment.