All files / core/blong-gogo/src oidc.ts

44.6% Statements 157/352
42.1% Branches 8/19
28% Functions 7/25
44.6% Lines 157/352

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 3531x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x         46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x                                                                                                                                                                                                                                                                                       5x 5x 5x 5x 5x 5x 5x             5x                   5x 5x 5x 5x 5x 5x 5x 5x 5x 5x         165x 165x 165x 165x 165x 165x 5x 165x 165x 165x 165x 165x 165x 165x 165x     165x 165x 165x 165x 165x 165x 165x 165x 165x 165x           165x 165x 165x 165x 165x                                     165x 165x 165x 165x 165x     165x 165x 165x 165x 165x 165x 165x 165x 165x 165x 165x 165x 165x 165x 165x 165x 165x 165x 165x 165x 46x 46x 46x             46x 46x 46x 46x 46x 46x  
import type {Errors} from '@feasibleone/blong';
import {createLocalJWKSet, decodeJwt, decodeProtectedHeader, jwtVerify} from 'jose';
import {readFileSync} from 'node:fs';
import {requestGet as get, loginService} from './lib.ts';
 
/**
 * The headers an issuer's own endpoints are read with.
 *
 * A cluster's discovery document and key set are not anonymous: the API server answers 401 unless
 * the caller presents an identity it knows, so an issuer that names its credentials by file is read
 * with them (`rpc.oidcHttp: OpenID returned HTTP error 401` is what an uncredentialed read looks
 * like). The file is read per fetch rather than cached, because what lives in a projected
 * service-account volume is rotated by the kubelet and the process is meant to pick the new one up.
 */
const issuerHeaders = (config: {tokenFile?: string} | undefined): Record<string, string> => {
    if (!config?.tokenFile) return {};
    return {authorization: `Bearer ${readFileSync(config.tokenFile, 'utf8').trim()}`};
};

export default ({
    issuers,
    tls = {},
    discoverService = false,
    session,
    errorPrefix,
    errors: {
        [`${errorPrefix}oidcEmpty`]: errorOidcEmpty,
        [`${errorPrefix}oidcHttp`]: errorOidcHttp,
        [`${errorPrefix}actionEmpty`]: errorActionEmpty,
        [`${errorPrefix}actionHttp`]: errorActionHttp,
        [`${errorPrefix}unauthorized`]: errorUnauthorized,
        [`${errorPrefix}oidcNoIssuer`]: errorNoIssuer,
        [`${errorPrefix}oidcNoKid`]: errorNoKid,
        [`${errorPrefix}oidcBadIssuer`]: errorBadIssuer,
        [`${errorPrefix}jwtInvalid`]: errorInvalid,
    },
}: {
    issuers: Record<
        string,
        {configuration?: string; url?: string; audience?: string; [key: string]: unknown} | false
    >;
    tls?: {
        rejectUnauthorized?: boolean;
        ca?: string | Buffer | Array<string | Buffer>;
        cert?: string | Buffer;
        key?: string | Buffer;
    };
    discoverService?:
        | boolean
        | ((service: string) => Promise<{protocol: string; hostname: string; port: number}>);
    session?: (decoded: object) => Promise<void>;
    errorPrefix: string;
    errors: Errors<{
        [key: string]: unknown;
    }>;
}) => {
    async function openIdConfig(
        issuer: string,
        headers?: Record<string, string | undefined> | undefined,
        protocol?: string,
    ) {
        if (issuer === 'blong-login') {
            const {
                protocol: loginProtocol,
                hostname,
                port,
            } = await loginService(
                discoverService as unknown as (
                    service: string,
                ) => Promise<{protocol: string; hostname: string; port: number}>,
            );
            issuer = `${loginProtocol}://${hostname}:${port}/rpc/login/.well-known/openid-configuration`;
        }
        // The headers are kept for every issuer, not only for `blong-login`. A configured issuer
        // is read with whatever the caller of this function brought, and for a cluster issuer that
        // is the process's own credential: discarding it here turned the API server's answer into
        // `rpc.oidcHttp: 401` for a request that presented nothing, with nothing in the message to
        // say the credential had been dropped on the way (F-494).
        return await get(issuer, errorOidcHttp, errorOidcEmpty, headers, protocol ?? '', tls);
    }

    let actionsCache: Record<string, number[] | number>;
    async function actions(method: string) {
        if (actionsCache) return actionsCache[method];
        const {protocol, hostname, port} = await loginService(
            discoverService as unknown as (
                service: string,
            ) => Promise<{protocol: string; hostname: string; port: number}>,
        );
        const actionsMap = (await get(
            `${protocol}://${hostname}:${port}/rpc/login/action`,
            errorActionHttp,
            errorActionEmpty,
            {},
            protocol,
            tls,
        )) as Record<string, number>;
        const fuzzyMap = Object.entries(actionsMap).reduce(
            (all, [action, bit]) => {
                for (let i = 1, segments = action.split('.'), n = segments.length; i < n; i += 1) {
                    const key = segments.slice(0, i).concat('%').join('.');
                    if (!all[key]) all[key] = [];
                    all[key].push(bit);
                }
                return all;
            },
            {} as Record<string, number[]>,
        );

        actionsCache = {...fuzzyMap, ...actionsMap};

        return actionsCache[method];
    }

    function checkPermission(bit: number, map: number[]) {
        bit -= 1;
        const index = Math.floor(bit / 8);
        return Number.isInteger(index) && index < map.length && map[index] & (1 << (bit % 8));
    }

    async function checkAuthSingle(method: string, map: number[]) {
        if (Array.isArray(method)) {
            for (const m of method) {
                if (!(await checkAuthSingle(m, map))) return false;
            }
            return true;
        }
        const bit = await actions(method);
        return Array.isArray(bit)
            ? bit.some(b => checkPermission(b, map))
            : checkPermission(bit, map);
    }

    async function checkAuth(method: string, map: number[], dontThrow: boolean) {
        if (!(await checkAuthSingle(method, map)) && !(await checkAuthSingle('%', map))) {
            if (dontThrow) return false;
            throw errorUnauthorized({params: {method}});
        }
        return true;
    }

    const issuerUrl = (base: string, url: string) =>
        base === 'blong-login' ? 'blong-login' : new URL(url, base.replace(/\/?$/, '/')).href;

    const loadIssuers = () =>
        Promise.all(
            Object.entries(issuers)
                .filter(
                    (
                        entry,
                    ): entry is [
                        string,
                        {
                            configuration?: string;
                            url?: string;
                            audience?: string;
                            [key: string]: unknown;
                        },
                    ] => !!entry[1],
                )
                .map(
                    ([
                        issuerId,
                        {
                            configuration,
                            url = '.well-known/openid-configuration',
                            audience = 'blong',
                            ...rest
                        },
                    ]) =>
                        (async () => [
                            issuerId,
                            {
                                ...((await openIdConfig(
                                    configuration || issuerUrl(issuerId, url),
                                    issuerHeaders(rest as {tokenFile?: string}),
                                )) as object),
                                audience,
                                issuerId,
                                ...rest,
                            },
                        ])(),
                ),
        );

    async function cache() {
        return (await loadIssuers()).reduce(
            (prev, [issuer, config]) => {
                const key = (config as {issuer?: string}).issuer;
                return {...prev, ...(key ? {[key]: config} : {}), [issuer as string]: config};
            },
            {} as Record<string, unknown>,
        );
    }

    const getIssuers = (headers: object, protocol: string) =>
        Promise.all(
            Object.entries(issuers)
                .filter(
                    (
                        entry,
                    ): entry is [
                        string,
                        {configuration?: string; url?: string; [key: string]: unknown},
                    ] => !!entry[1],
                )
                .map(([issuer, {configuration, url = '.well-known/openid-configuration'}]) =>
                    openIdConfig(
                        configuration || issuerUrl(issuer, url),
                        headers as Record<string, string | undefined>,
                        protocol,
                    ),
                ),
        );

    let issuersCache: Promise<Record<string, unknown>> | null = null;

    async function issuerConfig(issuerId: string): Promise<{
        issuer: string;
        jwks_uri: string;
        audience: string;
        [key: string]: unknown;
    }> {
        if (issuerId === 'blong-login')
            return openIdConfig('blong-login') as unknown as {
                issuer: string;
                jwks_uri: string;
                audience: string;
                [key: string]: unknown;
            };
        issuersCache = issuersCache ?? cache();
        const result = (await issuersCache)[issuerId] as
            | {issuer: string; jwks_uri: string; audience: string; [key: string]: unknown}
            | undefined;
        if (!result) {
            throw errorBadIssuer({params: {issuerId}});
        }
        return result;
    }

    async function jwks(issuerId: string) {
        const config = await issuerConfig(issuerId);
        return get(
            config.jwks_uri,
            errorOidcHttp,
            errorOidcEmpty,
            issuerHeaders(config as {tokenFile?: string}),
            '',
            tls,
        );
    }

    const keys: Record<string, ReturnType<typeof createLocalJWKSet>> = {};

    async function getKey(decoded: {iss?: string}, protectedHeader: {kid?: string}) {
        const issuerId = decoded?.iss;
        if (!issuerId) throw errorNoIssuer();
        const kid = protectedHeader?.kid;
        if (!kid) throw errorNoKid();
        if (!keys[issuerId])
            keys[issuerId] = createLocalJWKSet(
                (await jwks(issuerId)) as Parameters<typeof createLocalJWKSet>[0],
            );
        const result = await keys[issuerId](
            protectedHeader as Parameters<(typeof keys)[string]>[0],
            decoded as Parameters<(typeof keys)[string]>[1],
        );
        if (!result) throw errorInvalid({params: {message: 'Invalid OIDC key id'}});
        return result;
    }

    async function verify(
        token: string,
        {nonce, audience = 'blong'}: {nonce?: string; audience?: string},
        isId: boolean,
    ) {
        let decoded;
        let protectedHeader;
        try {
            decoded = decodeJwt(token);
            protectedHeader = decodeProtectedHeader(token);
        } catch (error) {
            throw errorInvalid({
                params: {message: (error as Error).message},
                cause: error as Error,
            });
        }
        const config = (decoded.iss &&
            decoded.iss !== 'blong-login' &&
            (await issuerConfig(decoded.iss))) || {audience};
        try {
            if (isId) {
                await jwtVerify(
                    token,
                    (await getKey(decoded, protectedHeader)) as unknown as Parameters<
                        typeof jwtVerify
                    >[1],
                    {
                        audience: (config as {audience?: string}).audience ?? audience,
                        issuer: decoded.iss as string,
                        ...(nonce ? {nonce} : {}),
                    } as Parameters<typeof jwtVerify>[2],
                );
            } else {
                await jwtVerify(
                    token,
                    (await getKey(decoded, protectedHeader)) as unknown as Parameters<
                        typeof jwtVerify
                    >[1],
                    {
                        audience: (config as {audience?: string}).audience,
                    },
                );
            }
        } catch (error) {
            throw errorInvalid({params: {message: (error as Error).message}, cause: error});
        }
        /**
         * The session step, for the issuers that ask for one.
         *
         * It is what hands a caller its session — an actor, a session id and the MLE key pair its
         * later calls are decrypted with — by asking the `identity` namespace, and the framework's
         * own issuer asks for it by name. An issuer may ask too (`session: true`), which is how a
         * realm that answers its own login keeps the framework's session contract without running
         * the framework's login realm: it answers `identity.checkInternal` itself. An issuer that
         * asks for nothing gets nothing — a cluster issuer answers for the caller by its signature
         * alone — and asking an `identity` service that does not exist is what made a verified
         * token fail with `ENOTFOUND identity.<namespace>.svc.cluster.local` (F-494).
         */
        const wantsSession =
            (config as {session?: boolean})?.session ?? decoded.iss === 'blong-login';
        if (session && wantsSession && !decoded.ses) await session(decoded);
        return {
            ...decoded,
            config,
        };
    }
 
    return {
        get: (
            url: string,
            errorHttp: (params: Record<string, unknown>) => unknown,
            errorEmpty: () => unknown,
            headers: Record<string, string | undefined> | undefined,
            protocol: string,
        ) => get(url, errorHttp, errorEmpty, headers, protocol, tls),
        verify,
        getIssuers,
        checkAuth,
        issuerConfig,
    };
};