1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
14 "git.arvados.org/arvados.git/lib/ctrlctx"
15 "git.arvados.org/arvados.git/sdk/go/auth"
16 "git.arvados.org/arvados.git/sdk/go/ctxlog"
19 func (conn *Conn) expireAPIClientAuthorization(ctx context.Context) error {
20 creds, ok := auth.FromContext(ctx)
22 return errors.New("credentials not found from context")
25 if len(creds.Tokens) == 0 {
26 // Old client may not have provided the token to expire
30 tx, err := ctrlctx.CurrentTx(ctx)
35 token := creds.Tokens[0]
38 if strings.HasPrefix(token, "v2/") {
39 tokenParts := strings.Split(token, "/")
40 if len(tokenParts) >= 3 {
41 tokenUuid = tokenParts[1]
42 tokenSecret = tokenParts[2]
46 var retrievedUuid string
47 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)
48 if err == sql.ErrNoRows {
49 ctxlog.FromContext(ctx).Debugf("expireAPIClientAuthorization(%s): not found in database", token)
51 } else if err != nil {
52 ctxlog.FromContext(ctx).WithError(err).Debugf("expireAPIClientAuthorization(%s): database error", token)
55 if tokenUuid != "" && retrievedUuid != tokenUuid {
56 // secret part matches, but UUID doesn't -- somewhat surprising
57 ctxlog.FromContext(ctx).Debugf("expireAPIClientAuthorization(%s): secret part found, but with different UUID: %s", tokenSecret, retrievedUuid)
61 res, err := tx.ExecContext(ctx, "UPDATE api_client_authorizations SET expires_at=current_timestamp AT TIME ZONE 'UTC' WHERE (expires_at IS NULL OR expires_at > current_timestamp AT TIME ZONE 'UTC' AND api_token=$1)", tokenSecret)
66 rows, err := res.RowsAffected()
71 ctxlog.FromContext(ctx).Debugf("expireAPIClientAuthorization(%s): no rows were updated", tokenSecret)
72 return fmt.Errorf("couldn't expire provided token")
74 ctxlog.FromContext(ctx).Debugf("expireAPIClientAuthorization(%s): multiple (%d) rows updated", tokenSecret, rows)
76 ctxlog.FromContext(ctx).Debugf("expireAPIClientAuthorization(%s): ok", tokenSecret)