Skip to content

Commit

Permalink
Fixes for the review
Browse files Browse the repository at this point in the history
  • Loading branch information
gmgigi96 committed Oct 15, 2021
1 parent d9ff893 commit bf15705
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions pkg/auth/manager/machine/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ import (
// To impersonate the given user it's only needed an api-key, saved
// in a config file.

// supported claims
var claims = []string{"mail", "uid", "username"}

type manager struct {
APIKey string `mapstructure:"api_key"`
GatewayAddr string `mapstructure:"gateway_addr"`
Expand Down Expand Up @@ -78,7 +81,7 @@ func (m *manager) Authenticate(ctx context.Context, user, secret string) (*userp
}

// username could be either a normal username or a string <claim>:<value>
// in the first case the calim is the default one, so "username"
// in the first case the claim is "username"
claim, value := parseUser(user)

userResponse, err := gtw.GetUserByClaim(ctx, &userpb.GetUserByClaimRequest{
Expand All @@ -104,9 +107,18 @@ func (m *manager) Authenticate(ctx context.Context, user, secret string) (*userp

}

func contains(lst []string, s string) bool {
for _, e := range lst {
if e == s {
return true
}
}
return false
}

func parseUser(user string) (string, string) {
s := strings.Split(user, ":")
if len(s) == 2 {
s := strings.SplitN(user, ":", 2)
if len(s) == 2 && contains(claims, s[0]) {
return s[0], s[1]
}
return "username", user
Expand Down

0 comments on commit bf15705

Please sign in to comment.