fix(oauth): fix OAuth token authentication flow for existing installations
This commit is contained in:
@@ -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> {
|
||||
// For OAuth tokens that already have a session, we just need to:
|
||||
// 1. Use the OAuth token as the session token
|
||||
// 2. Set OAuth mode for proper expiry handling
|
||||
public async initWithExistingInstallation(): Promise<void> {
|
||||
// For OAuth tokens that already have installation/device but need a new session
|
||||
// We need to:
|
||||
// 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
|
||||
this.session = new BunqSession(this.crypto, this.context);
|
||||
this.session.setOAuthMode(true);
|
||||
|
||||
// Try to get user info to validate the session
|
||||
try {
|
||||
// This will test if the session is valid
|
||||
const testClient = this.session.getHttpClient();
|
||||
const response = await testClient.get('/v1/user');
|
||||
if (existingContext && existingContext.clientPrivateKey && existingContext.clientPublicKey) {
|
||||
// Restore crypto keys from previous installation
|
||||
this.crypto.setKeys(
|
||||
existingContext.clientPrivateKey,
|
||||
existingContext.clientPublicKey
|
||||
);
|
||||
|
||||
if (response && response.Response) {
|
||||
console.log('Successfully reused existing OAuth session');
|
||||
// Update context with existing installation data
|
||||
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();
|
||||
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) {
|
||||
throw new Error(`Failed to reuse OAuth session: ${error.message}`);
|
||||
} else {
|
||||
// No existing installation, fall back to full init
|
||||
throw new Error('No existing installation found, full initialization required');
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user