Newer
Older
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { useCallback, useRef, useState } from "react"
import { OAuthContext } from "./OAuthContext"
import { IOAuthState, initialOAuthState } from "./OAuthState"
import { OAUTH_RESPONSE_MESSAGE_TYPE, OAUTH_STATE_STORAGE_KEY } from "../../commons/costants"
import { tokenPostRequest } from "./OAuthTokenRequest"
import { OidcToken, OidcClientSettings } from "./OidcConfig"
import { User } from "./User"
interface OAuthProviderProps extends OidcClientSettings {
children?: React.ReactNode;
}
type Timer = ReturnType<typeof setTimeout>;
const getAuthorizationUrl = (props: OidcClientSettings) => {
let url = `${props.authority}` +
`/authorize?` +
`&redirect_uri=${props.redirect_uri}` +
`&client_id=${props.client_id}`
url += props.client_secret ? `&client_secret=${props.client_secret}` : "";
url += props.response_type ? `&response_type=${props.response_type}` : "";
url += props.scope ? `&scope=${props.scope}` : "";
url += props.state ? `&state=${props.state}` : "";
return url;
}
const generateState = () => {
const validChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let array = new Uint8Array(40);
window.crypto.getRandomValues(array);
array = array.map((x) => validChars.codePointAt(x % validChars.length) || 0);
const randomState = String.fromCharCode.apply(null, Array.from(array));
return randomState;
};
const saveState = (state: string) => {
sessionStorage.setItem(OAUTH_STATE_STORAGE_KEY, state);
}
const openWindow = (url: string) => {
return window.open(url);
}
const closePopup = (popupRef: React.MutableRefObject<Window | undefined>) => {
popupRef.current?.close();
}
const cleanup = (
intervalRef: React.MutableRefObject<Timer | undefined>,
popupRef: React.MutableRefObject<Window | undefined>,
handleMessageListener: (message: any) => Promise<void>) => {
clearInterval(intervalRef.current);
closePopup(popupRef);
window.removeEventListener('message', handleMessageListener);
}
const parseJwt = (token: string) => {
var base64Payload = token.split('.')[1];
var payload = window.atob(base64Payload);
return JSON.parse(payload);
}
export const OAuthProvider = (props: OAuthProviderProps): JSX.Element => {
const { children } = props;
const [oAuthState, setOAuthState] = useState<IOAuthState>(initialOAuthState);
const popupRef = useRef<Window>();
const intervalRef = useRef<Timer>();
const signinPopup = useCallback(() => {
// 1. Init
setOAuthState(initialOAuthState);
// 2. Generate and save state
const state = generateState();
saveState(state);
// 3. Open window
const url = getAuthorizationUrl({ ...props, state });
popupRef.current = openWindow(url) || undefined;
// 4. Register message listener
async function handleMessageListener(message: any) {
if (message?.data?.type !== OAUTH_RESPONSE_MESSAGE_TYPE) {
return;
}
try {
const errorMaybe = message && message.data && message.data.error;
if (errorMaybe) {
setOAuthState({
isLoading: false,
isAuthenticated: false,
error: errorMaybe && new Error(errorMaybe)
});
console.log(errorMaybe);
} else {
const { code } = message && message.data && message.data.payload;
// Sent POST request to backend
tokenPostRequest({
client_id: props.client_id,
redirect_uri: props.redirect_uri,
code: code,
grant_type: props.grant_type,
code_verifier: undefined
})
.then(response => response.json())
.then(data => {
const token = OidcToken.createTokenFromResponse(data);
const user = new User({
session_state: null,
profile: parseJwt(token.access_token),
token: token
});
// The user is now logged in
console.log(parseJwt(token.access_token));
setOAuthState({
isLoading: false,
isAuthenticated: true,
user: user
});
})
.catch((err) => {
console.error(err);
setOAuthState({
isLoading: false,
isAuthenticated: false,
error: err instanceof Error ? err : new Error("Uknown error")
});
});
}
} catch (err) {
setOAuthState({
isLoading: false,
isAuthenticated: false,
error: err instanceof Error ? err : new Error("Uknown error")
});
console.log(err);
} finally {
cleanup(intervalRef, popupRef, handleMessageListener);
}
}
window.addEventListener('message', handleMessageListener);
// 5. Begin interval to check if popup was closed forcelly by the user
intervalRef.current = setInterval(() => {
const popupClosed = !popupRef.current
|| !popupRef.current.window
|| popupRef.current.window.closed;
if (popupClosed) {
setOAuthState({
isAuthenticated: false,
isLoading: false,
});
console.warn("Warning: Popup was closed before completing authentication.");
cleanup(intervalRef, popupRef, handleMessageListener);
}
}, 250);
// 6. Remove listener(s) on unmount
return () => {
window.removeEventListener('message', handleMessageListener);
if (intervalRef.current) clearInterval(intervalRef.current);
};
}, [props]);
const logout = useCallback(() => {
setOAuthState(initialOAuthState);
}, []);
return (
<OAuthContext.Provider
value={{
signinPopup: signinPopup,