Skip to content

Commit 9bf8a89

Browse files
authored
Merge pull request #13 from int128/extra-scopes
Add extra-scopes support
2 parents a91c020 + d4fb496 commit 9bf8a89

File tree

8 files changed

+36
-0
lines changed

8 files changed

+36
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ Key | Direction | Value
208208
`client-secret` | IN (Required) | Client Secret of the provider.
209209
`idp-certificate-authority` | IN (Optional) | CA certificate path of the provider.
210210
`idp-certificate-authority-data` | IN (Optional) | Base64 encoded CA certificate of the provider.
211+
`extra-scopes` | IN (Optional) | Scopes to request to the provider (comma separated).
211212
`id-token` | OUT | ID token got from the provider.
212213
`refresh-token` | OUT | Refresh token got from the provider.
213214

cli/cli.go

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ func (c *CLI) Run(ctx context.Context) error {
7070
Issuer: authProvider.IDPIssuerURL(),
7171
ClientID: authProvider.ClientID(),
7272
ClientSecret: authProvider.ClientSecret(),
73+
ExtraScopes: authProvider.ExtraScopes(),
7374
Client: &http.Client{Transport: &http.Transport{TLSClientConfig: tlsConfig}},
7475
ServerPort: 8000,
7576
SkipOpenBrowser: c.SkipOpenBrowser,

e2e/authserver/authserver.go

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const ServerKey = "authserver/testdata/server.key"
2323
// Config represents server configuration.
2424
type Config struct {
2525
Issuer string
26+
Scope string
2627
Cert string
2728
Key string
2829
}

e2e/authserver/handler.go

+8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type handler struct {
2222
authCode string
2323

2424
Issuer string
25+
Scope string // Default to openid
2526
IDToken string
2627
PrivateKey struct{ N, E string }
2728
}
@@ -33,6 +34,10 @@ func newHandler(t *testing.T, c *Config) *handler {
3334
jwks: readTemplate(t, "oidc-jwks.json"),
3435
authCode: "3d24a8bd-35e6-457d-999e-e04bb1dfcec7",
3536
Issuer: c.Issuer,
37+
Scope: c.Scope,
38+
}
39+
if h.Scope == "" {
40+
h.Scope = "openid"
3641
}
3742

3843
token := jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.StandardClaims{
@@ -83,6 +88,9 @@ func (h *handler) serveHTTP(w http.ResponseWriter, r *http.Request) error {
8388
// Authentication Response
8489
// http://openid.net/specs/openid-connect-core-1_0.html#AuthResponse
8590
q := r.URL.Query()
91+
if h.Scope != q.Get("scope") {
92+
return fmt.Errorf("scope wants %s but %s", h.Scope, q.Get("scope"))
93+
}
8694
to := fmt.Sprintf("%s?state=%s&code=%s", q.Get("redirect_uri"), q.Get("state"), h.authCode)
8795
http.Redirect(w, r, to, 302)
8896
case m == "POST" && p == "/protocol/openid-connect/token":

e2e/e2e_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ func TestE2E(t *testing.T) {
3737
authserver.Config{Issuer: "http://localhost:9000"},
3838
&tls.Config{},
3939
},
40+
"ExtraScope": {
41+
kubeconfigValues{
42+
Issuer: "http://localhost:9000",
43+
ExtraScopes: "profile groups",
44+
},
45+
cli.CLI{},
46+
authserver.Config{
47+
Issuer: "http://localhost:9000",
48+
Scope: "profile groups openid",
49+
},
50+
&tls.Config{},
51+
},
4052
"SkipTLSVerify": {
4153
kubeconfigValues{Issuer: "https://localhost:9000"},
4254
cli.CLI{SkipTLSVerify: true},

e2e/kubeconfig.go

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
type kubeconfigValues struct {
1111
Issuer string
12+
ExtraScopes string
1213
IDPCertificateAuthority string
1314
IDPCertificateAuthorityData string
1415
}

e2e/testdata/kubeconfig.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ users:
1919
client-id: kubernetes
2020
client-secret: a3c508c3-73c9-42e2-ab14-487a1bf67c33
2121
idp-issuer-url: {{ .Issuer }}
22+
#{{ if .ExtraScopes }}
23+
extra-scopes: {{ .ExtraScopes }}
24+
#{{ end }}
2225
#{{ if .IDPCertificateAuthority }}
2326
idp-certificate-authority: {{ .IDPCertificateAuthority }}
2427
#{{ end }}

kubeconfig/auth.go

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package kubeconfig
22

33
import (
44
"fmt"
5+
"strings"
56

67
"k8s.io/client-go/tools/clientcmd/api"
78
)
@@ -55,6 +56,14 @@ func (c *OIDCAuthProvider) IDPCertificateAuthorityData() string {
5556
return c.Config["idp-certificate-authority-data"]
5657
}
5758

59+
// ExtraScopes returns the extra-scopes.
60+
func (c *OIDCAuthProvider) ExtraScopes() []string {
61+
if c.Config["extra-scopes"] == "" {
62+
return []string{}
63+
}
64+
return strings.Split(c.Config["extra-scopes"], ",")
65+
}
66+
5867
// SetIDToken replaces the id-token.
5968
func (c *OIDCAuthProvider) SetIDToken(idToken string) {
6069
c.Config["id-token"] = idToken

0 commit comments

Comments
 (0)