refactor: move persistence logic into a dedicated service (#3493)
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
import {
|
||||
getLocalConfig,
|
||||
setLocalConfig,
|
||||
removeLocalConfig,
|
||||
} from "~/newstore/localpersistence"
|
||||
import { getService } from "~/modules/dioc"
|
||||
import { PersistenceService } from "~/services/persistence"
|
||||
|
||||
import * as E from "fp-ts/Either"
|
||||
import { z } from "zod"
|
||||
|
||||
const redirectUri = `${window.location.origin}/oauth`
|
||||
|
||||
const persistenceService = getService(PersistenceService)
|
||||
|
||||
// GENERAL HELPER FUNCTIONS
|
||||
|
||||
/**
|
||||
@@ -190,17 +189,17 @@ const tokenRequest = async ({
|
||||
accessTokenUrl = parsedOIDCConfiguration.data.token_endpoint
|
||||
}
|
||||
// Store oauth information
|
||||
setLocalConfig("tokenEndpoint", accessTokenUrl)
|
||||
setLocalConfig("client_id", clientId)
|
||||
setLocalConfig("client_secret", clientSecret)
|
||||
persistenceService.setLocalConfig("tokenEndpoint", accessTokenUrl)
|
||||
persistenceService.setLocalConfig("client_id", clientId)
|
||||
persistenceService.setLocalConfig("client_secret", clientSecret)
|
||||
|
||||
// Create and store a random state value
|
||||
const state = generateRandomString()
|
||||
setLocalConfig("pkce_state", state)
|
||||
persistenceService.setLocalConfig("pkce_state", state)
|
||||
|
||||
// Create and store a new PKCE codeVerifier (the plaintext random secret)
|
||||
const codeVerifier = generateRandomString()
|
||||
setLocalConfig("pkce_codeVerifier", codeVerifier)
|
||||
persistenceService.setLocalConfig("pkce_codeVerifier", codeVerifier)
|
||||
|
||||
// Hash and base64-urlencode the secret to use as the challenge
|
||||
const codeChallenge = await pkceChallengeFromVerifier(codeVerifier)
|
||||
@@ -244,14 +243,14 @@ const handleOAuthRedirect = async () => {
|
||||
|
||||
// If the server returned an authorization code, attempt to exchange it for an access token
|
||||
// Verify state matches what we set at the beginning
|
||||
if (getLocalConfig("pkce_state") !== queryParams.state) {
|
||||
if (persistenceService.getLocalConfig("pkce_state") !== queryParams.state) {
|
||||
return E.left("INVALID_STATE" as const)
|
||||
}
|
||||
|
||||
const tokenEndpoint = getLocalConfig("tokenEndpoint")
|
||||
const clientID = getLocalConfig("client_id")
|
||||
const clientSecret = getLocalConfig("client_secret")
|
||||
const codeVerifier = getLocalConfig("pkce_codeVerifier")
|
||||
const tokenEndpoint = persistenceService.getLocalConfig("tokenEndpoint")
|
||||
const clientID = persistenceService.getLocalConfig("client_id")
|
||||
const clientSecret = persistenceService.getLocalConfig("client_secret")
|
||||
const codeVerifier = persistenceService.getLocalConfig("pkce_codeVerifier")
|
||||
|
||||
if (!tokenEndpoint) {
|
||||
return E.left("NO_TOKEN_ENDPOINT" as const)
|
||||
@@ -303,11 +302,11 @@ const handleOAuthRedirect = async () => {
|
||||
}
|
||||
|
||||
const clearPKCEState = () => {
|
||||
removeLocalConfig("pkce_state")
|
||||
removeLocalConfig("pkce_codeVerifier")
|
||||
removeLocalConfig("tokenEndpoint")
|
||||
removeLocalConfig("client_id")
|
||||
removeLocalConfig("client_secret")
|
||||
persistenceService.removeLocalConfig("pkce_state")
|
||||
persistenceService.removeLocalConfig("pkce_codeVerifier")
|
||||
persistenceService.removeLocalConfig("tokenEndpoint")
|
||||
persistenceService.removeLocalConfig("client_id")
|
||||
persistenceService.removeLocalConfig("client_secret")
|
||||
}
|
||||
|
||||
export { tokenRequest, handleOAuthRedirect }
|
||||
|
||||
Reference in New Issue
Block a user