mirror of
https://github.com/renovatebot/renovate.git
synced 2026-10-25 06:51:11 +00:00
0f06e635df
* 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>
30 lines
765 B
TypeScript
30 lines
765 B
TypeScript
import { isPlainObject } from '@sindresorhus/is';
|
|
import { Json } from '../schema-utils/index.ts';
|
|
|
|
/**
|
|
* Detects whether a token string is likely a JWT (JSON Web Token).
|
|
*
|
|
* JWTs consist of three base64url-encoded segments separated by dots.
|
|
* The first segment (header) must decode to valid JSON containing
|
|
* at least a `typ` or `alg` field.
|
|
*/
|
|
export function isProbablyJwt(token: string | undefined): boolean {
|
|
if (!token) {
|
|
return false;
|
|
}
|
|
|
|
const parts = token.split('.');
|
|
if (parts.length !== 3) {
|
|
return false;
|
|
}
|
|
|
|
const result = Json.safeParse(
|
|
Buffer.from(parts[0], 'base64url').toString('utf8'),
|
|
);
|
|
return (
|
|
result.success &&
|
|
isPlainObject(result.data) &&
|
|
('typ' in result.data || 'alg' in result.data)
|
|
);
|
|
}
|