store access token and refresh token in the file system /tmp

This commit is contained in:
spencerwooo
2021-12-31 12:17:00 +08:00
parent 38d24146fa
commit 45ef22f34a
4 changed files with 70 additions and 26 deletions
+8 -10
View File
@@ -1,24 +1,22 @@
import axios from 'axios'
import CryptoJS from 'crypto-js'
import Keyv from 'keyv'
import KeyvFile from 'keyv-file'
import apiConfig from '../config/api.json'
// Just a disguise to obfuscate the client secret, used along with the following two functions
const AES_SECRET_KEY = 'onedrive-vercel-index'
export function obfuscateToken(token: string): string {
function obfuscateToken(token: string): string {
// Encrypt token with AES
const encrypted = CryptoJS.AES.encrypt(token, AES_SECRET_KEY)
return encrypted.toString()
}
export function revealObfuscatedToken(obfuscated: string): string {
// Decrypt SHA256 obfuscated token
const decrypted = CryptoJS.AES.decrypt(obfuscated, AES_SECRET_KEY)
return decrypted.toString(CryptoJS.enc.Utf8)
}
// Generate the Microsoft OAuth 2.0 authorization URL, used for requesting the authorisation code
export function generateAuthorisationUrl(): string {
const { clientId, redirectUri, authApi } = apiConfig
const authUrl = authApi.replace('/token', '/authorize')
@@ -34,6 +32,8 @@ export function generateAuthorisationUrl(): string {
return `${authUrl}?${params.toString()}`
}
// The code returned from the Microsoft OAuth 2.0 authorization URL is a request URL with hostname
// http://localhost and URL parameter code. This function extracts the code from the request URL
export function extractAuthCodeFromRedirected(url: string): string {
// Return empty string if the url is not the defined redirect uri
if (!url.startsWith(apiConfig.redirectUri)) {
@@ -45,6 +45,9 @@ export function extractAuthCodeFromRedirected(url: string): string {
return params.get('code') || ''
}
// After a successful authorisation, the code returned from the Microsoft OAuth 2.0 authorization URL
// will be used to request an access token. This function requests the access token with the authorisation code
// and returns the access token and refresh token on success.
export async function requestTokenWithAuthCode(
code: string
): Promise<
@@ -78,8 +81,3 @@ export async function requestTokenWithAuthCode(
return { error, errorDescription: error_description, errorUri: error_uri }
})
}
export function storeTokens(accessToken: string, refreshToken: string) {
// We can safely leverage Vercel's /tmp directory, and persist the tokens with file-system based KV storage
const kv = new Keyv({ store: new KeyvFile(), namespace: 'onedrive-vercel-index' })
}
+32
View File
@@ -0,0 +1,32 @@
// This should be only used on the server side, where the tokens are stored with KV store using
// a file system based storage. The tokens are stored in the file system as JSON at /tmp path.
import Keyv from 'keyv'
import { KeyvFile } from 'keyv-file'
const kv = new Keyv({
store: new KeyvFile(),
})
export async function storeOdAuthTokens({
accessToken,
accessTokenExpiry,
refreshToken,
}: {
accessToken: string
accessTokenExpiry: number
refreshToken: string
}): Promise<void> {
await kv.set('access_token', accessToken, accessTokenExpiry)
await kv.set('refresh_token', refreshToken)
}
export async function getOdAuthTokens(): Promise<{ accessToken: unknown; refreshToken: unknown }> {
const accessToken = await kv.get('access_token')
const refreshToken = await kv.get('refresh_token')
return {
accessToken,
refreshToken,
}
}