@@ -73,7 +73,7 @@ func (s *Store) encodeAuthorization(a *influxdb.Authorization) ([]byte, error) {
7373 // user-facing output. The empty string signals that the plaintext token is not available and that
7474 // the hashed token should be used instead.
7575 redactedAuth := * a
76- redactedAuth .Token = ""
76+ redactedAuth .ClearToken ()
7777 a = & redactedAuth
7878 }
7979 if d , err := json .Marshal (a ); err == nil {
@@ -105,7 +105,7 @@ func decodeAuthorization(b []byte, a *influxdb.Authorization) error {
105105// error is returned.
106106func (s * Store ) transformToken (a * influxdb.Authorization ) error {
107107 // Verify Token and HashedToken match if both are set.
108- if a .Token != "" && a . HashedToken != "" {
108+ if a .BothTokensSet () {
109109 match , err := s .hasher .Match (a .HashedToken , a .Token )
110110 if err != nil {
111111 return fmt .Errorf ("transformToken: error matching tokens: %w" , err )
@@ -115,7 +115,7 @@ func (s *Store) transformToken(a *influxdb.Authorization) error {
115115 }
116116 }
117117
118- if a .Token != "" {
118+ if a .IsTokenSet () {
119119 if s .useHashedTokens {
120120 // Need to generate HashedToken from Token. Redaction of the hashed token takes
121121 // place when the record is written to the KV store. In some cases the client
@@ -130,7 +130,7 @@ func (s *Store) transformToken(a *influxdb.Authorization) error {
130130 }
131131 } else {
132132 // Token hashing disabled, a.Token is available, clear a.HashedToken if set.
133- a .HashedToken = ""
133+ a .ClearHashedToken ()
134134 }
135135 }
136136
@@ -208,11 +208,11 @@ func (s *Store) GetAuthorizationByID(ctx context.Context, tx kv.Tx, id platform.
208208// compared first. Otherwise, auth.HashedToken is used to verify token. If neither field in auth is set, then
209209// the comparison fails.
210210func (s * Store ) validateToken (auth * influxdb.Authorization , token string ) (bool , error ) {
211- if auth .Token != "" {
211+ if auth .IsTokenSet () {
212212 return subtle .ConstantTimeCompare ([]byte (auth .Token ), []byte (token )) == 1 , nil
213213 }
214214
215- if auth .HashedToken != "" {
215+ if auth .IsHashedTokenSet () {
216216 match , err := s .hasher .Match (auth .HashedToken , token )
217217 if err != nil {
218218 return false , fmt .Errorf ("error matching hashed token %d (%s) for validation: %w" , auth .ID , auth .Description , err )
@@ -380,6 +380,11 @@ func (s *Store) commitAuthorization(ctx context.Context, tx kv.Tx, a *influxdb.A
380380 return errors .ErrInternalServiceError (err , errors .WithErrorCode (errors .EInternal ))
381381 }
382382
383+ // Sanity check that a is actually set. Shouldn't be possible during normal operation.
384+ if a .NoTokensSet () {
385+ return fmt .Errorf ("commitAuthorization: %w" , ErrNoTokenAvailable )
386+ }
387+
383388 v , err := s .encodeAuthorization (a )
384389 if err != nil {
385390 return errors .ErrInternalServiceError (err , errors .WithErrorCode (errors .EInvalid ))
@@ -390,7 +395,7 @@ func (s *Store) commitAuthorization(ctx context.Context, tx kv.Tx, a *influxdb.A
390395 return errors .ErrInternalServiceError (err , errors .WithErrorCode (errors .ENotFound ))
391396 }
392397
393- if ! s .useHashedTokens && a .Token != "" {
398+ if ! s .useHashedTokens && a .IsTokenSet () {
394399 idx , err := authIndexBucket (tx )
395400 if err != nil {
396401 return errors .ErrInternalServiceError (err , errors .WithErrorCode (errors .EInternal ))
@@ -401,7 +406,11 @@ func (s *Store) commitAuthorization(ctx context.Context, tx kv.Tx, a *influxdb.A
401406 }
402407 }
403408
404- if a .HashedToken != "" {
409+ // If we have a hashed token, we need to add it to the index even if hashed tokens are not
410+ // available. This is because if hashed tokens are enabled and then disabled, we will
411+ // only have hashed tokens available for some authorization records. They would be unusable
412+ // if we did not maintain their hashed indices.
413+ if a .IsHashedTokenSet () {
405414 idx , err := hashedAuthIndexBucket (tx )
406415 // Don't ignore a missing index here, we want an error.
407416 if err != nil {
@@ -438,13 +447,13 @@ func (s *Store) deleteIndices(ctx context.Context, tx kv.Tx, token, hashedToken
438447 return err
439448 }
440449
441- if token != "" {
450+ if influxdb . IsAuthTokenSet ( token ) {
442451 if err := authIdx .Delete ([]byte (token )); err != nil {
443452 return fmt .Errorf ("deleteIndices: error deleting from authIndex: %w" , err )
444453 }
445454 }
446455
447- if hashedToken != "" {
456+ if influxdb . IsAuthTokenSet ( hashedToken ) {
448457 if err := hashedAuthIdx .Delete ([]byte (hashedToken )); err != nil {
449458 return fmt .Errorf ("deleteIndices: error deleting from hashedAuthIndex: %w" , err )
450459 }
@@ -472,12 +481,12 @@ func (s *Store) UpdateAuthorization(ctx context.Context, tx kv.Tx, id platform.I
472481
473482 // Delete dangling indices from old raw tokens or hashed tokens.
474483 var removedToken string
475- if initialToken != "" && (a .Token != initialToken || s .useHashedTokens ) {
484+ if influxdb . IsAuthTokenSet ( initialToken ) && (a .Token != initialToken || s .useHashedTokens ) {
476485 removedToken = initialToken
477486 }
478487
479488 var removedHashedToken string
480- if initialHashedToken != "" && a .HashedToken != initialHashedToken {
489+ if influxdb . IsAuthTokenSet ( initialHashedToken ) && a .HashedToken != initialHashedToken {
481490 removedHashedToken = initialHashedToken
482491 }
483492
@@ -535,7 +544,7 @@ func (s *Store) uniqueAuthTokenByIndex(ctx context.Context, tx kv.Tx, index, key
535544
536545func (s * Store ) uniqueAuthToken (ctx context.Context , tx kv.Tx , a * influxdb.Authorization ) error {
537546 // Check if the raw token is unique.
538- if a .Token != "" {
547+ if a .IsTokenSet () {
539548 if err := s .uniqueAuthTokenByIndex (ctx , tx , authIndexName , authIndexKey (a .Token )); err != nil {
540549 return err
541550 }
@@ -544,10 +553,10 @@ func (s *Store) uniqueAuthToken(ctx context.Context, tx kv.Tx, a *influxdb.Autho
544553 // If Token is available, check for the uniqueness of the hashed version of Token using all
545554 // potential hashing schemes. If HashedToken was directly given, we must also check for it.
546555 allHashedTokens := make ([]string , 0 , s .hasher .AllHashesCount ()+ 1 )
547- if a .HashedToken != "" {
556+ if a .IsHashedTokenSet () {
548557 allHashedTokens = append (allHashedTokens , a .HashedToken )
549558 }
550- if a .Token != "" {
559+ if a .IsTokenSet () {
551560 allRawHashes , err := s .hasher .AllHashes (a .Token )
552561 if err != nil {
553562 return err
0 commit comments