2021-02-14 18:21:34 +00:00
|
|
|
import {stringifyParams} from 'query'
|
2021-02-20 18:31:18 +00:00
|
|
|
import globalStore from 'store'
|
|
|
|
import {setAuth, invalidateAccessToken, resetAuth} from 'reducers/auth'
|
|
|
|
import {setLogin} from 'reducers/login'
|
|
|
|
import config from 'config.json'
|
2021-02-23 18:32:06 +00:00
|
|
|
import {create as createPkce} from 'pkce'
|
2021-02-14 18:21:34 +00:00
|
|
|
|
2021-02-23 20:52:57 +00:00
|
|
|
class RequestError extends Error {
|
|
|
|
constructor(message, errors) {
|
|
|
|
super(message)
|
|
|
|
this.errors = errors
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-10 21:28:36 +00:00
|
|
|
class API {
|
2021-02-20 18:31:18 +00:00
|
|
|
constructor(store) {
|
|
|
|
this.store = store
|
|
|
|
this._getValidAccessTokenPromise = null
|
|
|
|
}
|
|
|
|
|
2021-02-23 17:26:37 +00:00
|
|
|
/**
|
|
|
|
* Fetches or directly returns from cache the metadata information from the
|
|
|
|
* authorization server, according to https://tools.ietf.org/html/rfc8414.
|
|
|
|
* Also validates compatibility with this metadata server, i.e. checking that
|
|
|
|
* it supports PKCE.
|
|
|
|
*/
|
|
|
|
async getAuthorizationServerMetadata() {
|
|
|
|
const url = new URL(config.auth.server)
|
|
|
|
const pathSuffix = url.pathname.replace(/^\/+|\/+$/, '')
|
|
|
|
url.pathname = '/.well-known/oauth-authorization-server' + (pathSuffix ? '/' + pathSuffix : '')
|
|
|
|
|
|
|
|
const response = await window.fetch(url.toString())
|
|
|
|
const metadata = await response.json()
|
|
|
|
|
2021-02-23 18:32:06 +00:00
|
|
|
const {
|
|
|
|
authorization_endpoint: authorizationEndpoint,
|
|
|
|
token_endpoint: tokenEndpoint,
|
2021-02-23 17:26:37 +00:00
|
|
|
response_types_supported: responseTypesSupported,
|
|
|
|
code_challenge_methods_supported: codeChallengeMethodsSupported,
|
|
|
|
} = metadata
|
|
|
|
if (!authorizationEndpoint) {
|
2021-02-23 18:32:06 +00:00
|
|
|
throw new Error('No authorization endpoint')
|
2021-02-23 17:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!authorizationEndpoint.startsWith(config.auth.server)) {
|
2021-02-23 18:32:06 +00:00
|
|
|
throw new Error('Invalid authorization endpoint')
|
2021-02-23 17:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!tokenEndpoint) {
|
2021-02-23 18:32:06 +00:00
|
|
|
throw new Error('No token endpoint')
|
2021-02-23 17:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!tokenEndpoint.startsWith(config.auth.server)) {
|
2021-02-23 18:32:06 +00:00
|
|
|
throw new Error('Invalid token endpoint')
|
2021-02-23 17:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!Array.isArray(responseTypesSupported) || !responseTypesSupported.includes('code')) {
|
2021-02-23 18:32:06 +00:00
|
|
|
throw new Error('Authorization code flow not supported or no support advertised.')
|
2021-02-23 17:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!Array.isArray(codeChallengeMethodsSupported) || !codeChallengeMethodsSupported.includes('S256')) {
|
2021-02-23 18:32:06 +00:00
|
|
|
throw new Error('PKCE with S256 not supported or no support advertised.')
|
2021-02-23 17:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {authorizationEndpoint, tokenEndpoint}
|
|
|
|
}
|
|
|
|
|
2021-02-20 18:31:18 +00:00
|
|
|
/**
|
|
|
|
* Return an access token, if it is (still) valid. If not, and a refresh
|
|
|
|
* token exists, use the refresh token to issue a new access token. If that
|
|
|
|
* fails, or neither is available, return `null`. This should usually result
|
|
|
|
* in a redirect to login.
|
|
|
|
*/
|
|
|
|
async getValidAccessToken() {
|
|
|
|
// prevent multiple parallel refresh processes
|
|
|
|
if (this._getValidAccessTokenPromise) {
|
|
|
|
return await this._getValidAccessTokenPromise
|
|
|
|
} else {
|
|
|
|
this._getValidAccessTokenPromise = this._getValidAccessToken()
|
|
|
|
const result = await this._getValidAccessTokenPromise
|
|
|
|
this._getValidAccessTokenPromise = null
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async _getValidAccessToken() {
|
|
|
|
let {auth} = this.store.getState()
|
|
|
|
|
|
|
|
if (!auth) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
const {tokenType, accessToken, refreshToken, expiresAt} = auth
|
|
|
|
|
|
|
|
// access token is valid
|
|
|
|
if (accessToken && expiresAt > new Date().getTime()) {
|
|
|
|
return `${tokenType} ${accessToken}`
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!refreshToken) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to use the refresh token
|
2021-02-23 17:26:37 +00:00
|
|
|
const {tokenEndpoint} = await this.getAuthorizationServerMetadata()
|
|
|
|
const url = new URL(tokenEndpoint)
|
2021-02-20 18:31:18 +00:00
|
|
|
url.searchParams.append('refresh_token', refreshToken)
|
|
|
|
url.searchParams.append('grant_type', 'refresh_token')
|
|
|
|
url.searchParams.append('scope', config.auth.scope)
|
|
|
|
const response = await window.fetch(url.toString())
|
|
|
|
const json = await response.json()
|
|
|
|
|
|
|
|
if (response.status === 200 && json != null && json.error == null) {
|
|
|
|
auth = this.getAuthFromTokenResponse(json)
|
|
|
|
this.store.dispatch(setAuth(auth))
|
|
|
|
return `${auth.tokenType} ${auth.accessToken}`
|
|
|
|
} else {
|
|
|
|
console.warn('Could not use refresh token, error response:', json)
|
|
|
|
this.store.dispatch(resetAuth())
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async exchangeAuthorizationCode(code) {
|
2021-02-23 18:32:06 +00:00
|
|
|
const codeVerifier = localStorage.getItem('codeVerifier');
|
|
|
|
if (!codeVerifier) {
|
|
|
|
throw new Error("No code verifier found");
|
|
|
|
}
|
|
|
|
|
2021-02-23 17:26:37 +00:00
|
|
|
const {tokenEndpoint} = await this.getAuthorizationServerMetadata()
|
|
|
|
const url = new URL(tokenEndpoint)
|
2021-02-20 18:31:18 +00:00
|
|
|
url.searchParams.append('code', code)
|
|
|
|
url.searchParams.append('grant_type', 'authorization_code')
|
|
|
|
url.searchParams.append('client_id', config.auth.clientId)
|
|
|
|
url.searchParams.append('redirect_uri', config.auth.redirectUri)
|
2021-02-23 18:32:06 +00:00
|
|
|
url.searchParams.append('code_verifier', codeVerifier)
|
2021-02-20 18:31:18 +00:00
|
|
|
const response = await window.fetch(url.toString())
|
|
|
|
const json = await response.json()
|
|
|
|
|
|
|
|
if (json.error) {
|
|
|
|
return json
|
|
|
|
}
|
|
|
|
|
|
|
|
const auth = api.getAuthFromTokenResponse(json)
|
|
|
|
this.store.dispatch(setAuth(auth))
|
|
|
|
|
|
|
|
const {user} = await this.get('/user')
|
|
|
|
this.store.dispatch(setLogin(user))
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-02-23 17:26:37 +00:00
|
|
|
async makeLoginUrl() {
|
|
|
|
const {authorizationEndpoint} = await this.getAuthorizationServerMetadata()
|
|
|
|
|
2021-02-23 18:32:06 +00:00
|
|
|
const {codeVerifier, codeChallenge} = createPkce()
|
|
|
|
localStorage.setItem("codeVerifier", codeVerifier);
|
|
|
|
|
2021-02-23 17:26:37 +00:00
|
|
|
const loginUrl = new URL(authorizationEndpoint)
|
2021-02-20 18:31:18 +00:00
|
|
|
loginUrl.searchParams.append('client_id', config.auth.clientId)
|
|
|
|
loginUrl.searchParams.append('scope', config.auth.scope)
|
|
|
|
loginUrl.searchParams.append('redirect_uri', config.auth.redirectUri)
|
|
|
|
loginUrl.searchParams.append('response_type', 'code')
|
2021-02-23 18:32:06 +00:00
|
|
|
loginUrl.searchParams.append('code_challenge', codeChallenge)
|
|
|
|
loginUrl.searchParams.append('code_challenge_method', 'S256')
|
2021-02-20 18:31:18 +00:00
|
|
|
|
|
|
|
// TODO: Implement PKCE
|
|
|
|
|
|
|
|
return loginUrl.toString()
|
2021-02-10 21:28:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fetch(url, options = {}) {
|
2021-02-20 18:31:18 +00:00
|
|
|
const accessToken = await this.getValidAccessToken()
|
|
|
|
|
2021-02-10 21:28:36 +00:00
|
|
|
const response = await window.fetch('/api' + url, {
|
|
|
|
...options,
|
|
|
|
headers: {
|
|
|
|
...(options.headers || {}),
|
2021-02-20 18:31:18 +00:00
|
|
|
Authorization: accessToken,
|
2021-02-10 21:28:36 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2021-02-20 18:31:18 +00:00
|
|
|
if (response.status === 401) {
|
|
|
|
// Unset login, since 401 means that we're not logged in. On the next
|
|
|
|
// request with `getValidAccessToken()`, this will be detected and the
|
|
|
|
// refresh token is used (if still valid).
|
|
|
|
this.store.dispatch(invalidateAccessToken())
|
|
|
|
|
|
|
|
throw new Error('401 Unauthorized')
|
|
|
|
}
|
|
|
|
|
2021-02-23 20:52:57 +00:00
|
|
|
let json
|
|
|
|
try {
|
|
|
|
json = await response.json()
|
|
|
|
} catch (err) {
|
|
|
|
json = null
|
|
|
|
}
|
|
|
|
|
2021-02-14 18:21:34 +00:00
|
|
|
if (response.status === 200) {
|
2021-02-23 20:52:57 +00:00
|
|
|
return json
|
2021-02-14 18:21:34 +00:00
|
|
|
} else {
|
2021-02-23 20:52:57 +00:00
|
|
|
throw new RequestError('Error code ' + response.status, json?.errors)
|
2021-02-14 18:21:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async post(url, {body: body_, ...options}) {
|
2021-02-17 20:50:18 +00:00
|
|
|
let body = body_
|
|
|
|
let headers = {...(options.headers || {})}
|
|
|
|
|
|
|
|
if (!(typeof body === 'string' || body instanceof FormData)) {
|
2021-02-20 18:31:18 +00:00
|
|
|
body = JSON.stringify(body)
|
|
|
|
headers['Content-Type'] = 'application/json'
|
2021-02-17 20:50:18 +00:00
|
|
|
}
|
|
|
|
|
2021-02-14 18:27:16 +00:00
|
|
|
return await this.fetch(url, {
|
2021-02-23 20:52:57 +00:00
|
|
|
method: 'post',
|
2021-02-14 18:27:16 +00:00
|
|
|
...options,
|
|
|
|
body,
|
2021-02-20 18:31:18 +00:00
|
|
|
headers,
|
2021-02-14 18:21:34 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async get(url, {query, ...options} = {}) {
|
|
|
|
const queryString = query ? stringifyParams(query) : null
|
|
|
|
return await this.fetch(url + (queryString ? '?' + queryString : ''), {method: 'get', ...options})
|
|
|
|
}
|
|
|
|
|
|
|
|
async delete(url, options = {}) {
|
2021-02-14 18:27:16 +00:00
|
|
|
return await this.get(url, {...options, method: 'delete'})
|
2021-02-10 21:28:36 +00:00
|
|
|
}
|
2021-02-20 18:31:18 +00:00
|
|
|
|
2021-02-23 20:52:57 +00:00
|
|
|
async put(url, options = {}) {
|
|
|
|
return await this.post(url, {...options, method: 'put'})
|
|
|
|
}
|
|
|
|
|
2021-02-20 18:31:18 +00:00
|
|
|
getAuthFromTokenResponse(tokenResponse) {
|
|
|
|
return {
|
|
|
|
tokenType: tokenResponse.token_type,
|
|
|
|
accessToken: tokenResponse.access_token,
|
|
|
|
refreshToken: tokenResponse.refresh_token,
|
|
|
|
expiresAt: new Date().getTime() + tokenResponse.expires_in * 1000,
|
|
|
|
scope: tokenResponse.scope,
|
|
|
|
}
|
|
|
|
}
|
2021-02-10 21:28:36 +00:00
|
|
|
}
|
|
|
|
|
2021-02-20 18:31:18 +00:00
|
|
|
const api = new API(globalStore)
|
2021-02-10 21:28:36 +00:00
|
|
|
|
|
|
|
export default api
|