Skip to content

Commit

Permalink
style: format
Browse files Browse the repository at this point in the history
  • Loading branch information
Benehiko committed Sep 11, 2023
1 parent f565481 commit e492c92
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 49 deletions.
49 changes: 25 additions & 24 deletions src/pkg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export const getUrlForFlow = (
flow: string,
query?: URLSearchParams,
) =>
`${removeTrailingSlash(base)}/self-service/${flow}/browser${query ? `?${query.toString()}` : ""
`${removeTrailingSlash(base)}/self-service/${flow}/browser${
query ? `?${query.toString()}` : ""
}`

export const defaultConfig: RouteOptionsCreator = () => {
Expand Down Expand Up @@ -54,36 +55,36 @@ const isErrorAuthenticatorAssuranceLevel = (
// or 403 error code.
export const redirectOnSoftError =
(res: Response, next: NextFunction, redirectTo: string) =>
(err: AxiosError) => {
if (!err.response) {
next(err)
return
}
(err: AxiosError) => {
if (!err.response) {
next(err)
return
}

if (
err.response.status === 404 ||
err.response.status === 410 ||
err.response.status === 403
) {
// in some cases Kratos will require us to redirect to a different page when the session_aal2_required
// for example, when recovery redirects us to settings
// but settings requires us to redirect to login?aal=aal2
const authenticatorAssuranceLevelError = err.response.data as unknown
if (
err.response.status === 404 ||
err.response.status === 410 ||
err.response.status === 403
isErrorAuthenticatorAssuranceLevel(authenticatorAssuranceLevelError)
) {
// in some cases Kratos will require us to redirect to a different page when the session_aal2_required
// for example, when recovery redirects us to settings
// but settings requires us to redirect to login?aal=aal2
const authenticatorAssuranceLevelError = err.response.data as unknown
if (
isErrorAuthenticatorAssuranceLevel(authenticatorAssuranceLevelError)
) {
res.redirect(
authenticatorAssuranceLevelError.redirect_browser_to || redirectTo,
)
return
}
res.redirect(`${redirectTo}`)
res.redirect(
authenticatorAssuranceLevelError.redirect_browser_to || redirectTo,
)
return
}

next(err)
res.redirect(`${redirectTo}`)
return
}

next(err)
}

export const handlebarsHelpers: UnknownObject = {
jsonPretty: (context: any) => JSON.stringify(context, null, 2),
onlyNodes: (
Expand Down
27 changes: 13 additions & 14 deletions src/routes/consent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import bodyParser from "body-parser"
import { doubleCsrf } from "csrf-csrf"
import { Request, Response, NextFunction } from "express"


// Sets up csrf protection
const {
generateToken, // Use this in your routes to provide a CSRF hash + token cookie and token.
Expand All @@ -23,21 +22,26 @@ const {
getSecret: () => "VERY_SECRET_VALUE", // A function that optionally takes the request and returns a secret
cookieName: "ax-x-csrf-token", // The name of the cookie to be used, recommend using Host prefix.
cookieOptions: {
sameSite: "lax", // Recommend you make this strict if posible
sameSite: "lax", // Recommend you make this strict if posible
secure: true,
},
ignoredMethods: ["GET", "HEAD", "OPTIONS"], // A list of request methods that will not be protected.
getTokenFromRequest: (req) => req.headers["x-csrf-token"], // A function that returns the token from the request
});
})

// Error handling, validation error interception
const csrfErrorHandler = (error: unknown, req: Request, res: Response, next: NextFunction) => {
const csrfErrorHandler = (
error: unknown,
req: Request,
res: Response,
next: NextFunction,
) => {
if (error == invalidCsrfTokenError) {
next(new Error("csrf validation error"))
} else {
next();
next()
}
};
}

async function createOAuth2ConsentRequestSession(
grantScopes: string[],
Expand Down Expand Up @@ -257,26 +261,21 @@ export const createConsentPostRoute: RouteCreator =
.catch(next)
}



var parseForm = bodyParser.urlencoded({ extended: false })

export const registerConsentRoute: RouteRegistrator = function(
export const registerConsentRoute: RouteRegistrator = function (
app,
createHelpers = defaultConfig,
) {
if (process.env.HYDRA_ADMIN_URL) {
console.log("found HYDRA_ADMIN_URL")
return app.get(
"/consent",
createConsentRoute(createHelpers),
)
return app.get("/consent", createConsentRoute(createHelpers))
} else {
return register404Route
}
}

export const registerConsentPostRoute: RouteRegistrator = function(
export const registerConsentPostRoute: RouteRegistrator = function (
app,
createHelpers = defaultConfig,
) {
Expand Down
24 changes: 13 additions & 11 deletions src/routes/sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export const createSessionsRoute: RouteCreator =
).data.logout_url || ""

const identityCredentialTrait =
session?.identity?.traits.email || session?.identity?.traits.username || ""
session?.identity?.traits.email ||
session?.identity?.traits.username ||
""

const sessionText =
identityCredentialTrait !== ""
Expand All @@ -44,14 +46,13 @@ export const createSessionsRoute: RouteCreator =
id: session?.identity?.id,
// sometimes the identity schema could contain recursive objects
// for this use case we will just stringify the object instead of recursively flatten the object
...Object.entries(session?.identity?.traits).reduce<Record<string, any>>(
(traits, [key, value]) => {
traits[key] =
typeof value === "object" ? JSON.stringify(value) : value
return traits
},
{},
),
...Object.entries(session?.identity?.traits).reduce<
Record<string, any>
>((traits, [key, value]) => {
traits[key] =
typeof value === "object" ? JSON.stringify(value) : value
return traits
}, {}),
"signup date": session?.identity?.created_at || "",
"authentication level":
session?.authenticator_assurance_level === "aal2"
Expand All @@ -71,8 +72,9 @@ export const createSessionsRoute: RouteCreator =
authMethods: session?.authentication_methods?.reduce<any>(
(methods, method, i) => {
methods.push({
[`authentication method used`]: `${method.method} (${method.completed_at && new Date(method.completed_at).toUTCString()
})`,
[`authentication method used`]: `${method.method} (${
method.completed_at && new Date(method.completed_at).toUTCString()
})`,
})
return methods
},
Expand Down

0 comments on commit e492c92

Please sign in to comment.