Files
renovate/lib/util/http/jwt.spec.ts
Jonathan Hill 0f06e635df feat(platform-azure): initial bearer token auth support for Azure DevOps (#43888)
* feat(platform/azure): support bearer token auth (auto-detect token type)

* refactor(platform/azure): use isPlainObject for JWT header check

Replace manual typeof/null check with isPlainObject() from
@sindresorhus/is in isProbablyJwt(). Remove unused _GlobalConfig
import from test file.

* refactor(platform/azure): use parseJsonc to parse JWT header

* test(platform/azure): use a real JWT in the set bearer token/endpoint test

* feat(platform/azure): log detected token type

* refactor(platform/azure): guard token before JWT check; remove outdated todo comment

* docs(platform/azure): restore TODO note about possibly-undefined token (#22198)

* refactor(platform/azure): move isProbablyJwt() and tests to new files lib/util/http/jwt.ts and lib/util/http/jwt.spec.ts

* refactor(platform/azure): use Json.safeParse() instead of parseJsonc() in isProbablyJwt()

* refactor(platform/azure): accept undefined token in isProbablyJwt and add guard to drop non-null assertions

* test(platform/azure): extract shared buildTestJwt helper and dedupe

* docs(platform/azure): reword the Entra ID token instructions

* chore(platform/azure): remove irrelevant lines from docstring

* refactor(platform/azure): export buildTestJwt() from new test/jwt-util.ts

* docs(platform/azure): use 1 for numbered lists

* refactor(test): extract arrow function from buildTestJwt

---------

Co-authored-by: Bastian Wenske <bastianwenske@outlook.de>
2026-07-19 23:01:52 +00:00

62 lines
2.0 KiB
TypeScript

import { buildTestJwt } from '~test/jwt-util.ts';
import { isProbablyJwt } from './jwt.ts';
describe('util/http/jwt', () => {
describe('isProbablyJwt', () => {
it('returns true for a valid JWT with typ and alg', () => {
const token = buildTestJwt(
{ typ: 'JWT', alg: 'RS256' },
{ aud: '499b84ac', sub: 'test' },
'fake-sig',
);
expect(isProbablyJwt(token)).toBeTrue();
});
it('returns true for a JWT with only alg in header', () => {
const token = buildTestJwt({ alg: 'HS256' }, { sub: 'x' }, 'sig');
expect(isProbablyJwt(token)).toBeTrue();
});
it('returns false for a 52-char PAT', () => {
expect(isProbablyJwt('a'.repeat(52))).toBeFalse();
});
it('returns false for an empty string', () => {
expect(isProbablyJwt('')).toBeFalse();
});
it('returns false for invalid base64 segments', () => {
expect(isProbablyJwt('not.valid.base64!!!')).toBeFalse();
});
it('returns false when header JSON has no typ or alg', () => {
const token = buildTestJwt({ foo: 'bar' }, { sub: 'x' }, 'sig');
expect(isProbablyJwt(token)).toBeFalse();
});
it('returns false for two segments', () => {
const seg = Buffer.from(
JSON.stringify({ typ: 'JWT', alg: 'RS256' }),
).toString('base64url');
expect(isProbablyJwt(`${seg}.${seg}`)).toBeFalse();
});
it('returns false for four segments', () => {
const seg = Buffer.from(
JSON.stringify({ typ: 'JWT', alg: 'RS256' }),
).toString('base64url');
expect(isProbablyJwt(`${seg}.${seg}.${seg}.${seg}`)).toBeFalse();
});
it('returns false when header decodes to a non-object', () => {
const token = buildTestJwt('just a string', { sub: 'x' }, 'sig');
expect(isProbablyJwt(token)).toBeFalse();
});
it('returns false when header decodes to null', () => {
const token = buildTestJwt(null, { sub: 'x' }, 'sig');
expect(isProbablyJwt(token)).toBeFalse();
});
});
});