Skip to content

Commit

Permalink
Expose roles and username claims via js_var from auth-access
Browse files Browse the repository at this point in the history
  • Loading branch information
jirutka committed Oct 2, 2023
1 parent 63bddcd commit 00cee68
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 4 additions & 0 deletions conf/auth-access.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# These variables are set by the auth-access handler.
js_var $oidc_jwt_claim_roles;
js_var $oidc_jwt_claim_username;

auth_request /-/internal/auth-access;
auth_request_set $auth_cookie $sent_http_set_cookie;
auth_request_set $auth_error $sent_http_x_error;
Expand Down
20 changes: 18 additions & 2 deletions src/handlers/auth-access.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { RequestHandler } from '..'
import type { Context, RequestHandler } from '..'
import { authorizeAccess, isAnonymousAllowed } from '../access'
import { Cookie, Session } from '../constants'
import { decodeAndValidateIdToken } from '../jwt'
import { IdToken, decodeAndValidateIdToken } from '../jwt'
import { refreshTokens } from '../oauth'


Expand All @@ -18,6 +18,7 @@ export const auth_access: RequestHandler = async (ctx) => {
vars[Session.IdToken] = undefined
})
if (idToken) {
exposeClaims(ctx, idToken)
return authorizeAccess(ctx, idToken, conf)
}
}
Expand All @@ -27,6 +28,7 @@ export const auth_access: RequestHandler = async (ctx) => {
log.info?.(`authorize: refreshing token for user ${getCookie(Cookie.Username)}`)
const { idToken } = await refreshTokens(ctx, refreshToken)

exposeClaims(ctx, idToken)
return authorizeAccess(ctx, idToken, conf)
}

Expand All @@ -41,3 +43,17 @@ export const auth_access: RequestHandler = async (ctx) => {
})
}
}

function exposeClaims ({ vars }: Context, idToken: IdToken): void {
// The following variables must be initialised using `js_var` to be set. If
// the variable is not initialised at all, the if condition is false.
if ('oidc_jwt_claims' in vars) {
vars.oidc_jwt_claims = JSON.stringify(idToken)
}
if ('oidc_jwt_claim_roles' in vars) {
vars.oidc_jwt_claim_roles = idToken.roles.join(' ')
}
if ('oidc_jwt_claim_username' in vars) {
vars.oidc_jwt_claim_username = idToken.username
}
}

0 comments on commit 00cee68

Please sign in to comment.