Skip to content

Commit bf178ef

Browse files
fix(examples): use /api/v2/user/login instead of authenticate()
Cloudflare added a managed challenge on /api/auth/v1/session (the endpoint bitgo.authenticate() posts to), which breaks headless script logins. Replace it with a direct call to the unchallenged /api/v2/user/login endpoint followed by bitgo.authenticateWithAccessToken(), handling both the direct access_token response and the legacy ECDH-encrypted token response via bitgo.handleTokenIssuance(). WCN-1331 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 16583d6 commit bf178ef

2 files changed

Lines changed: 58 additions & 10 deletions

File tree

examples/ts/btc/v1/wallet-passphrase-recovery.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
* - Environment (test/prod)
99
* - Activation code
1010
* - Encrypted wallet passphrase from Box D of keycard
11-
*
11+
*
1212
* You need to install node and BitGoJS SDK to run this script.
13-
*
13+
*
1414
* To install node, you can follow the instructions here: https://nodejs.org/en/download
1515
*
1616
* To install BitGoJS SDK, you can use the following command:
@@ -56,6 +56,42 @@ async function main(): Promise<void> {
5656
env: env,
5757
});
5858

59+
// Get login credentials from stdin
60+
const username = await askQuestion('\nEnter your BitGo username: ');
61+
const password = await askQuestion('Enter your BitGo password: ');
62+
const loginOtp = await askQuestion('Enter your OTP code for login: ');
63+
64+
console.log('\nAuthenticating with BitGo (via /api/v2/user/login)...');
65+
66+
// Authenticate with BitGo via /api/v2/user/login (avoids Cloudflare challenge on /api/auth/v1/session)
67+
const loginResponse = await bitgo
68+
.post(bitgo.url('/user/login', 2))
69+
.send({ email: username, password, otp: loginOtp })
70+
.result();
71+
72+
let accessToken: string;
73+
if (loginResponse.access_token) {
74+
accessToken = loginResponse.access_token;
75+
} else if (loginResponse.encryptedToken) {
76+
// Legacy accounts return an ECDH-encrypted token instead of a plain access_token
77+
const { token } = await bitgo.handleTokenIssuance(loginResponse, password);
78+
accessToken = token;
79+
} else {
80+
throw new Error('Login did not return a usable token (no access_token or encryptedToken).');
81+
}
82+
83+
bitgo.authenticateWithAccessToken({ accessToken });
84+
85+
console.log('Authentication successful.');
86+
87+
// Get a fresh OTP for session unlock
88+
const unlockOtp = await askQuestion('\nEnter a new OTP code for session unlock: ');
89+
90+
// Unlock session
91+
console.log('Unlocking session...');
92+
await bitgo.unlock({ otp: unlockOtp });
93+
console.log('Session unlocked successfully.');
94+
5995
// Get activation code
6096
const activationCode = await askQuestion('Enter activation code: ');
6197

examples/ts/btc/v1/wallet-recovery-validation.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,26 @@ async function main(): Promise<void> {
6363
const password = await askQuestion('Enter your BitGo password: ');
6464
const loginOtp = await askQuestion('Enter your OTP code for login: ');
6565

66-
console.log('\nAuthenticating with BitGo...');
66+
console.log('\nAuthenticating with BitGo (via /api/v2/user/login)...');
67+
68+
// Authenticate with BitGo via /api/v2/user/login (avoids Cloudflare challenge on /api/auth/v1/session)
69+
const loginResponse = await bitgo
70+
.post(bitgo.url('/user/login', 2))
71+
.send({ email: username, password, otp: loginOtp })
72+
.result();
73+
74+
let accessToken: string;
75+
if (loginResponse.access_token) {
76+
accessToken = loginResponse.access_token;
77+
} else if (loginResponse.encryptedToken) {
78+
// Legacy accounts return an ECDH-encrypted token instead of a plain access_token
79+
const { token } = await bitgo.handleTokenIssuance(loginResponse, password);
80+
accessToken = token;
81+
} else {
82+
throw new Error('Login did not return a usable token (no access_token or encryptedToken).');
83+
}
6784

68-
// Authenticate with BitGo
69-
await bitgo.authenticate({
70-
username,
71-
password,
72-
otp: loginOtp,
73-
});
85+
bitgo.authenticateWithAccessToken({ accessToken });
7486

7587
console.log('Authentication successful.');
7688

@@ -127,7 +139,7 @@ Recovery information received:
127139
console.log('\nDecrypting wallet password using recovery information...');
128140

129141
// Decrypt the original password
130-
const decryptedPassword = bitgo.decrypt({
142+
const decryptedPassword = await bitgo.decrypt({
131143
password: passcodeEncryptionCode,
132144
input: encryptedPrv,
133145
});

0 commit comments

Comments
 (0)