Skip to content

Commit

Permalink
fix: PR feedback to move defer logic to its own function for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartcarnie committed Oct 30, 2020
1 parent ce80340 commit 13b3bb8
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions v1/authorization/authorizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,8 @@ type Authorizer struct {
// Authorize returns an influxdb.Authorization if c can be verified; otherwise, an error.
// influxdb.ErrCredentialsUnauthorized will be returned if the credentials are invalid.
func (v *Authorizer) Authorize(ctx context.Context, c influxdb.CredentialsV1) (auth *influxdb.Authorization, err error) {
// the defer function provides the following guarantees:
// * the authorization token status is active and
// * the user status is active
defer func() {
if err != nil {
return
}

if auth == nil {
return
}

if auth.Status != influxdb.Active {
auth, err = nil, influxdb.ErrCredentialsUnauthorized
return
}

// check the user is still active
if user, userErr := v.User.FindUserByID(ctx, auth.UserID); err != nil {
auth, err = nil, v.normalizeError(userErr)
return
} else if user == nil || user.Status != influxdb.Active {
auth, err = nil, influxdb.ErrCredentialsUnauthorized
return
}
auth, err = v.checkAuthError(ctx, auth, err)
}()

switch c.Scheme {
Expand All @@ -86,6 +63,29 @@ func (v *Authorizer) Authorize(ctx context.Context, c influxdb.CredentialsV1) (a
}
}

func (v *Authorizer) checkAuthError(ctx context.Context, auth *influxdb.Authorization, err error) (*influxdb.Authorization, error) {
if err != nil {
return nil, err
}

if auth == nil {
return nil, influxdb.ErrCredentialsUnauthorized
}

if auth.Status != influxdb.Active {
return nil, influxdb.ErrCredentialsUnauthorized
}

// check the user is still active
if user, userErr := v.User.FindUserByID(ctx, auth.UserID); userErr != nil {
return nil, v.normalizeError(userErr)
} else if user == nil || user.Status != influxdb.Active {
return nil, influxdb.ErrCredentialsUnauthorized
}

return auth, nil
}

func (v *Authorizer) tryV1Authorization(ctx context.Context, c influxdb.CredentialsV1) (auth *influxdb.Authorization, err error) {
auth, err = v.AuthV1.FindAuthorizationByToken(ctx, c.Username)
if err != nil {
Expand Down

0 comments on commit 13b3bb8

Please sign in to comment.