@@ -17,6 +17,7 @@ import (
1717)
1818
1919var (
20+ ErrNilAuthorization = goerrors .New ("authorization cannot be nil" )
2021 ErrHashedTokenMismatch = goerrors .New ("HashedToken does not match Token" )
2122 ErrIncorrectToken = goerrors .New ("token is incorrect for authorization" )
2223 ErrNoTokenAvailable = goerrors .New ("no token available for authorization" )
@@ -108,7 +109,7 @@ func (s *Store) transformToken(a *influxdb.Authorization) error {
108109 // Note that even if a.HashedToken is set, we will regenerate it here. This ensures
109110 // that a.HashedToken will be stored using the currently configured hashing algorithm.
110111 if hashedToken , err := s .hasher .Hash (a .Token ); err != nil {
111- return fmt .Errorf ("error hashing token: %w" , err )
112+ return fmt .Errorf ("error hashing token for token %d (%s) : %w" , a . ID , a . Description , err )
112113 } else {
113114 a .HashedToken = hashedToken
114115 }
@@ -122,11 +123,17 @@ func (s *Store) transformToken(a *influxdb.Authorization) error {
122123}
123124
124125// CreateAuthorization takes an Authorization object and saves it in storage using its token
125- // using its token property as an index
126+ // using its token property as an index. The contents of a should be considered invalid if an
127+ // error occurs.
126128func (s * Store ) CreateAuthorization (ctx context.Context , tx kv.Tx , a * influxdb.Authorization ) (retErr error ) {
127129 defer func () {
128130 retErr = errors .ErrInternalServiceError (retErr , errors .WithErrorOp (influxdb .OpCreateAuthorization ))
129131 }()
132+
133+ if a == nil {
134+ return ErrNilAuthorization
135+ }
136+
130137 // if the provided ID is invalid, or already maps to an existing Auth, then generate a new one
131138 if ! a .ID .Valid () {
132139 id , err := s .generateSafeID (ctx , tx , authBucket )
@@ -193,7 +200,7 @@ func (s *Store) validateToken(auth *influxdb.Authorization, token string) (bool,
193200 if auth .HashedToken != "" {
194201 match , err := s .hasher .Match (auth .HashedToken , token )
195202 if err != nil {
196- return false , fmt .Errorf ("error matching hashed token for validation: %w" , err )
203+ return false , fmt .Errorf ("error matching hashed token %d (%s) for validation: %w" , auth . ID , auth . Description , err )
197204 }
198205 return match , nil
199206 }
@@ -437,6 +444,10 @@ func (s *Store) UpdateAuthorization(ctx context.Context, tx kv.Tx, id platform.I
437444 retErr = errors .ErrInternalServiceError (retErr , errors .WithErrorOp (influxdb .OpUpdateAuthorization ))
438445 }()
439446
447+ if a == nil {
448+ return nil , ErrNilAuthorization
449+ }
450+
440451 initialToken := a .Token
441452 initialHashedToken := a .HashedToken
442453
@@ -610,16 +621,28 @@ func (s *Store) authorizationsPredicateFn(f influxdb.AuthorizationFilter) kv.Cur
610621 // but we'll still look at the unhashed Token if it is available.
611622 }
612623 return func (_ , value []byte ) bool {
613- // it is assumed that token never has escaped string data
624+ // Check if "token" matches. It is assumed that token never has escaped string data.
614625 if got , _ , _ , err := jsonparser .Get (value , "token" ); err == nil {
615- return string (got ) == token
626+ if len (got ) > 0 {
627+ return string (got ) == token
628+ }
629+ } else {
630+ return true // predicate must return true on errors
616631 }
632+
633+ // Check if "hashedToken" matches, if applicable.
617634 if len (allHashes ) > 0 {
618635 if got , _ , _ , err := jsonparser .Get (value , "hashedToken" ); err == nil {
619- return slices .Contains (allHashes , string (got ))
636+ if len (got ) > 0 {
637+ return slices .Contains (allHashes , string (got ))
638+ }
639+ } else {
640+ return true // predicate must return true on errors
620641 }
621642 }
622- return true
643+
644+ // No match on "token" or "hashedToken", do not include this record.
645+ return false
623646 }
624647 }
625648
0 commit comments