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 Nonce to OIDC ID Token #16

Merged
merged 1 commit into from
Mar 19, 2024
Merged
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
4 changes: 2 additions & 2 deletions charts/identity/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: A Helm chart for deploying Unikorn's IdP

type: application

version: v0.1.12
appVersion: v0.1.12
version: v0.1.13
appVersion: v0.1.13

icon: https://raw.githubusercontent.com/unikorn-cloud/assets/main/images/logos/dark-on-light/icon.png
13 changes: 7 additions & 6 deletions pkg/oauth2/federated.go
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ func oidcPicture(email string) string {
}

// oidcIDToken builds an OIDC ID token.
func (a *Authenticator) oidcIDToken(r *http.Request, scope Scope, expiry time.Time, atHash, clientID, email string) (*string, error) {
func (a *Authenticator) oidcIDToken(r *http.Request, scope Scope, expiry time.Time, atHash string, code *Code) (*string, error) {
//nolint:nilnil
if !slices.Contains(scope, "openid") {
return nil, nil
Expand All @@ -843,25 +843,26 @@ func (a *Authenticator) oidcIDToken(r *http.Request, scope Scope, expiry time.Ti
claims := &IDToken{
Claims: jwt.Claims{
Issuer: "https://" + r.Host,
Subject: email,
Subject: code.Subject,
Audience: []string{
clientID,
code.ClientID,
},
Expiry: jwt.NewNumericDate(expiry),
IssuedAt: jwt.NewNumericDate(time.Now()),
},
OIDCClaims: OIDCClaims{
Nonce: code.ClientNonce,
ATHash: atHash,
},
}

// TODO: we should just pass through the federated id_token in the code...
if slices.Contains(scope, "email") {
claims.OIDCClaimsEmail.Email = email
claims.OIDCClaimsEmail.Email = code.Subject
}

if slices.Contains(scope, "profile") {
claims.OIDCClaimsProfile.Picture = oidcPicture(email)
claims.OIDCClaimsProfile.Picture = oidcPicture(code.Subject)
}

idToken, err := a.issuer.EncodeJWT(claims)
Expand Down Expand Up @@ -901,7 +902,7 @@ func (a *Authenticator) Token(w http.ResponseWriter, r *http.Request) (*generate
}

// Handle OIDC.
idToken, err := a.oidcIDToken(r, code.ClientScope, expiry, oidcHash(accessToken), r.Form.Get("client_id"), code.Subject)
idToken, err := a.oidcIDToken(r, code.ClientScope, expiry, oidcHash(accessToken), code)
if err != nil {
return nil, err
}
Expand Down
Loading