-
Notifications
You must be signed in to change notification settings - Fork 213
Add support for an authPolicy that returns Permission Denied when failed #1650
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- Add an authPolicy callback to CallableOptions for reusable auth middleware as well as helper auth policies (#1650) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -703,10 +703,11 @@ type v2CallableHandler<Req, Res> = ( | |
) => Res; | ||
|
||
/** @internal **/ | ||
export interface CallableOptions { | ||
export interface CallableOptions<T = any> { | ||
cors: cors.CorsOptions; | ||
enforceAppCheck?: boolean; | ||
consumeAppCheckToken?: boolean; | ||
authPolicy?: (token: AuthData | null, data: T) => boolean | Promise<boolean>; | ||
/** | ||
* Time in seconds between sending heartbeat messages to keep the connection | ||
* alive. Set to `null` to disable heartbeats. | ||
|
@@ -718,7 +719,7 @@ export interface CallableOptions { | |
|
||
/** @internal */ | ||
export function onCallHandler<Req = any, Res = any>( | ||
options: CallableOptions, | ||
options: CallableOptions<Req>, | ||
handler: v1CallableHandler | v2CallableHandler<Req, Res>, | ||
version: "gcfv1" | "gcfv2" | ||
): (req: Request, res: express.Response) => Promise<void> { | ||
|
@@ -739,7 +740,7 @@ function encodeSSE(data: unknown): string { | |
|
||
/** @internal */ | ||
function wrapOnCallHandler<Req = any, Res = any>( | ||
options: CallableOptions, | ||
options: CallableOptions<Req>, | ||
handler: v1CallableHandler | v2CallableHandler<Req, Res>, | ||
version: "gcfv1" | "gcfv2" | ||
): (req: Request, res: express.Response) => Promise<void> { | ||
|
@@ -841,6 +842,14 @@ function wrapOnCallHandler<Req = any, Res = any>( | |
} | ||
|
||
const data: Req = decode(req.body.data); | ||
if (options.authPolicy) { | ||
// Don't ask me why, but Google decided not to disambiguate between unauthenticated and unauthorized | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this true? we have permission-denied and unauthenticated: https://github.com/grpc/grpc/blob/master/doc/statuscodes.md There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wrote this comment before I realized better. Will delete the comment. |
||
// in GRPC status codes, despite the pedantry to disambiguate the two in architecture design. | ||
const authorized = await options.authPolicy(context.auth ?? null, data); | ||
if (!authorized) { | ||
throw new HttpsError("permission-denied", "Permission Denied"); | ||
} | ||
} | ||
let result: Res; | ||
if (version === "gcfv1") { | ||
result = await (handler as v1CallableHandler)(data, context); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
was checking in package-lock intentional? I don't see a change in package.json?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. I couldn't successfully compile until I ran
npm install
and that changedpackage-lock.json