Skip to content

Commit 507d8f1

Browse files
committed
Use buffer.toString directly on the provided string to address PR feedback.
1 parent b2e7e41 commit 507d8f1

2 files changed

Lines changed: 21 additions & 5 deletions

File tree

core/packages/google-auth-library-nodejs/src/auth/gdchclient.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,6 @@ export class GdchClient extends OAuth2Client {
297297

298298
private base64UrlEncode(str: string | Buffer): string {
299299
const buffer = typeof str === 'string' ? Buffer.from(str) : str;
300-
return buffer
301-
.toString('base64')
302-
.replace(/=/g, '')
303-
.replace(/\+/g, '-')
304-
.replace(/\//g, '_');
300+
return buffer.toString('base64url');
305301
}
306302
}

core/packages/google-auth-library-nodejs/test/test.gdchclient.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,4 +484,24 @@ describe('GdchClient', () => {
484484
});
485485
scope.done();
486486
});
487+
488+
describe('base64UrlEncode', () => {
489+
it('should correctly encode strings and buffers in base64url format', () => {
490+
const client = new GdchClient();
491+
const testCases = [
492+
{input: 'hello world', expected: 'aGVsbG8gd29ybGQ'},
493+
{input: 'foo bar baz', expected: 'Zm9vIGJhciBiYXo'},
494+
{input: 'this is a test', expected: 'dGhpcyBpcyBhIHRlc3Q'},
495+
{input: 'n>?', expected: 'bj4_'},
496+
{input: 'n>~', expected: 'bj5-'},
497+
];
498+
499+
for (const tc of testCases) {
500+
const stringResult = (client as any).base64UrlEncode(tc.input);
501+
const bufferResult = (client as any).base64UrlEncode(Buffer.from(tc.input));
502+
assert.strictEqual(stringResult, tc.expected);
503+
assert.strictEqual(bufferResult, tc.expected);
504+
}
505+
});
506+
});
487507
});

0 commit comments

Comments
 (0)