Skip to content

Commit

Permalink
some refactor of cases for authorization switch
Browse files Browse the repository at this point in the history
Signed-off-by: mteodor <mirko.teodorovic@gmail.com>
  • Loading branch information
mteodor committed Oct 22, 2021
1 parent c6edf82 commit f249f3e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
17 changes: 8 additions & 9 deletions readers/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,15 @@ func authorize(ctx context.Context, r *http.Request, chanID string) (err error)
if token == "" {
return errUnauthorizedAccess
}
if strings.HasPrefix(token, thingToken) {
token = strings.ReplaceAll(token, thingToken, "")
switch {
case strings.HasPrefix(token, thingToken):
token = strings.TrimPrefix(token, thingToken)
if _, err := thingsAuth.CanAccessByKey(ctx, &mainflux.AccessByKeyReq{Token: token, ChanID: chanID}); err != nil {
return errors.Wrap(errUnauthorizedAccess, errThingAccess)
}
return nil
}

if strings.HasPrefix(token, userToken) {
token = strings.ReplaceAll(token, userToken, "")
case strings.HasPrefix(token, userToken):
token = strings.TrimPrefix(token, userToken)
user, err := usersAuth.Identify(ctx, &mainflux.Token{Value: token})
if err != nil {
e, ok := status.FromError(err)
Expand All @@ -250,10 +249,10 @@ func authorize(ctx context.Context, r *http.Request, chanID string) (err error)
return err
}
return nil
}

return errors.Wrap(errUnauthorizedAccess, errWrongToken)
default:
return errors.Wrap(errUnauthorizedAccess, errWrongToken)

}
}

func readBoolValueQuery(r *http.Request, key string) (bool, error) {
Expand Down
4 changes: 1 addition & 3 deletions readers/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

package readers

import (
"errors"
)
import "errors"

const (
// EqualKey represents the equal comparison operator key.
Expand Down

0 comments on commit f249f3e

Please sign in to comment.