16159: Attempts to expire the user's token getting it from the context. (WIP)
authorLucas Di Pentima <lucas@di-pentima.com.ar>
Mon, 29 Mar 2021 19:12:05 +0000 (16:12 -0300)
committerLucas Di Pentima <lucas@di-pentima.com.ar>
Mon, 29 Mar 2021 19:12:05 +0000 (16:12 -0300)
To avoid making a request to railsAPI, the user's token is retrieved from the
context.

Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima <lucas@di-pentima.com.ar>

lib/controller/localdb/logout.go

index 600527405c39c773438befc63fc061c9ce1f21b1..417fdc06679a6f3cc33ab5bb21d0b6d4fa68ed13 100644 (file)
@@ -6,25 +6,51 @@ package localdb
 
 import (
        "context"
+       "errors"
+       "fmt"
+       "strings"
 
        "git.arvados.org/arvados.git/lib/ctrlctx"
-       "git.arvados.org/arvados.git/sdk/go/arvados"
+       "git.arvados.org/arvados.git/sdk/go/auth"
 )
 
 func (conn *Conn) ExpireAPIClientAuthorization(ctx context.Context) error {
-       aca, err := conn.railsProxy.APIClientAuthorizationCurrent(ctx, arvados.GetOptions{})
+       creds, ok := auth.FromContext(ctx)
+
+       if !ok {
+               return errors.New("credentials not found from context")
+       }
+
+       if len(creds.Tokens) < 1 {
+               return errors.New("no tokens found to expire")
+       }
+
+       token := creds.Tokens[0]
+       tokensecret := token
+       if strings.Contains(token, "/") {
+               tokenparts := strings.Split(token, "/")
+               if len(tokenparts) >= 3 {
+                       tokensecret = tokenparts[2]
+               }
+       }
+
+       tx, err := ctrlctx.CurrentTx(ctx)
        if err != nil {
                return err
        }
-       tx, err := ctrlctx.CurrentTx(ctx)
+
+       res, err := tx.ExecContext(ctx, "UPDATE api_client_authorizations SET expires_at=current_timestamp WHERE api_token=$1", tokensecret)
        if err != nil {
                return err
        }
 
-       err = tx.QueryRowxContext(ctx, "UPDATE api_client_authorizations SET expires_at=current_timestamp WHERE uuid=$1", aca.UUID).Err()
+       rows, err := res.RowsAffected()
        if err != nil {
                return err
        }
+       if rows != 1 {
+               return fmt.Errorf("token expiration affected rows: %d -  token: %s", rows, token)
+       }
 
        return nil
 }