Skip to content

Commit 00304cb

Browse files
committed
Add setupOAuth2Credentials
1 parent c1ed8db commit 00304cb

File tree

3 files changed

+100
-15
lines changed

3 files changed

+100
-15
lines changed

package-lock.json

+78-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/App.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ function reset() {
2727
function App() {
2828

2929
const [authorizing, setAuthorizing] = useState(true);
30+
const [clientId, setClientId] = useState(session.clientId);
3031
const [error, setError] = useState(false);
3132
const [networkError, setNetworkError] = useState(false);
3233

@@ -38,10 +39,13 @@ function App() {
3839
API.onConnectionRefused(() => {
3940
setNetworkError(true);
4041
});
42+
if (!clientId) API.setupOAuth2Credentials().then(([clientId]) => {
43+
setClientId(clientId);
44+
});
4145
}, []);
4246

4347
useEffect(() => {
44-
if (authorizing) {
48+
if (authorizing && clientId) {
4549
const params = QueryString.parse(window.location.search.slice(1, window.location.search.length));
4650

4751
if (params.cenitHost) session.cenitBackendBaseUrl = params.cenitHost;
@@ -64,7 +68,7 @@ function App() {
6468

6569
return () => subscription.unsubscribe();
6670
}
67-
}, [authorizing]);
71+
}, [authorizing, clientId]);
6872

6973
if (authorizing) {
7074
return <div className='flex full-width full-v-height justify-content-center align-items-center'>

src/services/ApiService.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import axios from 'axios';
22
import { apiRequest } from './AuthorizationService';
33
import { catchError } from "rxjs/operators";
44
import { from, of, throwError } from "rxjs";
5+
import session from "../util/session";
56

67
export const ApiResource = function () {
78

@@ -125,7 +126,21 @@ const API = {
125126

126127
onError: callback => ErrorCallbacks.push(callback),
127128

128-
onConnectionRefused: callback => ConnectionRefusedCallbacks.push(callback)
129+
onConnectionRefused: callback => ConnectionRefusedCallbacks.push(callback),
130+
131+
setupOAuth2Credentials: () => {
132+
const url = `${session.cenitBackendBaseUrl}/app/admin/oauth2/client/credentials`;
133+
const reverse = (str) => str.split('').reverse().join('');
134+
135+
return axios.get(url).then(({ data: { client_token } }) => {
136+
const { OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET } = JSON.parse(window.atob(reverse(client_token)));
137+
138+
session.set('OAUTH_CLIENT_ID', OAUTH_CLIENT_ID);
139+
session.set('OAUTH_CLIENT_SECRET', OAUTH_CLIENT_SECRET);
140+
141+
return [OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET];
142+
});
143+
}
129144
};
130145

131146
export default API;

0 commit comments

Comments
 (0)