-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace MapClaims with structured ArgoClaims
Signed-off-by: Atif Ali <[email protected]>
- Loading branch information
Showing
13 changed files
with
397 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,48 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/golang-jwt/jwt/v4" | ||
) | ||
|
||
const ( | ||
federatedClaimsKey = "federated_claims" | ||
userIDKey = "user_id" | ||
subKey = "sub" | ||
) | ||
// ArgoClaims defines the claims structure based on Dex's documented claims | ||
type ArgoClaims struct { | ||
jwt.RegisteredClaims | ||
Email string `json:"email,omitempty"` | ||
EmailVerified bool `json:"email_verified,omitempty"` | ||
Name string `json:"name,omitempty"` | ||
Groups []string `json:"groups,omitempty"` | ||
// As per Dex docs, federated_claims has a specific structure | ||
FederatedClaims *FederatedClaims `json:"federated_claims,omitempty"` | ||
} | ||
|
||
// GetUserIdentifier returns a consistent user identifier, checking federated_claims.user_id when Dex is in use | ||
func GetUserIdentifier(claims jwt.MapClaims) string { | ||
if federatedClaims, ok := claims[federatedClaimsKey].(map[string]interface{}); ok { | ||
if userID, exists := federatedClaims[userIDKey].(string); exists && userID != "" { | ||
return userID | ||
// FederatedClaims represents the structure documented by Dex | ||
type FederatedClaims struct { | ||
ConnectorID string `json:"connector_id"` | ||
UserID string `json:"user_id"` | ||
} | ||
|
||
// GetFederatedClaims extracts federated claims from jwt.MapClaims | ||
func GetFederatedClaims(claims jwt.MapClaims) *FederatedClaims { | ||
if federated, ok := claims["federated_claims"].(map[string]interface{}); ok { | ||
return &FederatedClaims{ | ||
ConnectorID: fmt.Sprint(federated["connector_id"]), | ||
UserID: fmt.Sprint(federated["user_id"]), | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// GetUserIdentifier returns a consistent user identifier, checking federated_claims.user_id when Dex is in use | ||
func GetUserIdentifier(claims *ArgoClaims) string { | ||
// Check federated claims first | ||
if claims.FederatedClaims != nil && claims.FederatedClaims.UserID != "" { | ||
return claims.FederatedClaims.UserID | ||
} | ||
// Fallback to sub | ||
if sub, ok := claims[subKey].(string); ok && sub != "" { | ||
return sub | ||
if claims.Subject != "" { | ||
return claims.Subject | ||
} | ||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.