fix(oauth): remove OAuth session caching to prevent authentication issues

This commit is contained in:
2025-07-25 00:44:04 +00:00
parent 9011390dc4
commit f790984a95
5 changed files with 34 additions and 213 deletions

View File

@@ -17,9 +17,6 @@ export interface IBunqConstructorOptions {
* the main bunq account
*/
export class BunqAccount {
// Static cache for OAuth token sessions to prevent multiple authentication attempts
private static oauthSessionCache = new Map<string, BunqApiContext>();
public options: IBunqConstructorOptions;
public apiContext: BunqApiContext;
public userId: number;
@@ -35,59 +32,32 @@ export class BunqAccount {
* Initialize the bunq account
*/
public async init() {
// For OAuth tokens, check if we already have a cached session
if (this.options.isOAuthToken) {
const cacheKey = `${this.options.apiKey}_${this.options.environment}`;
const cachedContext = BunqAccount.oauthSessionCache.get(cacheKey);
if (cachedContext && cachedContext.hasValidSession()) {
// Reuse existing session
this.apiContext = cachedContext;
console.log('Reusing existing OAuth session from cache');
} else {
// Create new context and cache it
this.apiContext = new BunqApiContext({
apiKey: this.options.apiKey,
environment: this.options.environment,
deviceDescription: this.options.deviceName,
permittedIps: this.options.permittedIps,
isOAuthToken: this.options.isOAuthToken
});
try {
await this.apiContext.init();
// Cache the successfully initialized context
BunqAccount.oauthSessionCache.set(cacheKey, this.apiContext);
} catch (error) {
// Handle "Superfluous authentication" or "Authentication token already has a user session" errors
if (error instanceof BunqApiError) {
const errorMessages = error.errors.map(e => e.error_description).join(' ');
if (errorMessages.includes('Superfluous authentication') ||
errorMessages.includes('Authentication token already has a user session')) {
console.log('OAuth token already has installation/device, attempting to create new session...');
// Try to create a new session with existing installation/device
await this.apiContext.initWithExistingInstallation();
// Cache the context with new session
BunqAccount.oauthSessionCache.set(cacheKey, this.apiContext);
} else {
throw error;
}
} else {
throw error;
}
}
}
} else {
// Regular API key flow
this.apiContext = new BunqApiContext({
apiKey: this.options.apiKey,
environment: this.options.environment,
deviceDescription: this.options.deviceName,
permittedIps: this.options.permittedIps,
isOAuthToken: this.options.isOAuthToken
});
// Create API context for both OAuth tokens and regular API keys
this.apiContext = new BunqApiContext({
apiKey: this.options.apiKey,
environment: this.options.environment,
deviceDescription: this.options.deviceName,
permittedIps: this.options.permittedIps,
isOAuthToken: this.options.isOAuthToken
});
try {
await this.apiContext.init();
} catch (error) {
// Handle "Superfluous authentication" or "Authentication token already has a user session" errors
if (error instanceof BunqApiError && this.options.isOAuthToken) {
const errorMessages = error.errors.map(e => e.error_description).join(' ');
if (errorMessages.includes('Superfluous authentication') ||
errorMessages.includes('Authentication token already has a user session')) {
console.log('OAuth token already has installation/device, attempting to create new session...');
// Try to create a new session with existing installation/device
await this.apiContext.initWithExistingInstallation();
} else {
throw error;
}
} else {
throw error;
}
}
// Create user instance
@@ -207,28 +177,4 @@ export class BunqAccount {
this.apiContext = null;
}
}
/**
* Clear the OAuth session cache
*/
public static clearOAuthCache(): void {
BunqAccount.oauthSessionCache.clear();
console.log('OAuth session cache cleared');
}
/**
* Clear a specific OAuth token from the cache
*/
public static clearOAuthCacheForToken(apiKey: string, environment: 'SANDBOX' | 'PRODUCTION'): void {
const cacheKey = `${apiKey}_${environment}`;
BunqAccount.oauthSessionCache.delete(cacheKey);
console.log(`OAuth session cache cleared for token in ${environment} environment`);
}
/**
* Get the current size of the OAuth cache
*/
public static getOAuthCacheSize(): number {
return BunqAccount.oauthSessionCache.size;
}
}