Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow custom headers in SubIdentityMapper #46

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions __tests__/authorizer/mapper/identity/sub.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,16 @@ describe("SubIdentityMapper", () => {

expect(result).toEqual(identityContext("test", "IDENTITY_TYPE_SUB"));
});

it("returns an identity context from a custom header", async () => {
const req = httpMocks.createRequest({
headers: {
identity: `my-id`,
},
});
const customSubMapper = SubIdentityMapper("identity");
const result = await customSubMapper(req);

expect(result).toEqual(identityContext("my-id", "IDENTITY_TYPE_SUB"));
});
});
4 changes: 2 additions & 2 deletions __tests__/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe("Middleware", () => {
expect(next).toBeCalledWith({
statusCode: 403,
error: "Forbidden",
message: `aserto-node: Forbidden by policy examplePolicy`,
message: `aserto-node: Forbidden by policy examplePolicy.check`,
});
});

Expand Down Expand Up @@ -248,7 +248,7 @@ describe("Middleware", () => {
expect(next).toBeCalledWith({
statusCode: 403,
error: "Forbidden",
message: `aserto-node: Forbidden by policy examplePolicy`,
message: `aserto-node: Forbidden by policy examplePolicy.GET.todos`,
});
});

Expand Down
12 changes: 8 additions & 4 deletions lib/authorizer/mapper/identity/sub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ const SubIdentityMapper = (
return async (req: Request): Promise<IdentityContext> => {
const authHeader = req.header(header);
if (authHeader) {
const token: JwtPayload = jwt_decode(authHeader);
if (token && token.sub) {
return identityContext(token.sub, "IDENTITY_TYPE_SUB");
if (header === "Authorization") {
const token: JwtPayload = jwt_decode(authHeader);
if (token && token.sub) {
return identityContext(token.sub, "IDENTITY_TYPE_SUB");
} else {
throw new Error("Missing token");
}
} else {
throw new Error("Missing token");
return identityContext(authHeader, "IDENTITY_TYPE_SUB");
}
} else {
throw new Error(`Missing ${header} header`);
Expand Down
38 changes: 22 additions & 16 deletions lib/authorizer/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,21 @@ export class Middleware {
resourceContext = { ...resourceContext, ...this.resourceMapper };
}

return this.client.Is({
identityContext: await this.identityContext(req),
policyContext: policyCtx,
policyInstance: this.policyInstance(),
resourceContext: resourceContext,
});
return [
await this.client.Is({
identityContext: await this.identityContext(req),
policyContext: policyCtx,
policyInstance: this.policyInstance(),
resourceContext: resourceContext,
}),
policyCtx.getPath(),
];
};
try {
const allowed = await callAuthorizer();
const [allowed, policyPath] = await callAuthorizer();
return allowed
? next()
: error(res, `Forbidden by policy ${this.policy.root}`);
: error(res, `Forbidden by policy ${policyPath}`);
} catch (err) {
error(res, err as string);
}
Expand All @@ -151,18 +154,21 @@ export class Middleware {
: this.resourceMapper
: ResourceParamsMapper(req);

return this.client.Is({
identityContext: await this.identityContext(req),
policyContext: policyCtx,
policyInstance: this.policyInstance(),
resourceContext: resourceContext,
});
return [
await this.client.Is({
identityContext: await this.identityContext(req),
policyContext: policyCtx,
policyInstance: this.policyInstance(),
resourceContext: resourceContext,
}),
policyCtx.getPath(),
];
};
try {
const allowed = await callAuthorizer();
const [allowed, policyPath] = await callAuthorizer();
return allowed
? next()
: error(res, `Forbidden by policy ${this.policy.root}`);
: error(res, `Forbidden by policy ${policyPath}`);
} catch (err) {
error(res, err as string);
}
Expand Down
Loading