Fix 2.4.2 upgrade notes formatting refs #19330
[arvados.git] / lib / controller / localdb / logout.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package localdb
6
7 import (
8         "context"
9         "database/sql"
10         "errors"
11         "fmt"
12         "net/http"
13         "strings"
14
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"
20 )
21
22 func logout(ctx context.Context, cluster *arvados.Cluster, opts arvados.LogoutOptions) (arvados.LogoutResponse, error) {
23         err := expireAPIClientAuthorization(ctx)
24         if err != nil {
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)
27         }
28
29         target := opts.ReturnTo
30         if target == "" {
31                 if cluster.Services.Workbench2.ExternalURL.Host != "" {
32                         target = cluster.Services.Workbench2.ExternalURL.String()
33                 } else {
34                         target = cluster.Services.Workbench1.ExternalURL.String()
35                 }
36         }
37         return arvados.LogoutResponse{RedirectLocation: target}, nil
38 }
39
40 func expireAPIClientAuthorization(ctx context.Context) error {
41         creds, ok := auth.FromContext(ctx)
42         if !ok {
43                 // Tests could be passing empty contexts
44                 ctxlog.FromContext(ctx).Debugf("expireAPIClientAuthorization: credentials not found from context")
45                 return nil
46         }
47
48         if len(creds.Tokens) == 0 {
49                 // Old client may not have provided the token to expire
50                 return nil
51         }
52
53         tx, err := ctrlctx.CurrentTx(ctx)
54         if err != nil {
55                 return err
56         }
57
58         token := creds.Tokens[0]
59         tokenSecret := token
60         var tokenUuid string
61         if strings.HasPrefix(token, "v2/") {
62                 tokenParts := strings.Split(token, "/")
63                 if len(tokenParts) >= 3 {
64                         tokenUuid = tokenParts[1]
65                         tokenSecret = tokenParts[2]
66                 }
67         }
68
69         var retrievedUuid string
70         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)
71         if err == sql.ErrNoRows {
72                 ctxlog.FromContext(ctx).Debugf("expireAPIClientAuthorization(%s): not found in database", token)
73                 return nil
74         } else if err != nil {
75                 ctxlog.FromContext(ctx).WithError(err).Debugf("expireAPIClientAuthorization(%s): database error", token)
76                 return err
77         }
78
79         if tokenUuid != "" && retrievedUuid != tokenUuid {
80                 // secret part matches, but UUID doesn't -- somewhat surprising
81                 ctxlog.FromContext(ctx).Debugf("expireAPIClientAuthorization(%s): secret part found, but with different UUID: %s", tokenSecret, retrievedUuid)
82                 return nil
83         }
84
85         res, err := tx.ExecContext(ctx, "UPDATE api_client_authorizations SET expires_at=current_timestamp AT TIME ZONE 'UTC' WHERE uuid=$1", retrievedUuid)
86         if err != nil {
87                 return err
88         }
89
90         rows, err := res.RowsAffected()
91         if err != nil {
92                 return err
93         }
94         if rows == 0 {
95                 ctxlog.FromContext(ctx).Debugf("expireAPIClientAuthorization(%s): no rows were updated", tokenSecret)
96                 return fmt.Errorf("couldn't expire provided token")
97         } else if rows > 1 {
98                 ctxlog.FromContext(ctx).Debugf("expireAPIClientAuthorization(%s): multiple (%d) rows updated", tokenSecret, rows)
99         } else {
100                 ctxlog.FromContext(ctx).Debugf("expireAPIClientAuthorization(%s): ok", tokenSecret)
101         }
102
103         return nil
104 }