Skip to content
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

Add information to JWT about enabled MFA's, allowing to enforce aal2 only if 2FA is enabled on user's account #1823

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions internal/api/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,15 @@ func (a *API) generateAccessToken(r *http.Request, tx *storage.Connection, user
issuedAt := time.Now().UTC()
expiresAt := issuedAt.Add(time.Second * time.Duration(config.JWT.Exp)).Unix()

atLeastOneVerifiedFactor := false
for i := 0; i < len(user.Factors); i++ {
Copy link
Contributor

@J0 J0 Nov 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution! There's a user.HasMFAEnabled() function I believe. Can consider using that . Otherwise no objections to this change - I think there might be some internal configuration we use to track claim additions, we will need to update that before merging this.

factor := user.Factors[i]
if factor.IsVerified() {
atLeastOneVerifiedFactor = true
break
}
}

claims := &hooks.AccessTokenClaims{
StandardClaims: jwt.StandardClaims{
Subject: user.ID.String(),
Expand All @@ -324,6 +333,7 @@ func (a *API) generateAccessToken(r *http.Request, tx *storage.Connection, user
Role: user.Role,
SessionId: sid,
AuthenticatorAssuranceLevel: aal.String(),
AtLeastOneVerifiedFactor: atLeastOneVerifiedFactor,
AuthenticationMethodReference: amr,
IsAnonymous: user.IsAnonymous,
}
Expand Down
1 change: 1 addition & 0 deletions internal/hooks/auth_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ type AccessTokenClaims struct {
AppMetaData map[string]interface{} `json:"app_metadata"`
UserMetaData map[string]interface{} `json:"user_metadata"`
Role string `json:"role"`
AtLeastOneVerifiedFactor bool `json:"has_factor,omitempty"`
AuthenticatorAssuranceLevel string `json:"aal,omitempty"`
AuthenticationMethodReference []models.AMREntry `json:"amr,omitempty"`
SessionId string `json:"session_id,omitempty"`
Expand Down
Loading