X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/3b4bb3d393adc3bd3ddfb4442a65087275a5c5c3..fbd40a96ea616d8042db23371083ebf80684825f:/lib/controller/localdb/login.go diff --git a/lib/controller/localdb/login.go b/lib/controller/localdb/login.go index 9a0ee746e6..1267414842 100644 --- a/lib/controller/localdb/login.go +++ b/lib/controller/localdb/login.go @@ -6,11 +6,16 @@ package localdb import ( "context" + "database/sql" + "encoding/json" "errors" + "fmt" "net/http" "net/url" + "strings" "git.arvados.org/arvados.git/lib/controller/rpc" + "git.arvados.org/arvados.git/lib/ctrlctx" "git.arvados.org/arvados.git/sdk/go/arvados" "git.arvados.org/arvados.git/sdk/go/auth" "git.arvados.org/arvados.git/sdk/go/httpserver" @@ -28,8 +33,13 @@ func chooseLoginController(cluster *arvados.Cluster, railsProxy *railsProxy) log wantSSO := cluster.Login.SSO.Enable wantPAM := cluster.Login.PAM.Enable wantLDAP := cluster.Login.LDAP.Enable + wantTest := cluster.Login.Test.Enable switch { - case wantGoogle && !wantOpenIDConnect && !wantSSO && !wantPAM && !wantLDAP: + case 1 != countTrue(wantGoogle, wantOpenIDConnect, wantSSO, wantPAM, wantLDAP, wantTest): + return errorLoginController{ + error: errors.New("configuration problem: exactly one of Login.Google, Login.OpenIDConnect, Login.SSO, Login.PAM, Login.LDAP, and Login.Test must be enabled"), + } + case wantGoogle: return &oidcLoginController{ Cluster: cluster, RailsProxy: railsProxy, @@ -37,26 +47,43 @@ func chooseLoginController(cluster *arvados.Cluster, railsProxy *railsProxy) log ClientID: cluster.Login.Google.ClientID, ClientSecret: cluster.Login.Google.ClientSecret, UseGooglePeopleAPI: cluster.Login.Google.AlternateEmailAddresses, + EmailClaim: "email", + EmailVerifiedClaim: "email_verified", } - case !wantGoogle && wantOpenIDConnect && !wantSSO && !wantPAM && !wantLDAP: + case wantOpenIDConnect: return &oidcLoginController{ - Cluster: cluster, - RailsProxy: railsProxy, - Issuer: cluster.Login.OpenIDConnect.Issuer, - ClientID: cluster.Login.OpenIDConnect.ClientID, - ClientSecret: cluster.Login.OpenIDConnect.ClientSecret, + Cluster: cluster, + RailsProxy: railsProxy, + Issuer: cluster.Login.OpenIDConnect.Issuer, + ClientID: cluster.Login.OpenIDConnect.ClientID, + ClientSecret: cluster.Login.OpenIDConnect.ClientSecret, + EmailClaim: cluster.Login.OpenIDConnect.EmailClaim, + EmailVerifiedClaim: cluster.Login.OpenIDConnect.EmailVerifiedClaim, + UsernameClaim: cluster.Login.OpenIDConnect.UsernameClaim, } - case !wantGoogle && !wantOpenIDConnect && wantSSO && !wantPAM && !wantLDAP: + case wantSSO: return &ssoLoginController{railsProxy} - case !wantGoogle && !wantOpenIDConnect && !wantSSO && wantPAM && !wantLDAP: + case wantPAM: return &pamLoginController{Cluster: cluster, RailsProxy: railsProxy} - case !wantGoogle && !wantOpenIDConnect && !wantSSO && !wantPAM && wantLDAP: + case wantLDAP: return &ldapLoginController{Cluster: cluster, RailsProxy: railsProxy} + case wantTest: + return &testLoginController{Cluster: cluster, RailsProxy: railsProxy} default: return errorLoginController{ - error: errors.New("configuration problem: exactly one of Login.Google, Login.OpenIDConnect, Login.SSO, Login.PAM, and Login.LDAP must be enabled"), + error: errors.New("BUG: missing case in login controller setup switch"), + } + } +} + +func countTrue(vals ...bool) int { + n := 0 + for _, val := range vals { + if val { + n++ } } + return n } // Login and Logout are passed through to the wrapped railsProxy; @@ -91,9 +118,9 @@ func noopLogout(cluster *arvados.Cluster, opts arvados.LogoutOptions) (arvados.L return arvados.LogoutResponse{RedirectLocation: target}, nil } -func createAPIClientAuthorization(ctx context.Context, conn *rpc.Conn, rootToken string, authinfo rpc.UserSessionAuthInfo) (arvados.APIClientAuthorization, error) { +func createAPIClientAuthorization(ctx context.Context, conn *rpc.Conn, rootToken string, authinfo rpc.UserSessionAuthInfo) (resp arvados.APIClientAuthorization, err error) { ctxRoot := auth.NewContext(ctx, &auth.Credentials{Tokens: []string{rootToken}}) - resp, err := conn.UserSessionCreate(ctxRoot, rpc.UserSessionCreateOptions{ + newsession, err := conn.UserSessionCreate(ctxRoot, rpc.UserSessionCreateOptions{ // Send a fake ReturnTo value instead of the caller's // opts.ReturnTo. We won't follow the resulting // redirect target anyway. @@ -101,12 +128,36 @@ func createAPIClientAuthorization(ctx context.Context, conn *rpc.Conn, rootToken AuthInfo: authinfo, }) if err != nil { - return arvados.APIClientAuthorization{}, err + return } - target, err := url.Parse(resp.RedirectLocation) + target, err := url.Parse(newsession.RedirectLocation) if err != nil { - return arvados.APIClientAuthorization{}, err + return } token := target.Query().Get("api_token") - return conn.APIClientAuthorizationCurrent(auth.NewContext(ctx, auth.NewCredentials(token)), arvados.GetOptions{}) + tx, err := ctrlctx.CurrentTx(ctx) + if err != nil { + return + } + tokensecret := token + if strings.Contains(token, "/") { + tokenparts := strings.Split(token, "/") + if len(tokenparts) >= 3 { + tokensecret = tokenparts[2] + } + } + var exp sql.NullString + var scopes []byte + err = tx.QueryRowxContext(ctx, "select uuid, api_token, expires_at, scopes from api_client_authorizations where api_token=$1", tokensecret).Scan(&resp.UUID, &resp.APIToken, &exp, &scopes) + if err != nil { + return + } + resp.ExpiresAt = exp.String + if len(scopes) > 0 { + err = json.Unmarshal(scopes, &resp.Scopes) + if err != nil { + return resp, fmt.Errorf("unmarshal scopes: %s", err) + } + } + return }