-
Notifications
You must be signed in to change notification settings - Fork 162
Allow customisation of OIDC scopes #3234
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import ( | |
"fmt" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
"time" | ||
|
||
"github.com/coreos/go-oidc/v3/oidc" | ||
|
@@ -37,6 +38,14 @@ const ( | |
ClaimGroups string = "groups" | ||
) | ||
|
||
// DefaultScopes is the set of scopes that we require. | ||
var DefaultScopes = []string{ | ||
oidc.ScopeOpenID, | ||
ScopeProfile, | ||
ScopeEmail, | ||
ScopeGroups, | ||
} | ||
|
||
// OIDCConfig is used to configure an AuthServer to interact with | ||
// an OIDC issuer. | ||
type OIDCConfig struct { | ||
|
@@ -45,6 +54,7 @@ type OIDCConfig struct { | |
ClientSecret string | ||
RedirectURL string | ||
TokenDuration time.Duration | ||
Scopes []string | ||
ClaimsConfig *ClaimsConfig | ||
} | ||
|
||
|
@@ -110,9 +120,29 @@ func NewOIDCConfigFromSecret(secret corev1.Secret) OIDCConfig { | |
|
||
cfg.TokenDuration = tokenDuration | ||
|
||
scopes := splitAndTrim(string(secret.Data["customScopes"])) | ||
if len(scopes) == 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I may have been doing the odd There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case,
Taking the length of a nil slice yields |
||
scopes = []string{oidc.ScopeOpenID, ScopeEmail, ScopeGroups} | ||
} | ||
|
||
cfg.Scopes = scopes | ||
|
||
return cfg | ||
} | ||
|
||
func splitAndTrim(s string) []string { | ||
result := []string{} | ||
splits := strings.Split(s, ",") | ||
|
||
for _, s := range splits { | ||
if v := strings.TrimSpace(s); v != "" { | ||
result = append(result, v) | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
func claimsConfigFromSecret(secret corev1.Secret) *ClaimsConfig { | ||
claimUsername, ok := secret.Data["claimUsername"] | ||
if !ok { | ||
|
@@ -214,27 +244,20 @@ func (s *AuthServer) verifier() *oidc.IDTokenVerifier { | |
} | ||
|
||
func (s *AuthServer) oauth2Config(scopes []string) *oauth2.Config { | ||
requestScopes := []string{} | ||
// Ensure "openid" scope is always present. | ||
if !contains(scopes, oidc.ScopeOpenID) { | ||
scopes = append(scopes, oidc.ScopeOpenID) | ||
requestScopes = append(requestScopes, oidc.ScopeOpenID) | ||
} | ||
|
||
// Request "email" scope to get user's email address. | ||
if !contains(scopes, ScopeEmail) { | ||
scopes = append(scopes, ScopeEmail) | ||
} | ||
|
||
// Request "groups" scope to get user's groups. | ||
if !contains(scopes, ScopeGroups) { | ||
scopes = append(scopes, ScopeGroups) | ||
} | ||
requestScopes = append(requestScopes, scopes...) | ||
|
||
return &oauth2.Config{ | ||
ClientID: s.OIDCConfig.ClientID, | ||
ClientSecret: s.OIDCConfig.ClientSecret, | ||
RedirectURL: s.OIDCConfig.RedirectURL, | ||
Endpoint: s.provider.Endpoint(), | ||
Scopes: scopes, | ||
Scopes: requestScopes, | ||
} | ||
} | ||
|
||
|
@@ -502,8 +525,7 @@ func (s *AuthServer) startAuthFlow(rw http.ResponseWriter, r *http.Request) { | |
|
||
state := base64.StdEncoding.EncodeToString(b) | ||
|
||
scopes := []string{ScopeProfile} | ||
authCodeURL := s.oauth2Config(scopes).AuthCodeURL(state) | ||
authCodeURL := s.oauth2Config(s.OIDCConfig.Scopes).AuthCodeURL(state) | ||
|
||
// Issue state cookie | ||
http.SetCookie(rw, s.createCookie(StateCookieName, state)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This flag doesn't seem to be connected right now, maybe we remove it for now or?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is connected, through a long line of bits.
It is passed here
weave-gitops/cmd/gitops-server/cmd/cmd.go
Line 175 in 2deffd8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh you're right. I was testing with mixed secret/cli config and then missed this
weave-gitops/pkg/server/auth/init.go
Lines 40 to 41 in 2deffd8
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find it a bit confusing that the secret overrides the cmd-line, but that's the existing behaviour for other options, consistency wins out :-)