api-client/helpers/oauth.js

240 lines
6 KiB
JavaScript
Raw Normal View History

2021-06-21 04:27:45 +00:00
import {
getLocalConfig,
setLocalConfig,
removeLocalConfig,
} from "~/newstore/localpersistence"
2020-02-24 18:44:50 +00:00
const redirectUri = `${window.location.origin}/`
2020-01-06 06:06:50 +00:00
// GENERAL HELPER FUNCTIONS
/**
2020-01-11 07:41:02 +00:00
* Makes a POST request and parse the response as JSON
*
* @param {String} url - The resource
* @param {Object} params - Configuration options
* @returns {Object}
*/
2020-01-10 00:57:48 +00:00
const sendPostRequest = async (url, params) => {
2020-01-11 05:12:08 +00:00
const body = Object.keys(params)
2020-06-19 06:56:04 +00:00
.map((key) => `${key}=${params[key]}`)
.join("&")
2020-01-06 06:06:50 +00:00
const options = {
method: "post",
2020-01-06 06:06:50 +00:00
headers: {
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8",
2020-01-06 06:06:50 +00:00
},
2020-02-24 18:44:50 +00:00
body,
}
2020-01-06 06:06:50 +00:00
try {
2020-02-24 18:44:50 +00:00
const response = await fetch(url, options)
const data = await response.json()
return data
2020-01-06 06:06:50 +00:00
} catch (err) {
console.error("Request failed", err)
2020-02-24 18:44:50 +00:00
throw err
2020-01-06 06:06:50 +00:00
}
2020-02-24 18:44:50 +00:00
}
/**
* Parse a query string into an object
*
2020-01-11 07:43:53 +00:00
* @param {String} searchQuery - The search query params
* @returns {Object}
*/
2020-06-19 06:56:04 +00:00
const parseQueryString = (searchQuery) => {
if (searchQuery === "") {
2020-02-24 18:44:50 +00:00
return {}
2020-01-10 00:57:48 +00:00
}
2020-06-19 06:56:04 +00:00
const segments = searchQuery.split("&").map((s) => s.split("="))
2021-05-18 09:27:29 +00:00
const queryString = segments.reduce(
(obj, el) => ({ ...obj, [el[0]]: el[1] }),
{}
)
2020-02-24 18:44:50 +00:00
return queryString
}
/**
* Get OAuth configuration from OpenID Discovery endpoint
*
* @returns {Object}
*/
2020-06-19 06:56:04 +00:00
const getTokenConfiguration = async (endpoint) => {
2020-01-06 07:44:50 +00:00
const options = {
method: "GET",
2020-01-06 07:44:50 +00:00
headers: {
"Content-type": "application/json",
2020-02-24 18:44:50 +00:00
},
}
2020-01-06 07:44:50 +00:00
try {
2020-02-24 18:44:50 +00:00
const response = await fetch(endpoint, options)
const config = await response.json()
return config
2020-01-06 07:44:50 +00:00
} catch (err) {
console.error("Request failed", err)
2020-02-24 18:44:50 +00:00
throw err
2020-01-06 07:44:50 +00:00
}
2020-02-24 18:44:50 +00:00
}
2020-01-06 06:06:50 +00:00
// PKCE HELPER FUNCTIONS
/**
2020-01-11 07:47:24 +00:00
* Generates a secure random string using the browser crypto functions
*
* @returns {Object}
*/
2020-01-06 06:06:50 +00:00
const generateRandomString = () => {
2020-02-24 18:44:50 +00:00
const array = new Uint32Array(28)
window.crypto.getRandomValues(array)
2020-06-19 06:56:04 +00:00
return Array.from(array, (dec) => `0${dec.toString(16)}`.substr(-2)).join("")
2020-02-24 18:44:50 +00:00
}
/**
* Calculate the SHA256 hash of the input text
*
* @returns {Promise<ArrayBuffer>}
*/
2020-06-19 06:56:04 +00:00
const sha256 = (plain) => {
2020-02-24 18:44:50 +00:00
const encoder = new TextEncoder()
const data = encoder.encode(plain)
return window.crypto.subtle.digest("SHA-256", data)
2020-02-24 18:44:50 +00:00
}
/**
2020-01-11 07:55:01 +00:00
* Encodes the input string into Base64 format
*
* @param {String} str - The string to be converted
* @returns {Promise<ArrayBuffer>}
*/
2020-01-10 00:57:48 +00:00
const base64urlencode = (
2020-01-11 07:55:01 +00:00
str // Converts the ArrayBuffer to string using Uint8 array to convert to what btoa accepts.
2020-01-10 00:57:48 +00:00
) =>
2020-01-06 06:06:50 +00:00
// btoa accepts chars only within ascii 0-255 and base64 encodes them.
// Then convert the base64 encoded to base64url encoded
// (replace + with -, replace / with _, trim trailing =)
2020-01-10 00:57:48 +00:00
btoa(String.fromCharCode.apply(null, new Uint8Array(str)))
.replace(/\+/g, "-")
.replace(/\//g, "_")
.replace(/=+$/, "")
/**
* Return the base64-urlencoded sha256 hash for the PKCE challenge
*
* @param {String} v - The randomly generated string
* @returns {String}
*/
2020-06-19 06:56:04 +00:00
const pkceChallengeFromVerifier = async (v) => {
2020-02-24 18:44:50 +00:00
const hashed = await sha256(v)
return base64urlencode(hashed)
}
2020-01-06 06:06:50 +00:00
// OAUTH REQUEST
/**
2020-01-11 08:00:03 +00:00
* Initiates PKCE Auth Code flow when requested
*
* @param {Object} - The necessary params
* @returns {Void}
*/
2020-01-10 00:57:48 +00:00
const tokenRequest = async ({
2020-01-06 07:44:50 +00:00
oidcDiscoveryUrl,
2020-01-06 06:06:50 +00:00
grantType,
authUrl,
accessTokenUrl,
clientId,
2020-02-24 18:44:50 +00:00
scope,
2020-01-06 06:06:50 +00:00
}) => {
2020-01-06 07:50:20 +00:00
// Check oauth configuration
if (oidcDiscoveryUrl !== "") {
2021-05-18 09:27:29 +00:00
const { authorizationEndpoint, tokenEndpoint } =
await getTokenConfiguration(oidcDiscoveryUrl)
authUrl = authorizationEndpoint
accessTokenUrl = tokenEndpoint
2020-01-06 07:44:50 +00:00
}
2020-01-06 06:06:50 +00:00
// Store oauth information
2021-06-21 04:27:45 +00:00
setLocalConfig("tokenEndpoint", accessTokenUrl)
setLocalConfig("client_id", clientId)
2020-01-06 06:06:50 +00:00
// Create and store a random state value
2020-02-24 18:44:50 +00:00
const state = generateRandomString()
2021-06-21 04:27:45 +00:00
setLocalConfig("pkce_state", state)
2020-01-06 06:06:50 +00:00
2021-05-18 09:27:29 +00:00
// Create and store a new PKCE codeVerifier (the plaintext random secret)
const codeVerifier = generateRandomString()
2021-06-21 04:27:45 +00:00
setLocalConfig("pkce_codeVerifier", codeVerifier)
2020-01-06 06:06:50 +00:00
// Hash and base64-urlencode the secret to use as the challenge
2021-05-18 09:27:29 +00:00
const codeChallenge = await pkceChallengeFromVerifier(codeVerifier)
2020-01-06 06:06:50 +00:00
// Build the authorization URL
2020-01-10 00:57:48 +00:00
const buildUrl = () =>
`${authUrl + `?response_type=${grantType}`}&client_id=${encodeURIComponent(
clientId
)}&state=${encodeURIComponent(state)}&scope=${encodeURIComponent(
scope
2021-05-18 09:27:29 +00:00
)}&redirect_uri=${encodeURIComponent(
redirectUri
)}&codeChallenge=${encodeURIComponent(
codeChallenge
)}&codeChallenge_method=S256`
2020-01-06 06:06:50 +00:00
// Redirect to the authorization server
2020-02-24 18:44:50 +00:00
window.location = buildUrl()
}
2020-01-06 06:06:50 +00:00
// OAUTH REDIRECT HANDLING
/**
* Handle the redirect back from the authorization server and
* get an access token from the token endpoint
*
* @returns {Object}
*/
2021-06-21 04:27:45 +00:00
const oauthRedirect = () => {
let tokenResponse = ""
2021-05-18 09:27:29 +00:00
const q = parseQueryString(window.location.search.substring(1))
2020-01-06 06:06:50 +00:00
// Check if the server returned an error string
2020-01-10 00:57:48 +00:00
if (q.error) {
2020-02-24 18:44:50 +00:00
alert(`Error returned from authorization server: ${q.error}`)
2020-01-06 06:06:50 +00:00
}
// If the server returned an authorization code, attempt to exchange it for an access token
2020-01-10 00:57:48 +00:00
if (q.code) {
2020-01-06 06:06:50 +00:00
// Verify state matches what we set at the beginning
2021-06-21 04:27:45 +00:00
if (getLocalConfig("pkce_state") !== q.state) {
alert("Invalid state")
2020-01-06 06:06:50 +00:00
} else {
try {
// Exchange the authorization code for an access token
2021-06-21 04:27:45 +00:00
tokenResponse = sendPostRequest(getLocalConfig("tokenEndpoint"), {
grant_type: "authorization_code",
code: q.code,
client_id: getLocalConfig("client_id"),
redirect_uri: redirectUri,
codeVerifier: getLocalConfig("pkce_codeVerifier"),
})
2020-01-06 06:06:50 +00:00
} catch (err) {
2020-02-24 18:44:50 +00:00
console.log(`${error.error}\n\n${error.error_description}`)
2020-01-06 06:06:50 +00:00
}
}
// Clean these up since we don't need them anymore
2021-06-21 04:27:45 +00:00
removeLocalConfig("pkce_state")
removeLocalConfig("pkce_codeVerifier")
removeLocalConfig("tokenEndpoint")
removeLocalConfig("client_id")
2020-02-24 18:44:50 +00:00
return tokenResponse
2020-01-06 06:06:50 +00:00
}
2020-02-24 18:44:50 +00:00
return tokenResponse
}
2020-01-06 06:06:50 +00:00
2020-02-24 18:44:50 +00:00
export { tokenRequest, oauthRedirect }