🔑 Store tokens in Sqllite, moved queries, other fixes
- Create cache dir if missing - Moved Sqlite queries to queries.ts - Updated dependencies - Added pino-pretty to dev-dependencies - Changed Sqlite to store tokens as separate rows - Removed in-memory storage of tokens - isValidToken function - Throw Exception if refresh token is present but invalid - Fixed fetch query in smn http file
This commit is contained in:
@ -1,19 +1,73 @@
|
||||
import { type Database } from "better-sqlite3"
|
||||
import { type OAuthTokenResponse } from "@/bank/sparebank1.ts"
|
||||
import Database from "better-sqlite3"
|
||||
|
||||
const tokenKey = "sparebank1"
|
||||
import { type OAuthTokenResponse } from "@/bank/sparebank1.ts"
|
||||
import dayjs, { type Dayjs } from "dayjs"
|
||||
|
||||
export type TokenResponse = {
|
||||
key: TokenKey
|
||||
token: string
|
||||
expires_at: Dayjs
|
||||
}
|
||||
|
||||
export type TokenKey = "access-token" | "refresh-token"
|
||||
|
||||
export function createDb(filename: string) {
|
||||
const db = new Database(filename)
|
||||
db.pragma("journal_mode = WAL")
|
||||
db.exec(
|
||||
"CREATE TABLE IF NOT EXISTS tokens ('key' VARCHAR PRIMARY KEY, token VARCHAR NOT NULL, expires_at DATETIME NOT NULL)",
|
||||
)
|
||||
return db
|
||||
}
|
||||
|
||||
export function insertTokens(
|
||||
db: Database,
|
||||
db: Database.Database,
|
||||
oAuthToken: OAuthTokenResponse,
|
||||
): void {
|
||||
db.prepare("INSERT INTO tokens VALUES (?, ?)").run(tokenKey, oAuthToken)
|
||||
}
|
||||
|
||||
export function fetchTokens(db: Database): OAuthTokenResponse | null {
|
||||
return (
|
||||
(db
|
||||
.prepare("SELECT data FROM tokens WHERE key = ?")
|
||||
.get(tokenKey) as OAuthTokenResponse) ?? null
|
||||
insertAccessToken(db, oAuthToken.access_token, oAuthToken.expires_in)
|
||||
insertRefreshToken(
|
||||
db,
|
||||
oAuthToken.refresh_token,
|
||||
oAuthToken.refresh_token_absolute_expires_in,
|
||||
)
|
||||
}
|
||||
|
||||
function insertAccessToken(
|
||||
db: Database.Database,
|
||||
accessToken: string,
|
||||
expiresIn: number,
|
||||
) {
|
||||
insert(db, "access-token", accessToken, expiresIn)
|
||||
}
|
||||
|
||||
function insertRefreshToken(
|
||||
db: Database.Database,
|
||||
refreshToken: string,
|
||||
expiresIn: number,
|
||||
) {
|
||||
insert(db, "refresh-token", refreshToken, expiresIn)
|
||||
}
|
||||
|
||||
function insert(
|
||||
db: Database.Database,
|
||||
key: TokenKey,
|
||||
token: string,
|
||||
expiresIn: number,
|
||||
) {
|
||||
db.prepare("INSERT OR REPLACE INTO tokens VALUES (?, ?, ?)").run(
|
||||
key,
|
||||
token,
|
||||
dayjs().add(expiresIn, "seconds"),
|
||||
)
|
||||
}
|
||||
|
||||
export function fetchToken(
|
||||
db: Database.Database,
|
||||
tokenKey: TokenKey,
|
||||
): TokenResponse | null {
|
||||
return (
|
||||
(db
|
||||
.prepare("SELECT * FROM tokens WHERE 'key' = ?")
|
||||
.get(tokenKey) as TokenResponse) ?? null
|
||||
)
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ import { BANK_INITIAL_REFRESH_TOKEN } from "@/../config.ts"
|
||||
import logger from "@/logger.ts"
|
||||
import dayjs from "dayjs"
|
||||
import { Database } from "better-sqlite3"
|
||||
import { insertTokens } from "@/bank/db/queries.ts"
|
||||
import { fetchToken, insertTokens, TokenResponse } from "@/bank/db/queries.ts"
|
||||
import * as Api from "./sparebank1Api.ts"
|
||||
|
||||
export interface OAuthTokenResponse {
|
||||
@ -15,16 +15,6 @@ export interface OAuthTokenResponse {
|
||||
refresh_token: string
|
||||
}
|
||||
|
||||
interface AccessToken {
|
||||
access_token: string
|
||||
expires_in: number
|
||||
}
|
||||
|
||||
interface RefreshToken {
|
||||
refresh_token: string
|
||||
expires_in: number
|
||||
}
|
||||
|
||||
export interface Transaction {
|
||||
id: string
|
||||
date: string
|
||||
@ -46,47 +36,39 @@ export interface Sparebank1 {
|
||||
|
||||
export class Sparebank1Impl implements Sparebank1 {
|
||||
private static baseUrl = "https://api.sparebank1.no"
|
||||
private _accessToken: AccessToken | undefined
|
||||
private _refreshToken: RefreshToken | undefined
|
||||
private readonly db: Database
|
||||
|
||||
constructor(db: Database) {
|
||||
this.db = db
|
||||
}
|
||||
|
||||
private set accessToken(accessToken: AccessToken) {
|
||||
this._accessToken = accessToken
|
||||
}
|
||||
|
||||
private set refreshToken(refreshToken: RefreshToken) {
|
||||
this._refreshToken = refreshToken
|
||||
}
|
||||
|
||||
private async getAccessToken(): Promise<string> {
|
||||
const accessToken = this._accessToken
|
||||
if (!accessToken) {
|
||||
const response = await this.fetchNewRefreshToken()
|
||||
return response.access_token
|
||||
const accessToken = fetchToken(this.db, "access-token")
|
||||
|
||||
if (accessToken && this.isValidToken(accessToken)) {
|
||||
return accessToken.token
|
||||
}
|
||||
return accessToken.access_token
|
||||
const response = await this.fetchNewTokens()
|
||||
return response.access_token
|
||||
}
|
||||
|
||||
private isValidToken(tokenResponse: TokenResponse): boolean {
|
||||
return dayjs() < tokenResponse.expires_at
|
||||
}
|
||||
|
||||
private async getRefreshToken(): Promise<string> {
|
||||
const refreshToken = this._refreshToken
|
||||
// TODO check if valid, use jsonwebtoken npm library?
|
||||
const isValid = true
|
||||
if (!refreshToken) {
|
||||
const tokenResponse = fetchToken(this.db, "refresh-token")
|
||||
if (!tokenResponse) {
|
||||
return BANK_INITIAL_REFRESH_TOKEN
|
||||
} else if (isValid) {
|
||||
return refreshToken.refresh_token
|
||||
} else {
|
||||
const response = await this.fetchNewRefreshToken()
|
||||
return response.refresh_token
|
||||
} else if (this.isValidToken(tokenResponse)) {
|
||||
return tokenResponse.token
|
||||
}
|
||||
// TODO clear database, if refresh token is invalid, will cause Exceptions on each call
|
||||
throw new Error("Refresh token is expired. Create a new one")
|
||||
}
|
||||
|
||||
async fetchNewRefreshToken(): Promise<OAuthTokenResponse> {
|
||||
const refreshToken: string = await this.getRefreshToken()
|
||||
async fetchNewTokens(): Promise<OAuthTokenResponse> {
|
||||
const refreshToken = await this.getRefreshToken()
|
||||
const result = await Api.refreshToken(refreshToken)
|
||||
|
||||
if (result.status === "failure") {
|
||||
@ -95,15 +77,6 @@ export class Sparebank1Impl implements Sparebank1 {
|
||||
const oAuthToken = result.data
|
||||
|
||||
insertTokens(this.db, oAuthToken)
|
||||
|
||||
this.accessToken = {
|
||||
access_token: oAuthToken.access_token,
|
||||
expires_in: oAuthToken.expires_in,
|
||||
}
|
||||
this.refreshToken = {
|
||||
refresh_token: oAuthToken.refresh_token,
|
||||
expires_in: oAuthToken.refresh_token_expires_in,
|
||||
}
|
||||
return oAuthToken
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user