Skip to content

Commit 05c9d14

Browse files
authored
Merge pull request #116 from nais/auth-scopes-prompt
add config for additional oauth scopes
2 parents 2ff6615 + 01cd247 commit 05c9d14

File tree

10 files changed

+25
-10
lines changed

10 files changed

+25
-10
lines changed

charts/Feature.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ values:
114114
config:
115115
type: string
116116

117+
oauth.additionalScopes:
118+
displayName: OAuth additional scopes
119+
description: List of additional scopes to use in the OAuth login flow
120+
config:
121+
type: string_array
122+
117123
staticServiceAccounts:
118124
displayName: Static nais-api service accounts
119125
description: JSON-encoded list of static service accounts

charts/templates/deployment.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ spec:
7272
value: "https://{{ .Values.host }}/oauth2/callback"
7373
- name: OAUTH_FRONTEND_URL
7474
value: "https://{{ .Values.host }}"
75+
- name: OAUTH_ADDITIONAL_SCOPES
76+
value: "{{ .Values.oauth.additionalScopes | join "," }}"
7577
- name: LISTEN_ADDRESS
7678
value: ":3000"
7779
- name: INTERNAL_LISTEN_ADDRESS

charts/values.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ oauth: # mapped in fasit
4949
issuer: "https://accounts.google.com"
5050
clientID: ""
5151
clientSecret: ""
52+
additionalScopes: []
5253

5354
slack:
5455
feedbackChannel: "console-user-feedback"

go.mod

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
module github.com/nais/api
22

3-
go 1.24.0
4-
5-
toolchain go1.24.1
3+
go 1.24.1
64

75
tool (
86
github.com/99designs/gqlgen

internal/auth/authn/handler.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,13 @@ func (h *handler) Login(w http.ResponseWriter, r *http.Request) {
7575
Secure: true,
7676
HttpOnly: true,
7777
})
78-
consentUrl := h.oauth2Config.AuthCodeURL(oauthState, oauth2.SetAuthURLParam("prompt", "select_account"))
79-
http.Redirect(w, r, consentUrl, http.StatusFound)
78+
79+
opts := make([]oauth2.AuthCodeOption, 0)
80+
81+
if prompt := r.URL.Query().Get("prompt"); prompt != "" {
82+
opts = append(opts, oauth2.SetAuthURLParam("prompt", prompt))
83+
}
84+
http.Redirect(w, r, h.oauth2Config.AuthCodeURL(oauthState, opts...), http.StatusFound)
8085
}
8186

8287
func (h *handler) Callback(w http.ResponseWriter, r *http.Request) {

internal/auth/authn/oidc.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type OIDC struct {
1212
provider *oidc.Provider
1313
}
1414

15-
func NewOIDC(ctx context.Context, issuer, clientID, clientSecret, redirectURL string) (*OIDC, error) {
15+
func NewOIDC(ctx context.Context, issuer, clientID, clientSecret, redirectURL string, additionalScopes []string) (*OIDC, error) {
1616
provider, err := oidc.NewProvider(ctx, issuer)
1717
if err != nil {
1818
return nil, err
@@ -25,7 +25,7 @@ func NewOIDC(ctx context.Context, issuer, clientID, clientSecret, redirectURL st
2525
ClientSecret: clientSecret,
2626
Endpoint: provider.Endpoint(),
2727
RedirectURL: redirectURL,
28-
Scopes: []string{oidc.ScopeOpenID, "profile", "email"},
28+
Scopes: append([]string{oidc.ScopeOpenID, "profile", "email"}, additionalScopes...),
2929
},
3030
}, nil
3131
}

internal/cmd/api/api.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ func loadEnvFile(log logrus.FieldLogger) error {
299299
}
300300

301301
func setupAuthHandler(ctx context.Context, cfg oAuthConfig, log logrus.FieldLogger) (authn.Handler, error) {
302-
cf, err := authn.NewOIDC(ctx, cfg.Issuer, cfg.ClientID, cfg.ClientSecret, cfg.RedirectURL)
302+
cf, err := authn.NewOIDC(ctx, cfg.Issuer, cfg.ClientID, cfg.ClientSecret, cfg.RedirectURL, cfg.AdditionalScopes)
303303
if err != nil {
304304
return nil, err
305305
}

internal/cmd/api/config.go

+3
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ type oAuthConfig struct {
8989

9090
// RedirectURL The URL that Google will redirect back to after performing authentication.
9191
RedirectURL string `env:"OAUTH_REDIRECT_URL"`
92+
93+
// AdditionalScopes is a list of additional scopes to request in the OAuth login flow.
94+
AdditionalScopes []string `env:"OAUTH_ADDITIONAL_SCOPES"`
9295
}
9396

9497
type unleashConfig struct {

mise.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
pin = true
33

44
[tools]
5-
go = "1.24.0"
5+
go = "1.24.1"
66
helm = "3.17.1"
77
node = "lts"
88
protoc = "29.3"

pkg/apiclient/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/nais/api/pkg/apiclient
22

3-
go 1.24
3+
go 1.24.1
44

55
require (
66
github.com/stretchr/testify v1.10.0

0 commit comments

Comments
 (0)