From ae857acb21a7907ac8ce3223120c4a7e333570a1 Mon Sep 17 00:00:00 2001 From: Alexander Hebel Date: Fri, 16 Feb 2024 14:47:38 +0100 Subject: [PATCH 1/3] add scim_id and groups as token methods --- auth/token.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/auth/token.go b/auth/token.go index 5e3fae7..85f4c19 100644 --- a/auth/token.go +++ b/auth/token.go @@ -25,6 +25,8 @@ const ( claimSapGlobalAppTID = "app_tid" claimIasIssuer = "ias_iss" claimAzp = "azp" + claimScimId = "scim_id" + claimGroups = "groups" ) type Token struct { @@ -146,6 +148,18 @@ func (t Token) UserUUID() string { return v } +// ScimId returns "scim_id" claim, if it doesn't exist empty string is returned +func (t Token) ScimId() string { + v, _ := t.GetClaimAsString(claimScimId) + return v +} + +// Groups returns "groups" claim, if it doesn't exist empty string is returned +func (t Token) Groups() []string { + v, _ := t.GetClaimAsStringSlice(claimGroups) + return v +} + // ErrClaimNotExists shows that the requested custom claim does not exist in the token var ErrClaimNotExists = errors.New("claim does not exist in the token") @@ -168,7 +182,7 @@ func (t Token) GetClaimAsString(claim string) (string, error) { return stringValue, nil } -// GetClaimAsStringSlice returns a custom claim type asserted as string slice. The claim name is case sensitive. Returns error if the claim is not available or not an array +// GetClaimAsStringSlice returns a custom claim type asserted as string slice. The claim name is case-sensitive. Returns error if the claim is not available or not an array func (t Token) GetClaimAsStringSlice(claim string) ([]string, error) { value, exists := t.jwtToken.Get(claim) if !exists { From ffdf05dcd210ee715f28356a859ec19795872028 Mon Sep 17 00:00:00 2001 From: Alexander Hebel Date: Fri, 16 Feb 2024 14:48:57 +0100 Subject: [PATCH 2/3] better naming in sample --- sample/middleware.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sample/middleware.go b/sample/middleware.go index 8eef8d2..068d4b6 100644 --- a/sample/middleware.go +++ b/sample/middleware.go @@ -28,8 +28,8 @@ func main() { panic(err) } authMiddleware := auth.NewMiddleware(config, auth.Options{}) - r.Use(authMiddleware.AuthenticationHandler) - r.HandleFunc("/helloWorld", helloWorld).Methods(http.MethodGet) + r.Use(authMiddleware.AuthenticationHandler) // force oauth2 bearer token flow + r.HandleFunc("/auth", parseToken).Methods(http.MethodGet) address := ":" + os.Getenv("PORT") if address == "" { @@ -47,7 +47,7 @@ func main() { } } -func helloWorld(w http.ResponseWriter, r *http.Request) { +func parseToken(w http.ResponseWriter, r *http.Request) { user, ok := auth.TokenFromCtx(r) if ok { _, _ = fmt.Fprintf(w, "Hello world!\nYou're logged in as %s", user.Email()) From 53b9e5c06090e0d2bbcb71b949fcaf8f8ffe1e47 Mon Sep 17 00:00:00 2001 From: Alexander Hebel Date: Fri, 16 Feb 2024 14:55:05 +0100 Subject: [PATCH 3/3] fix linter --- auth/token.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/auth/token.go b/auth/token.go index 85f4c19..c5d321d 100644 --- a/auth/token.go +++ b/auth/token.go @@ -25,7 +25,7 @@ const ( claimSapGlobalAppTID = "app_tid" claimIasIssuer = "ias_iss" claimAzp = "azp" - claimScimId = "scim_id" + claimScimID = "scim_id" claimGroups = "groups" ) @@ -148,9 +148,9 @@ func (t Token) UserUUID() string { return v } -// ScimId returns "scim_id" claim, if it doesn't exist empty string is returned -func (t Token) ScimId() string { - v, _ := t.GetClaimAsString(claimScimId) +// ScimID returns "scim_id" claim, if it doesn't exist empty string is returned +func (t Token) ScimID() string { + v, _ := t.GetClaimAsString(claimScimID) return v }