1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
24 "git.arvados.org/arvados.git/lib/controller/api"
25 "git.arvados.org/arvados.git/lib/controller/rpc"
26 "git.arvados.org/arvados.git/lib/ctrlctx"
27 "git.arvados.org/arvados.git/sdk/go/arvados"
28 "git.arvados.org/arvados.git/sdk/go/auth"
29 "git.arvados.org/arvados.git/sdk/go/ctxlog"
30 "git.arvados.org/arvados.git/sdk/go/httpserver"
31 "github.com/coreos/go-oidc"
32 lru "github.com/hashicorp/golang-lru"
33 "github.com/jmoiron/sqlx"
34 "github.com/sirupsen/logrus"
36 "google.golang.org/api/option"
37 "google.golang.org/api/people/v1"
42 tokenCacheNegativeTTL = time.Minute * 5
43 tokenCacheTTL = time.Minute * 10
44 tokenCacheRaceWindow = time.Minute
47 type oidcLoginController struct {
48 Cluster *arvados.Cluster
50 Issuer string // OIDC issuer URL, e.g., "https://accounts.google.com"
53 UseGooglePeopleAPI bool // Use Google People API to look up alternate email addresses
54 EmailClaim string // OpenID claim to use as email address; typically "email"
55 EmailVerifiedClaim string // If non-empty, ensure claim value is true before accepting EmailClaim; typically "email_verified"
56 UsernameClaim string // If non-empty, use as preferred username
57 AuthParams map[string]string // Additional parameters to pass with authentication request
59 // override Google People API base URL for testing purposes
60 // (normally empty, set by google pkg to
61 // https://people.googleapis.com/)
62 peopleAPIBasePath string
64 provider *oidc.Provider // initialized by setup()
65 oauth2conf *oauth2.Config // initialized by setup()
66 verifier *oidc.IDTokenVerifier // initialized by setup()
67 mu sync.Mutex // protects setup()
70 // Initialize ctrl.provider and ctrl.oauth2conf.
71 func (ctrl *oidcLoginController) setup() error {
73 defer ctrl.mu.Unlock()
74 if ctrl.provider != nil {
78 redirURL, err := (*url.URL)(&ctrl.Cluster.Services.Controller.ExternalURL).Parse("/" + arvados.EndpointLogin.Path)
80 return fmt.Errorf("error making redirect URL: %s", err)
82 provider, err := oidc.NewProvider(context.Background(), ctrl.Issuer)
86 ctrl.oauth2conf = &oauth2.Config{
87 ClientID: ctrl.ClientID,
88 ClientSecret: ctrl.ClientSecret,
89 Endpoint: provider.Endpoint(),
90 Scopes: []string{oidc.ScopeOpenID, "profile", "email"},
91 RedirectURL: redirURL.String(),
93 ctrl.verifier = provider.Verifier(&oidc.Config{
94 ClientID: ctrl.ClientID,
96 ctrl.provider = provider
100 func (ctrl *oidcLoginController) Logout(ctx context.Context, opts arvados.LogoutOptions) (arvados.LogoutResponse, error) {
101 return noopLogout(ctrl.Cluster, opts)
104 func (ctrl *oidcLoginController) Login(ctx context.Context, opts arvados.LoginOptions) (arvados.LoginResponse, error) {
107 return loginError(fmt.Errorf("error setting up OpenID Connect provider: %s", err))
109 if opts.State == "" {
110 // Initiate OIDC sign-in.
111 if opts.ReturnTo == "" {
112 return loginError(errors.New("missing return_to parameter"))
114 state := ctrl.newOAuth2State([]byte(ctrl.Cluster.SystemRootToken), opts.Remote, opts.ReturnTo)
115 var authparams []oauth2.AuthCodeOption
116 for k, v := range ctrl.AuthParams {
117 authparams = append(authparams, oauth2.SetAuthURLParam(k, v))
119 return arvados.LoginResponse{
120 RedirectLocation: ctrl.oauth2conf.AuthCodeURL(state.String(), authparams...),
123 // Callback after OIDC sign-in.
124 state := ctrl.parseOAuth2State(opts.State)
125 if !state.verify([]byte(ctrl.Cluster.SystemRootToken)) {
126 return loginError(errors.New("invalid OAuth2 state"))
128 oauth2Token, err := ctrl.oauth2conf.Exchange(ctx, opts.Code)
130 return loginError(fmt.Errorf("error in OAuth2 exchange: %s", err))
132 rawIDToken, ok := oauth2Token.Extra("id_token").(string)
134 return loginError(errors.New("error in OAuth2 exchange: no ID token in OAuth2 token"))
136 idToken, err := ctrl.verifier.Verify(ctx, rawIDToken)
138 return loginError(fmt.Errorf("error verifying ID token: %s", err))
140 authinfo, err := ctrl.getAuthInfo(ctx, oauth2Token, idToken)
142 return loginError(err)
144 ctxRoot := auth.NewContext(ctx, &auth.Credentials{Tokens: []string{ctrl.Cluster.SystemRootToken}})
145 return ctrl.Parent.UserSessionCreate(ctxRoot, rpc.UserSessionCreateOptions{
146 ReturnTo: state.Remote + "," + state.ReturnTo,
151 func (ctrl *oidcLoginController) UserAuthenticate(ctx context.Context, opts arvados.UserAuthenticateOptions) (arvados.APIClientAuthorization, error) {
152 return arvados.APIClientAuthorization{}, httpserver.ErrorWithStatus(errors.New("username/password authentication is not available"), http.StatusBadRequest)
155 // claimser can decode arbitrary claims into a map. Implemented by
156 // *oauth2.IDToken and *oauth2.UserInfo.
157 type claimser interface {
158 Claims(interface{}) error
161 // Use a person's token to get all of their email addresses, with the
162 // primary address at index 0. The provided defaultAddr is always
163 // included in the returned slice, and is used as the primary if the
164 // Google API does not indicate one.
165 func (ctrl *oidcLoginController) getAuthInfo(ctx context.Context, token *oauth2.Token, claimser claimser) (*rpc.UserSessionAuthInfo, error) {
166 var ret rpc.UserSessionAuthInfo
167 defer ctxlog.FromContext(ctx).WithField("ret", &ret).Debug("getAuthInfo returned")
169 var claims map[string]interface{}
170 if err := claimser.Claims(&claims); err != nil {
171 return nil, fmt.Errorf("error extracting claims from token: %s", err)
172 } else if verified, _ := claims[ctrl.EmailVerifiedClaim].(bool); verified || ctrl.EmailVerifiedClaim == "" {
173 // Fall back to this info if the People API call
174 // (below) doesn't return a primary && verified email.
175 name, _ := claims["name"].(string)
176 if names := strings.Fields(strings.TrimSpace(name)); len(names) > 1 {
177 ret.FirstName = strings.Join(names[0:len(names)-1], " ")
178 ret.LastName = names[len(names)-1]
179 } else if len(names) > 0 {
180 ret.FirstName = names[0]
182 ret.Email, _ = claims[ctrl.EmailClaim].(string)
185 if ctrl.UsernameClaim != "" {
186 ret.Username, _ = claims[ctrl.UsernameClaim].(string)
189 if !ctrl.UseGooglePeopleAPI {
191 return nil, fmt.Errorf("cannot log in with unverified email address %q", claims[ctrl.EmailClaim])
196 svc, err := people.NewService(ctx, option.WithTokenSource(ctrl.oauth2conf.TokenSource(ctx, token)), option.WithScopes(people.UserEmailsReadScope))
198 return nil, fmt.Errorf("error setting up People API: %s", err)
200 if p := ctrl.peopleAPIBasePath; p != "" {
201 // Override normal API endpoint (for testing)
204 person, err := people.NewPeopleService(svc).Get("people/me").PersonFields("emailAddresses,names").Do()
206 if strings.Contains(err.Error(), "Error 403") && strings.Contains(err.Error(), "accessNotConfigured") {
207 // Log the original API error, but display
208 // only the "fix config" advice to the user.
209 ctxlog.FromContext(ctx).WithError(err).WithField("email", ret.Email).Error("People API is not enabled")
210 return nil, errors.New("configuration error: Login.GoogleAlternateEmailAddresses is true, but Google People API is not enabled")
212 return nil, fmt.Errorf("error getting profile info from People API: %s", err)
215 // The given/family names returned by the People API and
216 // flagged as "primary" (if any) take precedence over the
217 // split-by-whitespace result from above.
218 for _, name := range person.Names {
219 if name.Metadata != nil && name.Metadata.Primary {
220 ret.FirstName = name.GivenName
221 ret.LastName = name.FamilyName
226 altEmails := map[string]bool{}
228 altEmails[ret.Email] = true
230 for _, ea := range person.EmailAddresses {
231 if ea.Metadata == nil || !ea.Metadata.Verified {
232 ctxlog.FromContext(ctx).WithField("address", ea.Value).Info("skipping unverified email address")
235 altEmails[ea.Value] = true
236 if ea.Metadata.Primary || ret.Email == "" {
240 if len(altEmails) == 0 {
241 return nil, errors.New("cannot log in without a verified email address")
243 for ae := range altEmails {
247 ret.AlternateEmails = append(ret.AlternateEmails, ae)
248 if ret.Username == "" {
249 i := strings.Index(ae, "@")
250 if i > 0 && strings.ToLower(ae[i+1:]) == strings.ToLower(ctrl.Cluster.Users.PreferDomainForUsername) {
251 ret.Username = strings.SplitN(ae[:i], "+", 2)[0]
258 func loginError(sendError error) (resp arvados.LoginResponse, err error) {
259 tmpl, err := template.New("error").Parse(`<h2>Login error:</h2><p>{{.}}</p>`)
263 err = tmpl.Execute(&resp.HTML, sendError.Error())
267 func (ctrl *oidcLoginController) newOAuth2State(key []byte, remote, returnTo string) oauth2State {
269 Time: time.Now().Unix(),
273 s.HMAC = s.computeHMAC(key)
277 type oauth2State struct {
278 HMAC []byte // hash of other fields; see computeHMAC()
279 Time int64 // creation time (unix timestamp)
280 Remote string // remote cluster if requesting a salted token, otherwise blank
281 ReturnTo string // redirect target
284 func (ctrl *oidcLoginController) parseOAuth2State(encoded string) (s oauth2State) {
285 // Errors are not checked. If decoding/parsing fails, the
286 // token will be rejected by verify().
287 decoded, _ := base64.RawURLEncoding.DecodeString(encoded)
288 f := strings.Split(string(decoded), "\n")
292 fmt.Sscanf(f[0], "%x", &s.HMAC)
293 fmt.Sscanf(f[1], "%x", &s.Time)
294 fmt.Sscanf(f[2], "%s", &s.Remote)
295 fmt.Sscanf(f[3], "%s", &s.ReturnTo)
299 func (s oauth2State) verify(key []byte) bool {
300 if delta := time.Now().Unix() - s.Time; delta < 0 || delta > 300 {
303 return hmac.Equal(s.computeHMAC(key), s.HMAC)
306 func (s oauth2State) String() string {
308 enc := base64.NewEncoder(base64.RawURLEncoding, &buf)
309 fmt.Fprintf(enc, "%x\n%x\n%s\n%s", s.HMAC, s.Time, s.Remote, s.ReturnTo)
314 func (s oauth2State) computeHMAC(key []byte) []byte {
315 mac := hmac.New(sha256.New, key)
316 fmt.Fprintf(mac, "%x %s %s", s.Time, s.Remote, s.ReturnTo)
320 func OIDCAccessTokenAuthorizer(cluster *arvados.Cluster, getdb func(context.Context) (*sqlx.DB, error)) *oidcTokenAuthorizer {
321 // We want ctrl to be nil if the chosen controller is not a
322 // *oidcLoginController, so we can ignore the 2nd return value
323 // of this type cast.
324 ctrl, _ := NewConn(cluster).loginController.(*oidcLoginController)
325 cache, err := lru.New2Q(tokenCacheSize)
329 return &oidcTokenAuthorizer{
336 type oidcTokenAuthorizer struct {
337 ctrl *oidcLoginController
338 getdb func(context.Context) (*sqlx.DB, error)
339 cache *lru.TwoQueueCache
342 func (ta *oidcTokenAuthorizer) Middleware(w http.ResponseWriter, r *http.Request, next http.Handler) {
344 // Not using a compatible (OIDC) login controller.
345 } else if authhdr := strings.Split(r.Header.Get("Authorization"), " "); len(authhdr) > 1 && (authhdr[0] == "OAuth2" || authhdr[0] == "Bearer") {
346 err := ta.registerToken(r.Context(), authhdr[1])
348 http.Error(w, err.Error(), http.StatusInternalServerError)
355 func (ta *oidcTokenAuthorizer) WrapCalls(origFunc api.RoutableFunc) api.RoutableFunc {
357 // Not using a compatible (OIDC) login controller.
360 return func(ctx context.Context, opts interface{}) (_ interface{}, err error) {
361 creds, ok := auth.FromContext(ctx)
363 return origFunc(ctx, opts)
365 // Check each token in the incoming request. If any
366 // are valid OAuth2 access tokens, insert/update them
367 // in the database so RailsAPI's auth code accepts
369 for _, tok := range creds.Tokens {
370 err = ta.registerToken(ctx, tok)
375 return origFunc(ctx, opts)
379 // registerToken checks whether tok is a valid OIDC Access Token and,
380 // if so, ensures that an api_client_authorizations row exists so that
381 // RailsAPI will accept it as an Arvados token.
382 func (ta *oidcTokenAuthorizer) registerToken(ctx context.Context, tok string) error {
383 if tok == ta.ctrl.Cluster.SystemRootToken || strings.HasPrefix(tok, "v2/") {
386 if cached, hit := ta.cache.Get(tok); !hit {
387 // Fall through to database and OIDC provider checks
389 } else if exp, ok := cached.(time.Time); ok {
390 // cached negative result (value is expiry time)
391 if time.Now().Before(exp) {
396 // cached positive result
397 aca := cached.(arvados.APIClientAuthorization)
399 if aca.ExpiresAt != "" {
400 t, err := time.Parse(time.RFC3339Nano, aca.ExpiresAt)
402 return fmt.Errorf("error parsing expires_at value: %w", err)
404 expiring = t.Before(time.Now().Add(time.Minute))
411 db, err := ta.getdb(ctx)
415 tx, err := db.Beginx()
420 ctx = ctrlctx.NewWithTransaction(ctx, tx)
422 // We use hmac-sha256(accesstoken,systemroottoken) as the
423 // secret part of our own token, and avoid storing the auth
424 // provider's real secret in our database.
425 mac := hmac.New(sha256.New, []byte(ta.ctrl.Cluster.SystemRootToken))
426 io.WriteString(mac, tok)
427 hmac := fmt.Sprintf("%x", mac.Sum(nil))
430 err = tx.QueryRowContext(ctx, `select (expires_at is not null and expires_at - interval '1 minute' <= current_timestamp at time zone 'UTC') from api_client_authorizations where api_token=$1`, hmac).Scan(&expiring)
431 if err != nil && err != sql.ErrNoRows {
432 return fmt.Errorf("database error while checking token: %w", err)
433 } else if err == nil && !expiring {
434 // Token is already in the database as an Arvados
435 // token, and isn't about to expire, so we can pass it
436 // through to RailsAPI etc. regardless of whether it's
437 // an OIDC access token.
440 updating := err == nil
442 // Check whether the token is a valid OIDC access token. If
443 // so, swap it out for an Arvados token (creating/updating an
444 // api_client_authorizations row if needed) which downstream
445 // server components will accept.
446 err = ta.ctrl.setup()
448 return fmt.Errorf("error setting up OpenID Connect provider: %s", err)
450 oauth2Token := &oauth2.Token{
453 userinfo, err := ta.ctrl.provider.UserInfo(ctx, oauth2.StaticTokenSource(oauth2Token))
455 ta.cache.Add(tok, time.Now().Add(tokenCacheNegativeTTL))
458 ctxlog.FromContext(ctx).WithField("userinfo", userinfo).Debug("(*oidcTokenAuthorizer)registerToken: got userinfo")
459 authinfo, err := ta.ctrl.getAuthInfo(ctx, oauth2Token, userinfo)
464 // Expiry time for our token is one minute longer than our
465 // cache TTL, so we don't pass it through to RailsAPI just as
467 exp := time.Now().UTC().Add(tokenCacheTTL + tokenCacheRaceWindow)
469 var aca arvados.APIClientAuthorization
471 _, err = tx.ExecContext(ctx, `update api_client_authorizations set expires_at=$1 where api_token=$2`, exp, hmac)
473 return fmt.Errorf("error updating token expiry time: %w", err)
475 ctxlog.FromContext(ctx).WithField("HMAC", hmac).Debug("(*oidcTokenAuthorizer)registerToken: updated api_client_authorizations row")
477 aca, err = ta.ctrl.Parent.CreateAPIClientAuthorization(ctx, ta.ctrl.Cluster.SystemRootToken, *authinfo)
481 _, err = tx.ExecContext(ctx, `update api_client_authorizations set api_token=$1, expires_at=$2 where uuid=$3`, hmac, exp, aca.UUID)
483 return fmt.Errorf("error adding OIDC access token to database: %w", err)
486 ctxlog.FromContext(ctx).WithFields(logrus.Fields{"UUID": aca.UUID, "HMAC": hmac}).Debug("(*oidcTokenAuthorizer)registerToken: inserted api_client_authorizations row")
492 aca.ExpiresAt = exp.Format(time.RFC3339Nano)
493 ta.cache.Add(tok, aca)