1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
15 "git.arvados.org/arvados.git/lib/ctrlctx"
16 "git.arvados.org/arvados.git/sdk/go/arvados"
17 "git.arvados.org/arvados.git/sdk/go/auth"
18 "git.arvados.org/arvados.git/sdk/go/ctxlog"
19 "git.arvados.org/arvados.git/sdk/go/httpserver"
22 func logout(ctx context.Context, cluster *arvados.Cluster, opts arvados.LogoutOptions) (arvados.LogoutResponse, error) {
23 err := expireAPIClientAuthorization(ctx)
25 ctxlog.FromContext(ctx).Errorf("attempting to expire token on logout: %q", err)
26 return arvados.LogoutResponse{}, httpserver.ErrorWithStatus(errors.New("could not expire token on logout"), http.StatusInternalServerError)
29 target := opts.ReturnTo
31 if cluster.Services.Workbench2.ExternalURL.Host != "" {
32 target = cluster.Services.Workbench2.ExternalURL.String()
34 target = cluster.Services.Workbench1.ExternalURL.String()
36 } else if err := validateLoginRedirectTarget(cluster, target); err != nil {
37 return arvados.LogoutResponse{}, httpserver.ErrorWithStatus(fmt.Errorf("invalid return_to parameter: %s", err), http.StatusBadRequest)
39 return arvados.LogoutResponse{RedirectLocation: target}, nil
42 func expireAPIClientAuthorization(ctx context.Context) error {
43 creds, ok := auth.FromContext(ctx)
45 // Tests could be passing empty contexts
46 ctxlog.FromContext(ctx).Debugf("expireAPIClientAuthorization: credentials not found from context")
50 if len(creds.Tokens) == 0 {
51 // Old client may not have provided the token to expire
55 tx, err := ctrlctx.CurrentTx(ctx)
60 token := creds.Tokens[0]
63 if strings.HasPrefix(token, "v2/") {
64 tokenParts := strings.Split(token, "/")
65 if len(tokenParts) >= 3 {
66 tokenUuid = tokenParts[1]
67 tokenSecret = tokenParts[2]
71 var retrievedUuid string
72 err = tx.QueryRowContext(ctx, `SELECT uuid FROM api_client_authorizations WHERE api_token=$1 AND (expires_at IS NULL OR expires_at > current_timestamp AT TIME ZONE 'UTC') LIMIT 1`, tokenSecret).Scan(&retrievedUuid)
73 if err == sql.ErrNoRows {
74 ctxlog.FromContext(ctx).Debugf("expireAPIClientAuthorization(%s): not found in database", token)
76 } else if err != nil {
77 ctxlog.FromContext(ctx).WithError(err).Debugf("expireAPIClientAuthorization(%s): database error", token)
81 if tokenUuid != "" && retrievedUuid != tokenUuid {
82 // secret part matches, but UUID doesn't -- somewhat surprising
83 ctxlog.FromContext(ctx).Debugf("expireAPIClientAuthorization(%s): secret part found, but with different UUID: %s", tokenSecret, retrievedUuid)
87 res, err := tx.ExecContext(ctx, "UPDATE api_client_authorizations SET expires_at=current_timestamp AT TIME ZONE 'UTC' WHERE uuid=$1", retrievedUuid)
92 rows, err := res.RowsAffected()
97 ctxlog.FromContext(ctx).Debugf("expireAPIClientAuthorization(%s): no rows were updated", tokenSecret)
98 return fmt.Errorf("couldn't expire provided token")
100 ctxlog.FromContext(ctx).Debugf("expireAPIClientAuthorization(%s): multiple (%d) rows updated", tokenSecret, rows)
102 ctxlog.FromContext(ctx).Debugf("expireAPIClientAuthorization(%s): ok", tokenSecret)