-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat/fallback
- Loading branch information
Showing
11 changed files
with
153 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ on: | |
branches: | ||
- main | ||
jobs: | ||
cicd: | ||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// @ts-check | ||
|
||
import { indexedDbStorage } from './indexed-db-storage.js' | ||
import { memoryStorage } from './memory-storage.js' | ||
|
||
/** | ||
* @typedef {object} Storage | ||
* @property {function(string):Promise<any>} get - Retrieves the value associated with the key. | ||
* @property {function(string,any):Promise<void>} set - Sets a new value for the key. | ||
* @property {function(string):Promise<any>} delete - Deletes the value associated with the key. | ||
*/ | ||
|
||
export { | ||
indexedDbStorage, | ||
memoryStorage | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// @ts-check | ||
|
||
import { openDB } from 'idb' | ||
|
||
const DEFAULT_IDB_VERSION = 1 | ||
const DEFAULT_IDB_STORAGE_NAME = 'saturn-db' | ||
const DEFAULT_SATURN_STORAGE_NAME = 'saturn-client' | ||
|
||
/** | ||
* @function indexedDbStorage | ||
* @returns {import('./index.js').Storage} | ||
*/ | ||
export function indexedDbStorage () { | ||
const indexedDbExists = (typeof window !== 'undefined') && window?.indexedDB | ||
let dbPromise | ||
if (indexedDbExists) { | ||
dbPromise = openDB(DEFAULT_IDB_STORAGE_NAME, DEFAULT_IDB_VERSION, { | ||
upgrade (db) { | ||
db.createObjectStore(DEFAULT_SATURN_STORAGE_NAME) | ||
} | ||
}) | ||
} | ||
|
||
return { | ||
get: async (key) => indexedDbExists && (await dbPromise).get(DEFAULT_SATURN_STORAGE_NAME, key), | ||
set: async (key, value) => indexedDbExists && (await dbPromise).put(DEFAULT_SATURN_STORAGE_NAME, value, key), | ||
delete: async (key) => indexedDbExists && (await dbPromise).delete(DEFAULT_SATURN_STORAGE_NAME, key) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// @ts-check | ||
|
||
/** | ||
* @function memoryStorage | ||
* @returns {import('./index.js').Storage} | ||
*/ | ||
export function memoryStorage () { | ||
const storageObject = {} | ||
|
||
return { | ||
get: async (key) => storageObject[key], | ||
set: async (key, value) => { storageObject[key] = value }, | ||
delete: async (key) => { delete storageObject[key] } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { base64 } from 'multiformats/bases/base64' | ||
import { bytes } from 'multiformats' | ||
|
||
const JWT_KEY = 'strn/jwt' | ||
|
||
/** | ||
* @param {string} jwt | ||
*/ | ||
export function isJwtValid (jwt) { | ||
if (!jwt) return false | ||
const { exp } = JSON.parse(bytes.toString(base64.decode('m' + jwt.split('.')[1]))) | ||
return Date.now() < exp * 1000 | ||
} | ||
|
||
/** | ||
* @param {object} opts | ||
* @param {string} opts.clientKey | ||
* @param {string} opts.authURL | ||
* @param {import('./utils/storage.js').Storage} storage | ||
* @returns {Promise<string>} | ||
*/ | ||
export async function getJWT (opts, storage) { | ||
try { | ||
const jwt = await storage.get(JWT_KEY) | ||
if (isJwtValid(jwt)) return jwt | ||
} catch (e) { | ||
} | ||
|
||
const { clientKey, authURL } = opts | ||
const url = `${authURL}?clientKey=${clientKey}` | ||
|
||
const result = await fetch(url) | ||
const { token, message } = await result.json() | ||
|
||
if (!token) throw new Error(message || 'Failed to refresh jwt') | ||
|
||
try { | ||
await storage.set(JWT_KEY, token) | ||
} catch (e) { | ||
} | ||
|
||
return token | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { isJwtValid } from '#src/utils/jwt.js' | ||
import { describe, it } from 'node:test' | ||
import assert from 'node:assert/strict' | ||
|
||
describe('JWT tests', () => { | ||
it('should validate a jwt', () => { | ||
const fixture = 'eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI2NGQ1ZGI0ZC1jYmQ3LTRkYWMtOWY4Zi01NGQyMjk0OGE3Y2UiLCJzdWIiOiJhYmMxMjMiLCJzdWJUeXBlIjoiY2xpZW50S2V5IiwiYWxsb3dfbGlzdCI6WyIqIl0sImlhdCI6MTY5NjQ3MTQ5MSwiZXhwIjoxNjk2NDc1MDkxfQ.ZJeuzb6JucwUarI7_MlomTjow4Lc4RHZsPhqDepT1q6Pxs5KNVeOQwdZeCDqFSa8QQTiK-VHoKtDH7x349F5QA' | ||
assert.equal(isJwtValid(fixture), false) | ||
}) | ||
}) |