Skip to content

Commit 52445e4

Browse files
authored
Merge pull request #65 from getlarge/64-feat-enable-passing-promises-to-resolvers
feat: enable passing promises to resolvers
2 parents 8687991 + f1b7d85 commit 52445e4

File tree

3 files changed

+69
-27
lines changed

3 files changed

+69
-27
lines changed

packages/hydra-client-wrapper/src/lib/ory-oauth2-authentication.guard.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,28 @@ export interface IOryOAuth2AuthenticationGuard {
1616
}
1717

1818
export interface OryOAuth2AuthenticationGuardOptions {
19-
scopeResolver: (ctx: ExecutionContext) => string;
20-
accessTokenResolver: (ctx: ExecutionContext) => string;
21-
isValidToken: (token: IntrospectedOAuth2Token) => boolean;
19+
scopeResolver: (
20+
this: IOryOAuth2AuthenticationGuard,
21+
ctx: ExecutionContext
22+
) => string | Promise<string>;
23+
accessTokenResolver: (
24+
this: IOryOAuth2AuthenticationGuard,
25+
ctx: ExecutionContext
26+
) => string | Promise<string>;
27+
isValidToken: (
28+
this: IOryOAuth2AuthenticationGuard,
29+
token: IntrospectedOAuth2Token
30+
) => boolean;
2231
postValidationHook?: (
2332
this: IOryOAuth2AuthenticationGuard,
2433
ctx: ExecutionContext,
2534
token: IntrospectedOAuth2Token
2635
) => void | Promise<void>;
27-
unauthorizedFactory: (ctx: ExecutionContext, error: unknown) => Error;
36+
unauthorizedFactory: (
37+
this: IOryOAuth2AuthenticationGuard,
38+
ctx: ExecutionContext,
39+
error: unknown
40+
) => Error;
2841
}
2942

3043
const defaultOptions: OryOAuth2AuthenticationGuardOptions = {
@@ -59,10 +72,13 @@ export const OryOAuth2AuthenticationGuard = (
5972
...options,
6073
};
6174

62-
const scope = scopeResolver(context);
63-
const token = accessTokenResolver(context);
75+
const scope = await scopeResolver.bind(this)(context);
76+
const token = await accessTokenResolver.bind(this)(context);
6477
if (!token) {
65-
throw unauthorizedFactory(context, new Error('No token provided'));
78+
throw unauthorizedFactory.bind(this)(
79+
context,
80+
new Error('No token provided')
81+
);
6682
}
6783

6884
let decodedToken: IntrospectedOAuth2Token;
@@ -73,10 +89,13 @@ export const OryOAuth2AuthenticationGuard = (
7389
});
7490
decodedToken = data;
7591
} catch (error) {
76-
throw unauthorizedFactory(context, error);
92+
throw unauthorizedFactory.bind(this)(context, error);
7793
}
78-
if (!isValidToken(decodedToken)) {
79-
throw unauthorizedFactory(context, new Error('Invalid token'));
94+
if (!isValidToken.bind(this)(decodedToken)) {
95+
throw unauthorizedFactory.bind(this)(
96+
context,
97+
new Error('Invalid token')
98+
);
8099
}
81100
if (typeof postValidationHook === 'function') {
82101
await postValidationHook.bind(this)(context, decodedToken);

packages/keto-client-wrapper/src/lib/ory-authorization.guard.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,16 @@ import {
2020
import { OryPermissionsService } from './ory-permissions';
2121

2222
export interface OryAuthorizationGuardOptions {
23-
errorFactory?: (error: Error) => Error;
24-
postCheck?: (relationTuple: string | string[], isPermitted: boolean) => void;
25-
unauthorizedFactory: (ctx: ExecutionContext, error: unknown) => Error;
23+
postCheck?: (
24+
this: IAuthorizationGuard,
25+
relationTuple: string | string[],
26+
isPermitted: boolean
27+
) => void;
28+
unauthorizedFactory: (
29+
this: IAuthorizationGuard,
30+
ctx: ExecutionContext,
31+
error: unknown
32+
) => Error;
2633
}
2734

2835
export abstract class IAuthorizationGuard implements CanActivate {
@@ -80,14 +87,14 @@ export const OryAuthorizationGuard = (
8087
);
8188

8289
if (result.hasError()) {
83-
throw unauthorizedFactory(context, result.error);
90+
throw unauthorizedFactory.bind(this)(context, result.error);
8491
}
8592

8693
try {
8794
const { data } = await this.oryService.checkPermission(result.value);
8895
return { allowed: data.allowed, relationTuple };
8996
} catch (error) {
90-
throw unauthorizedFactory(context, error);
97+
throw unauthorizedFactory.bind(this)(context, error);
9198
}
9299
}
93100
const evaluatedConditions = await Promise.all(
@@ -119,10 +126,10 @@ export const OryAuthorizationGuard = (
119126
);
120127

121128
if (postCheck) {
122-
postCheck(relationTuple, allowed);
129+
postCheck.bind(this)(relationTuple, allowed);
123130
}
124131
if (!allowed) {
125-
throw unauthorizedFactory(
132+
throw unauthorizedFactory.bind(this)(
126133
context,
127134
new Error(`Unauthorized access for ${relationTuple}`)
128135
);

packages/kratos-client-wrapper/src/lib/ory-authentication.guard.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,25 @@ export interface IOryAuthenticationGuard {
1616
}
1717

1818
export interface OryAuthenticationGuardOptions {
19-
cookieResolver: (ctx: ExecutionContext) => string;
20-
isValidSession: (session: Session) => boolean;
21-
sessionTokenResolver: (ctx: ExecutionContext) => string;
19+
cookieResolver: (
20+
this: IOryAuthenticationGuard,
21+
ctx: ExecutionContext
22+
) => string | Promise<string>;
23+
isValidSession: (this: IOryAuthenticationGuard, session: Session) => boolean;
24+
sessionTokenResolver: (
25+
this: IOryAuthenticationGuard,
26+
ctx: ExecutionContext
27+
) => string | Promise<string>;
2228
postValidationHook?: (
2329
this: IOryAuthenticationGuard,
2430
ctx: ExecutionContext,
2531
session: Session
2632
) => void | Promise<void>;
27-
unauthorizedFactory: (ctx: ExecutionContext, error: unknown) => Error;
33+
unauthorizedFactory: (
34+
this: IOryAuthenticationGuard,
35+
ctx: ExecutionContext,
36+
error: unknown
37+
) => Error;
2838
}
2939

3040
const defaultOptions: OryAuthenticationGuardOptions = {
@@ -61,10 +71,13 @@ export const OryAuthenticationGuard = (
6171
...options,
6272
};
6373

64-
const cookie = cookieResolver(context);
65-
const xSessionToken = sessionTokenResolver(context);
74+
const cookie = await cookieResolver.bind(this)(context);
75+
const xSessionToken = await sessionTokenResolver.bind(this)(context);
6676
if (!cookie && !xSessionToken) {
67-
throw unauthorizedFactory(context, new Error('No session token'));
77+
throw unauthorizedFactory.bind(this)(
78+
context,
79+
new Error('No session token')
80+
);
6881
}
6982
let session: Session;
7083
try {
@@ -74,11 +87,14 @@ export const OryAuthenticationGuard = (
7487
});
7588
session = data;
7689
} catch (error) {
77-
throw unauthorizedFactory(context, error);
90+
throw unauthorizedFactory.bind(this)(context, error);
7891
}
7992

80-
if (!isValidSession(session)) {
81-
throw unauthorizedFactory(context, new Error('Invalid session'));
93+
if (!isValidSession.bind(this)(session)) {
94+
throw unauthorizedFactory.bind(this)(
95+
context,
96+
new Error('Invalid session')
97+
);
8298
}
8399
if (typeof postValidationHook === 'function') {
84100
await postValidationHook.bind(this)(context, session);

0 commit comments

Comments
 (0)