implement oidc discovery

This commit is contained in:
Abdul Rifqi Al Abqary
2020-01-06 16:44:50 +09:00
parent 2e7e40c4cc
commit 83a20bd7de
2 changed files with 26 additions and 2 deletions

View File

@@ -30,6 +30,23 @@ const parseQueryString = string => {
segments.forEach(s => queryString[s[0]] = s[1]);
return queryString;
}
// Get OAuth configuration from OpenID Discovery endpoint
const getTokenConfiguration = async endpoint => {
const options = {
method: 'GET',
headers: {
'Content-type': 'application/json'
}
}
try {
const response = await fetch(endpoint, options);
const config = await response.json();
return config;
} catch (err) {
console.error('Request failed', err);
throw err;
}
}
//////////////////////////////////////////////////////////////////////
// PKCE HELPER FUNCTIONS
@@ -67,6 +84,7 @@ const pkceChallengeFromVerifier = async(v) => {
// Initiate PKCE Auth Code flow when requested
const tokenRequest = async({
oidcDiscoveryUrl,
grantType,
authUrl,
accessTokenUrl,
@@ -74,6 +92,13 @@ const tokenRequest = async({
scope
}) => {
// Check configuration URLs
if (oidcDiscoveryUrl !== '') {
const { authorization_endpoint, token_endpoint } = await getTokenConfiguration(oidcDiscoveryUrl);
authUrl = authorization_endpoint;
accessTokenUrl = token_endpoint;
}
// Store oauth information
localStorage.setItem('token_endpoint', accessTokenUrl);
localStorage.setItem('client_id', clientId);

View File

@@ -2455,8 +2455,7 @@ export default {
authUrl: this.authUrl,
accessTokenUrl: this.accessTokenUrl,
clientId: this.clientId,
scope: this.scope,
clientAuth: this.clientAuth
scope: this.scope
};
await tokenRequest(tokenReqParams);
} catch (e) {