1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
17 "git.arvados.org/arvados.git/lib/controller/rpc"
18 "git.arvados.org/arvados.git/lib/ctrlctx"
19 "git.arvados.org/arvados.git/sdk/go/arvados"
20 "git.arvados.org/arvados.git/sdk/go/auth"
21 "git.arvados.org/arvados.git/sdk/go/httpserver"
24 type loginController interface {
25 Login(ctx context.Context, opts arvados.LoginOptions) (arvados.LoginResponse, error)
26 Logout(ctx context.Context, opts arvados.LogoutOptions) (arvados.LogoutResponse, error)
27 UserAuthenticate(ctx context.Context, options arvados.UserAuthenticateOptions) (arvados.APIClientAuthorization, error)
30 func chooseLoginController(cluster *arvados.Cluster, railsProxy *railsProxy) loginController {
31 wantGoogle := cluster.Login.Google.Enable
32 wantOpenIDConnect := cluster.Login.OpenIDConnect.Enable
33 wantSSO := cluster.Login.SSO.Enable
34 wantPAM := cluster.Login.PAM.Enable
35 wantLDAP := cluster.Login.LDAP.Enable
36 wantTest := cluster.Login.Test.Enable
37 wantLoginCluster := cluster.Login.LoginCluster != "" && cluster.Login.LoginCluster != cluster.ClusterID
39 case 1 != countTrue(wantGoogle, wantOpenIDConnect, wantSSO, wantPAM, wantLDAP, wantTest, wantLoginCluster):
40 return errorLoginController{
41 error: errors.New("configuration problem: exactly one of Login.Google, Login.OpenIDConnect, Login.SSO, Login.PAM, Login.LDAP, Login.Test, or Login.LoginCluster must be set"),
44 return &oidcLoginController{
46 RailsProxy: railsProxy,
47 Issuer: "https://accounts.google.com",
48 ClientID: cluster.Login.Google.ClientID,
49 ClientSecret: cluster.Login.Google.ClientSecret,
50 UseGooglePeopleAPI: cluster.Login.Google.AlternateEmailAddresses,
52 EmailVerifiedClaim: "email_verified",
54 case wantOpenIDConnect:
55 return &oidcLoginController{
57 RailsProxy: railsProxy,
58 Issuer: cluster.Login.OpenIDConnect.Issuer,
59 ClientID: cluster.Login.OpenIDConnect.ClientID,
60 ClientSecret: cluster.Login.OpenIDConnect.ClientSecret,
61 EmailClaim: cluster.Login.OpenIDConnect.EmailClaim,
62 EmailVerifiedClaim: cluster.Login.OpenIDConnect.EmailVerifiedClaim,
63 UsernameClaim: cluster.Login.OpenIDConnect.UsernameClaim,
66 return &ssoLoginController{railsProxy}
68 return &pamLoginController{Cluster: cluster, RailsProxy: railsProxy}
70 return &ldapLoginController{Cluster: cluster, RailsProxy: railsProxy}
72 return &testLoginController{Cluster: cluster, RailsProxy: railsProxy}
73 case wantLoginCluster:
74 return &federatedLoginController{Cluster: cluster}
76 return errorLoginController{
77 error: errors.New("BUG: missing case in login controller setup switch"),
82 func countTrue(vals ...bool) int {
84 for _, val := range vals {
92 // Login and Logout are passed through to the wrapped railsProxy;
93 // UserAuthenticate is rejected.
94 type ssoLoginController struct{ *railsProxy }
96 func (ctrl *ssoLoginController) UserAuthenticate(ctx context.Context, opts arvados.UserAuthenticateOptions) (arvados.APIClientAuthorization, error) {
97 return arvados.APIClientAuthorization{}, httpserver.ErrorWithStatus(errors.New("username/password authentication is not available"), http.StatusBadRequest)
100 type errorLoginController struct{ error }
102 func (ctrl errorLoginController) Login(context.Context, arvados.LoginOptions) (arvados.LoginResponse, error) {
103 return arvados.LoginResponse{}, ctrl.error
105 func (ctrl errorLoginController) Logout(context.Context, arvados.LogoutOptions) (arvados.LogoutResponse, error) {
106 return arvados.LogoutResponse{}, ctrl.error
108 func (ctrl errorLoginController) UserAuthenticate(context.Context, arvados.UserAuthenticateOptions) (arvados.APIClientAuthorization, error) {
109 return arvados.APIClientAuthorization{}, ctrl.error
112 type federatedLoginController struct {
113 Cluster *arvados.Cluster
116 func (ctrl federatedLoginController) Login(context.Context, arvados.LoginOptions) (arvados.LoginResponse, error) {
117 return arvados.LoginResponse{}, httpserver.ErrorWithStatus(errors.New("Should have been redirected to login cluster"), http.StatusBadRequest)
119 func (ctrl federatedLoginController) Logout(_ context.Context, opts arvados.LogoutOptions) (arvados.LogoutResponse, error) {
120 return noopLogout(ctrl.Cluster, opts)
122 func (ctrl federatedLoginController) UserAuthenticate(context.Context, arvados.UserAuthenticateOptions) (arvados.APIClientAuthorization, error) {
123 return arvados.APIClientAuthorization{}, httpserver.ErrorWithStatus(errors.New("username/password authentication is not available"), http.StatusBadRequest)
126 func noopLogout(cluster *arvados.Cluster, opts arvados.LogoutOptions) (arvados.LogoutResponse, error) {
127 target := opts.ReturnTo
129 if cluster.Services.Workbench2.ExternalURL.Host != "" {
130 target = cluster.Services.Workbench2.ExternalURL.String()
132 target = cluster.Services.Workbench1.ExternalURL.String()
135 return arvados.LogoutResponse{RedirectLocation: target}, nil
138 func createAPIClientAuthorization(ctx context.Context, conn *rpc.Conn, rootToken string, authinfo rpc.UserSessionAuthInfo) (resp arvados.APIClientAuthorization, err error) {
139 ctxRoot := auth.NewContext(ctx, &auth.Credentials{Tokens: []string{rootToken}})
140 newsession, err := conn.UserSessionCreate(ctxRoot, rpc.UserSessionCreateOptions{
141 // Send a fake ReturnTo value instead of the caller's
142 // opts.ReturnTo. We won't follow the resulting
143 // redirect target anyway.
144 ReturnTo: ",https://none.invalid",
150 target, err := url.Parse(newsession.RedirectLocation)
154 token := target.Query().Get("api_token")
155 tx, err := ctrlctx.CurrentTx(ctx)
160 if strings.Contains(token, "/") {
161 tokenparts := strings.Split(token, "/")
162 if len(tokenparts) >= 3 {
163 tokensecret = tokenparts[2]
166 var exp sql.NullString
168 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)
172 resp.ExpiresAt = exp.String
174 err = json.Unmarshal(scopes, &resp.Scopes)
176 return resp, fmt.Errorf("unmarshal scopes: %s", err)