Skip to content

Commit 8f1fd3b

Browse files
committed
remove ternary
1 parent e0490ed commit 8f1fd3b

1 file changed

Lines changed: 33 additions & 11 deletions

File tree

internal-packages/rbac/src/index.ts

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,33 @@ export function withActionAliases(underlying: RbacAbility): RbacAbility {
102102
};
103103
}
104104

105+
function resolveBearerResolution(
106+
result: BearerAuthResult,
107+
context: {
108+
useHostForAdditionalKey: boolean;
109+
useHostForPublicJWT: boolean;
110+
rawToken: string | undefined;
111+
usingPlugin: boolean;
112+
}
113+
): BearerResolution {
114+
if ("resolution" in result) {
115+
return result.resolution as BearerResolution;
116+
}
117+
118+
if (context.useHostForAdditionalKey) {
119+
return { credentialKind: "additional_api_key", lookupPath: "additional" };
120+
}
121+
122+
if (context.useHostForPublicJWT) {
123+
return { credentialKind: "public_jwt", lookupPath: "jwt_current" };
124+
}
125+
126+
return {
127+
credentialKind: context.rawToken?.startsWith("tr_") ? "root_api_key" : "unknown",
128+
lookupPath: context.usingPlugin ? "plugin" : "not_found",
129+
};
130+
}
131+
105132
// Loads the plugin lazily; falls back to the fallback implementation if not installed.
106133
// Synchronous create() avoids top-level await (not supported in the webapp's CJS build).
107134
class LazyController implements RoleBaseAccessController {
@@ -227,17 +254,12 @@ class LazyController implements RoleBaseAccessController {
227254
? await this._hostCredentialResolver.authenticate(...args)
228255
: await controller.authenticateBearer(...args);
229256

230-
const resolution: BearerResolution =
231-
"resolution" in result
232-
? (result.resolution as BearerResolution)
233-
: useHostForAdditionalKey
234-
? { credentialKind: "additional_api_key", lookupPath: "additional" }
235-
: useHostForPublicJWT
236-
? { credentialKind: "public_jwt", lookupPath: "jwt_current" }
237-
: {
238-
credentialKind: rawToken?.startsWith("tr_") ? "root_api_key" : "unknown",
239-
lookupPath: usingPlugin ? "plugin" : "not_found",
240-
};
257+
const resolution = resolveBearerResolution(result, {
258+
useHostForAdditionalKey,
259+
useHostForPublicJWT,
260+
rawToken,
261+
usingPlugin,
262+
});
241263

242264
// The format is only a routing hint. A successful host resolution on the
243265
// additional-key path must still produce the expected principal type.

0 commit comments

Comments
 (0)