fix(oauth): fix OAuth token authentication flow for existing installations
This commit is contained in:
@@ -1,5 +1,14 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 2025-07-22 - 3.1.1 - fix(oauth)
|
||||||
|
Fix OAuth token authentication flow for existing installations
|
||||||
|
|
||||||
|
- Fixed initWithExistingInstallation to properly create new sessions with existing installation/device
|
||||||
|
- OAuth tokens now correctly skip installation/device steps when they already exist
|
||||||
|
- Session creation still uses OAuth token as the secret parameter
|
||||||
|
- Properly handles "Superfluous authentication" errors by reusing existing installation
|
||||||
|
- Renamed initWithExistingSession to initWithExistingInstallation for clarity
|
||||||
|
|
||||||
## 2025-07-22 - 3.1.0 - feat(oauth)
|
## 2025-07-22 - 3.1.0 - feat(oauth)
|
||||||
Add OAuth session caching to prevent multiple authentication attempts
|
Add OAuth session caching to prevent multiple authentication attempts
|
||||||
|
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@apiclient.xyz/bunq",
|
"name": "@apiclient.xyz/bunq",
|
||||||
"version": "3.1.0",
|
"version": "3.1.1",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "A full-featured TypeScript/JavaScript client for the bunq API",
|
"description": "A full-featured TypeScript/JavaScript client for the bunq API",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
|
@@ -64,10 +64,10 @@ export class BunqAccount {
|
|||||||
const errorMessages = error.errors.map(e => e.error_description).join(' ');
|
const errorMessages = error.errors.map(e => e.error_description).join(' ');
|
||||||
if (errorMessages.includes('Superfluous authentication') ||
|
if (errorMessages.includes('Superfluous authentication') ||
|
||||||
errorMessages.includes('Authentication token already has a user session')) {
|
errorMessages.includes('Authentication token already has a user session')) {
|
||||||
console.log('OAuth token already has an active session, attempting to reuse...');
|
console.log('OAuth token already has installation/device, attempting to create new session...');
|
||||||
// Try to use the token directly without creating new session
|
// Try to create a new session with existing installation/device
|
||||||
await this.apiContext.initWithExistingSession();
|
await this.apiContext.initWithExistingInstallation();
|
||||||
// Cache the context with existing session
|
// Cache the context with new session
|
||||||
BunqAccount.oauthSessionCache.set(cacheKey, this.apiContext);
|
BunqAccount.oauthSessionCache.set(cacheKey, this.apiContext);
|
||||||
} else {
|
} else {
|
||||||
throw error;
|
throw error;
|
||||||
|
@@ -171,31 +171,49 @@ export class BunqApiContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize with existing OAuth session (skip installation/device/session creation)
|
* Initialize with existing installation and device (for OAuth tokens that already completed these steps)
|
||||||
*/
|
*/
|
||||||
public async initWithExistingSession(): Promise<void> {
|
public async initWithExistingInstallation(): Promise<void> {
|
||||||
// For OAuth tokens that already have a session, we just need to:
|
// For OAuth tokens that already have installation/device but need a new session
|
||||||
// 1. Use the OAuth token as the session token
|
// We need to:
|
||||||
// 2. Set OAuth mode for proper expiry handling
|
// 1. Try to load existing installation/device info
|
||||||
|
// 2. Create a new session using the OAuth token as the secret
|
||||||
|
|
||||||
this.context.sessionToken = this.options.apiKey;
|
const existingContext = await this.loadContext();
|
||||||
|
|
||||||
// Create session instance with existing token
|
if (existingContext && existingContext.clientPrivateKey && existingContext.clientPublicKey) {
|
||||||
this.session = new BunqSession(this.crypto, this.context);
|
// Restore crypto keys from previous installation
|
||||||
this.session.setOAuthMode(true);
|
this.crypto.setKeys(
|
||||||
|
existingContext.clientPrivateKey,
|
||||||
// Try to get user info to validate the session
|
existingContext.clientPublicKey
|
||||||
try {
|
);
|
||||||
// This will test if the session is valid
|
|
||||||
const testClient = this.session.getHttpClient();
|
|
||||||
const response = await testClient.get('/v1/user');
|
|
||||||
|
|
||||||
if (response && response.Response) {
|
// Update context with existing installation data
|
||||||
console.log('Successfully reused existing OAuth session');
|
this.context = { ...this.context, ...existingContext };
|
||||||
|
|
||||||
|
// Create new session instance
|
||||||
|
this.session = new BunqSession(this.crypto, this.context);
|
||||||
|
|
||||||
|
// Try to create a new session with the OAuth token
|
||||||
|
try {
|
||||||
|
await this.session.init(
|
||||||
|
this.options.deviceDescription,
|
||||||
|
this.options.permittedIps || [],
|
||||||
|
true // skipInstallationAndDevice = true
|
||||||
|
);
|
||||||
|
|
||||||
|
if (this.options.isOAuthToken) {
|
||||||
|
this.session.setOAuthMode(true);
|
||||||
|
}
|
||||||
|
|
||||||
await this.saveContext();
|
await this.saveContext();
|
||||||
|
console.log('Successfully created new session with existing installation');
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error(`Failed to create session with OAuth token: ${error.message}`);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} else {
|
||||||
throw new Error(`Failed to reuse OAuth session: ${error.message}`);
|
// No existing installation, fall back to full init
|
||||||
|
throw new Error('No existing installation found, full initialization required');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -24,14 +24,16 @@ export class BunqSession {
|
|||||||
/**
|
/**
|
||||||
* Initialize a new bunq API session
|
* Initialize a new bunq API session
|
||||||
*/
|
*/
|
||||||
public async init(deviceDescription: string, permittedIps: string[] = []): Promise<void> {
|
public async init(deviceDescription: string, permittedIps: string[] = [], skipInstallationAndDevice: boolean = false): Promise<void> {
|
||||||
// Step 1: Installation
|
if (!skipInstallationAndDevice) {
|
||||||
await this.createInstallation();
|
// Step 1: Installation
|
||||||
|
await this.createInstallation();
|
||||||
|
|
||||||
|
// Step 2: Device registration
|
||||||
|
await this.registerDevice(deviceDescription, permittedIps);
|
||||||
|
}
|
||||||
|
|
||||||
// Step 2: Device registration
|
// Step 3: Session creation (always required)
|
||||||
await this.registerDevice(deviceDescription, permittedIps);
|
|
||||||
|
|
||||||
// Step 3: Session creation
|
|
||||||
await this.createSession();
|
await this.createSession();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user