@@ -17,10 +17,29 @@ import { applyTotpProviderController } from "./totp-provider/controller.js";
17
17
18
18
/**
19
19
* @typedef {(
20
- * user: QueryResultAuthUser
21
- * ) => AuthDetermineTwoStepResult} AuthDetermineTwoStepCheckFunction
20
+ * ctx: import("koa").ExtendableContext, user: QueryResultAuthUser
21
+ * ) => Promise<AuthDetermineTwoStepResult>|AuthDetermineTwoStepResult}
22
+ * AuthDetermineTwoStepCheckFunction
22
23
*/
23
24
25
+ /**
26
+ * @type {AuthDetermineTwoStepCheckFunction }
27
+ */
28
+ export const authDefaultDetermineTwoStepFunction = ( ctx , user ) => {
29
+ if ( ! isNil ( user ?. totpSettings ?. verifiedAt ) ) {
30
+ return {
31
+ type : "checkTwoStep" ,
32
+ twoStepType : "totpProvider" ,
33
+ } ;
34
+ } else if ( ! isNil ( user ?. passwordLogin ?. otpEnabledAt ) ) {
35
+ // TODO: what should happen if the user didn't login via passwordLogin?
36
+ return {
37
+ type : "checkTwoStep" ,
38
+ twoStepType : "passwordBasedOtp" ,
39
+ } ;
40
+ }
41
+ } ;
42
+
24
43
/**
25
44
* Apply the auth package with the provided settings.
26
45
* Make sure to call `routerClearMemoizedHandlers` when calling `applyAuth` again on the
@@ -37,24 +56,9 @@ export async function applyAuth({
37
56
keycloakBased,
38
57
passwordBased,
39
58
totpProvider,
59
+ determineTwoStepFunction,
40
60
} ) {
41
- /**
42
- * @type {AuthDetermineTwoStepCheckFunction }
43
- */
44
- const determineTwoStepFunction = ( user ) => {
45
- if ( ! isNil ( user ?. totpSettings ?. verifiedAt ) ) {
46
- return {
47
- type : "checkTwoStep" ,
48
- twoStepType : "totpProvider" ,
49
- } ;
50
- } else if ( ! isNil ( user ?. passwordLogin ?. otpEnabledAt ) ) {
51
- // TODO: what should happen if the user didn't login via passwordLogin?
52
- return {
53
- type : "checkTwoStep" ,
54
- twoStepType : "passwordBasedOtp" ,
55
- } ;
56
- }
57
- } ;
61
+ determineTwoStepFunction ??= authDefaultDetermineTwoStepFunction ;
58
62
59
63
await applyAuthController ( ) ;
60
64
await applySessionController ( { } ) ;
0 commit comments