feat(oauth): add OAuth token support

This commit is contained in:
2025-07-22 21:10:41 +00:00
parent 4b398b56da
commit cffba39844
7 changed files with 117 additions and 2 deletions

View File

@@ -9,6 +9,7 @@ export interface IBunqConstructorOptions {
apiKey: string;
environment: 'SANDBOX' | 'PRODUCTION';
permittedIps?: string[];
isOAuthToken?: boolean; // Set to true when using OAuth access token instead of API key
}
/**
@@ -35,7 +36,8 @@ export class BunqAccount {
apiKey: this.options.apiKey,
environment: this.options.environment,
deviceDescription: this.options.deviceName,
permittedIps: this.options.permittedIps
permittedIps: this.options.permittedIps,
isOAuthToken: this.options.isOAuthToken
});
// Initialize API context (handles installation, device registration, session)

View File

@@ -9,6 +9,7 @@ export interface IBunqApiContextOptions {
environment: 'SANDBOX' | 'PRODUCTION';
deviceDescription: string;
permittedIps?: string[];
isOAuthToken?: boolean;
}
export class BunqApiContext {
@@ -43,6 +44,15 @@ export class BunqApiContext {
* Initialize the API context (installation, device, session)
*/
public async init(): Promise<void> {
// If using OAuth token, skip session creation
if (this.options.isOAuthToken) {
// OAuth tokens already have an associated session
this.context.sessionToken = this.options.apiKey;
this.session = new BunqSession(this.crypto, this.context);
this.session.setOAuthMode(true);
return;
}
// Try to load existing context
const existingContext = await this.loadContext();
@@ -125,6 +135,11 @@ export class BunqApiContext {
* Refresh session if needed
*/
public async ensureValidSession(): Promise<void> {
// OAuth tokens don't need session refresh
if (this.options.isOAuthToken) {
return;
}
await this.session.refreshSession();
await this.saveContext();
}

View File

@@ -13,6 +13,7 @@ export class BunqSession {
private crypto: BunqCrypto;
private context: IBunqApiContext;
private sessionExpiryTime: plugins.smarttime.TimeStamp;
private isOAuthMode: boolean = false;
constructor(crypto: BunqCrypto, context: IBunqApiContext) {
this.crypto = crypto;
@@ -139,10 +140,27 @@ export class BunqSession {
this.sessionExpiryTime = plugins.smarttime.TimeStamp.fromMilliSeconds(Date.now() + 600000);
}
/**
* Set OAuth mode
*/
public setOAuthMode(isOAuth: boolean): void {
this.isOAuthMode = isOAuth;
if (isOAuth) {
// OAuth tokens don't expire in the same way as regular sessions
// Set a far future expiry time
this.sessionExpiryTime = plugins.smarttime.TimeStamp.fromMilliSeconds(Date.now() + 365 * 24 * 60 * 60 * 1000);
}
}
/**
* Check if session is still valid
*/
public isSessionValid(): boolean {
// OAuth tokens are always considered valid (they have their own expiry mechanism)
if (this.isOAuthMode) {
return true;
}
if (!this.sessionExpiryTime) {
return false;
}
@@ -155,6 +173,11 @@ export class BunqSession {
* Refresh the session if needed
*/
public async refreshSession(): Promise<void> {
// OAuth tokens don't need session refresh
if (this.isOAuthMode) {
return;
}
if (!this.isSessionValid()) {
await this.createSession();
}