forked from okta/okta-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecurity.tsx
108 lines (94 loc) · 3.77 KB
/
Security.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*!
* Copyright (c) 2017-Present, Okta, Inc. and/or its affiliates. All rights reserved.
* The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
*
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and limitations under the License.
*/
import * as React from 'react';
import { AuthSdkError, AuthState, OktaAuth } from '@okta/okta-auth-js';
import OktaContext, { OnAuthRequiredFunction, RestoreOriginalUriFunction } from './OktaContext';
import OktaError from './OktaError';
const Security: React.FC<{
oktaAuth: OktaAuth,
restoreOriginalUri: RestoreOriginalUriFunction,
onAuthRequired?: OnAuthRequiredFunction,
children?: React.ReactNode
} & React.HTMLAttributes<HTMLDivElement>> = ({
oktaAuth,
restoreOriginalUri,
onAuthRequired,
children
}) => {
const [authState, setAuthState] = React.useState(() => {
if (!oktaAuth) {
return null;
}
return oktaAuth.authStateManager.getAuthState();
});
const [oktaAuthMajorVersion] = React.useState(() => {
if (!oktaAuth || !oktaAuth._oktaUserAgent) {
return null;
}
const oktaAuthVersion = oktaAuth._oktaUserAgent.getVersion();
const majorVersion = oktaAuthVersion?.split('.')[0];
return majorVersion;
});
React.useEffect(() => {
if (!oktaAuth || !restoreOriginalUri) {
return;
}
// Add default restoreOriginalUri callback
if (oktaAuth.options.restoreOriginalUri && restoreOriginalUri) {
console.warn('Two custom restoreOriginalUri callbacks are detected. The one from the OktaAuth configuration will be overridden by the provided restoreOriginalUri prop from the Security component.');
}
oktaAuth.options.restoreOriginalUri = async (oktaAuth: unknown, originalUri: string) => {
restoreOriginalUri(oktaAuth as OktaAuth, originalUri);
};
// Add okta-react userAgent
if (oktaAuth._oktaUserAgent) {
oktaAuth._oktaUserAgent.addEnvironment(`${process.env.PACKAGE_NAME}/${process.env.PACKAGE_VERSION}`);
} else {
console.warn('_oktaUserAgent is not available on auth SDK instance. Please use okta-auth-js@^5.3.1 .');
}
// Update Security provider with latest authState
const handler = (authState: AuthState) => {
setAuthState(authState);
};
oktaAuth.authStateManager.subscribe(handler);
// Trigger an initial change event to make sure authState is latest
oktaAuth.start();
return () => {
oktaAuth.authStateManager.unsubscribe(handler);
oktaAuth.stop();
};
}, [oktaAuth, restoreOriginalUri]);
if (!oktaAuth) {
const err = new AuthSdkError('No oktaAuth instance passed to Security Component.');
return <OktaError error={err} />;
}
if (!restoreOriginalUri) {
const err = new AuthSdkError('No restoreOriginalUri callback passed to Security Component.');
return <OktaError error={err} />;
}
if (oktaAuthMajorVersion !== process.env.AUTH_JS_MAJOR_VERSION
// use SKIP_VERSION_CHECK flag to control version check in tests
&& process.env.SKIP_VERSION_CHECK !== '1') {
const err = new AuthSdkError(`Passed in oktaAuth is not compatible with the SDK, okta-auth-js version ${process.env.AUTH_JS_MAJOR_VERSION}.x is the current supported version.`);
return <OktaError error={err} />;
}
return (
<OktaContext.Provider value={{
oktaAuth,
authState,
_onAuthRequired: onAuthRequired
}}>
{children}
</OktaContext.Provider>
);
};
export default Security;