Skip to content

Commit

Permalink
Merge pull request #4 from insighio/CNSL-12-support-api-keys
Browse files Browse the repository at this point in the history
CNSL-12 support api keys
  • Loading branch information
kostasbalampekos committed Dec 11, 2023
2 parents b00d321 + f87e8cf commit f5b5c65
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion auth/api/http/keys/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func issueEndpoint(svc auth.Service) endpoint.Endpoint {
newKey.ExpiresAt = exp
}

key, secret, err := svc.Issue(ctx, req.token, newKey)
key, secret, err := svc.Issue(ctx, req.Token, newKey)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions auth/api/http/keys/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

type issueKeyReq struct {
token string
Token string `json:"token,omitempty"`
Type uint32 `json:"type,omitempty"`
Duration time.Duration `json:"duration,omitempty"`
}
Expand All @@ -20,7 +20,7 @@ func (req issueKeyReq) validate() error {
if req.Type == auth.UserKey {
return nil
}
if req.token == "" || (req.Type != auth.APIKey) {
if req.Token == "" || (req.Type != auth.APIKey) {
return auth.ErrMalformedEntity
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion auth/api/http/keys/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func decodeIssue(_ context.Context, r *http.Request) (interface{}, error) {
return nil, errUnsupportedContentType
}
req := issueKeyReq{
token: r.Header.Get("Authorization"),
Token: r.Header.Get("Authorization"),
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
return nil, errors.Wrap(auth.ErrMalformedEntity, err)
Expand Down
1 change: 1 addition & 0 deletions auth/api/logging.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0

//go:build !test
// +build !test

package api
Expand Down
8 changes: 7 additions & 1 deletion auth/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,13 @@ func (svc service) Identify(ctx context.Context, token string) (Identity, error)
}

switch key.Type {
case APIKey, RecoveryKey, UserKey, EmailVerificationKey:
case RecoveryKey, UserKey, EmailVerificationKey:
return Identity{ID: key.IssuerID, Email: key.Subject}, nil
case APIKey:
_, err := svc.keys.Retrieve(context.TODO(), key.IssuerID, key.ID)
if err != nil {
return Identity{}, ErrUnauthorizedAccess
}
return Identity{ID: key.IssuerID, Email: key.Subject}, nil
default:
return Identity{}, ErrUnauthorizedAccess
Expand Down

0 comments on commit f5b5c65

Please sign in to comment.