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

Send configured claims as headers to backends #72

Open
wants to merge 3 commits 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
7 changes: 4 additions & 3 deletions internal/api/storage/v1alpha1/types.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package v1alpha1

type UserInfo struct {
Username string `json:"username"`
Email string `json:"email"`
Groups []string `json:"groups"`
Username string `json:"username"`
Email string `json:"email"`
Groups []string `json:"groups"`
ExtraClaims map[string]string `json:"extraClaims"`
}
1 change: 1 addition & 0 deletions internal/configuration/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Config struct {
GroupClaimPrefix string `long:"group-claim-prefix" env:"GROUP_CLAIM_PREFIX" default:"oidc:" description:"prefix oidc group claims with this value"`
EncryptionKeyString string `long:"encryption-key" env:"ENCRYPTION_KEY" description:"Encryption key used to encrypt the cookie (required)" json:"-"`
GroupsAttributeName string `long:"groups-attribute-name" env:"GROUPS_ATTRIBUTE_NAME" default:"groups" description:"Map the correct attribute that contain the user groups"`
ExtraClaims map[string]string `long:"extra-claims" env:"EXTRA_CLAIMS" description:"List of extra claims in the jwt to pass as headers. Format is 'Header-Name:claim-name'" env-delim:","`

// RBAC
EnableRBAC bool `long:"enable-rbac" env:"ENABLE_RBAC" description:"Indicates that RBAC support should be enabled"`
Expand Down
39 changes: 27 additions & 12 deletions internal/handlers/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,14 @@ func (s *Server) AuthHandler(rule string) http.HandlerFunc {
}

// Authorize user
groups, err := s.getGroupsFromSession(r)
session, err := s.getSession(r)
if err != nil {
logger.Errorf("error getting groups from session: %v", err)
logger.Errorf("error getting session: %v", err)
s.notAuthenticated(logger, w, r)
return
}

groups := session.Groups
if groups == nil {
logger.Info("groups session data is missing, re-authenticating")
s.notAuthenticated(logger, w, r)
Expand Down Expand Up @@ -212,6 +213,11 @@ func (s *Server) AuthHandler(rule string) http.HandlerFunc {
w.Header().Set(headerName, id.Email)
}

// Map extra claims to headers
for k, v := range session.ExtraClaims {
w.Header().Set(k, v)
}

if s.config.EnableImpersonation {
// Set impersonation headers
logger.Debug(fmt.Sprintf("setting authorization token and impersonation headers: email: %s, groups: %s", id.Email, groups))
Expand Down Expand Up @@ -375,10 +381,23 @@ func (s *Server) AuthCallbackHandler() http.HandlerFunc {
logger.Warnf("failed to get groups claim from the ID token (GroupsAttributeName: %s)", s.config.GroupsAttributeName)
}

// Save extra claims
extraClaims := make(map[string]string)
for header_name, claim_name := range s.config.ExtraClaims {
claim_value, ok := claims[claim_name]
if !ok || (ok && strings.TrimSpace(claim_name) == "") {
logger.Warnf("failed to get extra claim from the ID token (ClaimName: %s)", claim_name)
continue
}

extraClaims[header_name] = claim_value.(string)
}

if err := s.userinfo.Save(r, w, &v1alpha1.UserInfo{
Username: name.(string),
Email: email.(string),
Groups: groups,
Username: name.(string),
Email: email.(string),
Groups: groups,
ExtraClaims: extraClaims,
}); err != nil {
logger.Errorf("error saving session: %v", err)
http.Error(w, "Bad Gateway", 502)
Expand Down Expand Up @@ -484,13 +503,9 @@ func (s *Server) logger(r *http.Request, rule, msg string) *logrus.Entry {
return logger
}

// getGroupsFromSession returns list of groups present in the session
func (s *Server) getGroupsFromSession(r *http.Request) ([]string, error) {
userInfo, err := s.userinfo.Get(r)
if err != nil {
return nil, err
}
return userInfo.Groups, nil
// getSession returns the current session containing username, email, groups and extra claims
func (s *Server) getSession(r *http.Request) (*v1alpha1.UserInfo, error) {
return s.userinfo.Get(r)
}

// authzIsBypassed returns true if the request matches a bypass URI pattern
Expand Down