feat: add OAuth 2.0 support

This commit is contained in:
liyasthomas
2021-08-25 21:30:13 +05:30
parent 9e16cff62a
commit bc9236c9a7
7 changed files with 250 additions and 18 deletions

View File

@@ -198,7 +198,7 @@ const tokenRequest = async ({
* Handle the redirect back from the authorization server and
* get an access token from the token endpoint
*
* @returns {Object}
* @returns {Promise<any | void>}
*/
const oauthRedirect = () => {
@@ -213,6 +213,7 @@ const oauthRedirect = () => {
// Verify state matches what we set at the beginning
if (getLocalConfig("pkce_state") !== q.state) {
alert("Invalid state")
Promise.reject(tokenResponse)
} else {
try {
// Exchange the authorization code for an access token
@@ -225,6 +226,7 @@ const oauthRedirect = () => {
})
} catch (e) {
console.error(e)
return Promise.reject(tokenResponse)
}
}
// Clean these up since we don't need them anymore
@@ -234,7 +236,7 @@ const oauthRedirect = () => {
removeLocalConfig("client_id")
return tokenResponse
}
return tokenResponse
return Promise.reject(tokenResponse)
}
export { tokenRequest, oauthRedirect }

View File

@@ -15,8 +15,20 @@ export type HoppRESTAuthBearer = {
token: string
}
export type HoppRESTAuthOAuth2 = {
authType: "oauth-2"
token: string
oidcDiscoveryURL: string
authURL: string
accessTokenURL: string
clientID: string
scope: string
}
export type HoppRESTAuth = { authActive: boolean } & (
| HoppRESTAuthNone
| HoppRESTAuthBasic
| HoppRESTAuthBearer
| HoppRESTAuthOAuth2
)

View File

@@ -87,7 +87,10 @@ export function getEffectiveRESTRequest(
`${request.auth.username}:${request.auth.password}`
)}`,
})
} else if (request.auth.authType === "bearer") {
} else if (
request.auth.authType === "bearer" ||
request.auth.authType === "oauth-2"
) {
effectiveFinalHeaders.push({
active: true,
key: "Authorization",