Skip to content

Commit

Permalink
NOISSUE - Reformat Things and Users Policies Endpoint (absmach#1831)
Browse files Browse the repository at this point in the history
* Reformat Policies Enpoint to Take Sub Obj

Signed-off-by: rodneyosodo <[email protected]>

* Add Redirect to `policies`

Signed-off-by: rodneyosodo <[email protected]>

* Make Delete Endpoint not to Contain Body

Signed-off-by: rodneyosodo <[email protected]>

* Remove gRPC unused functions

Signed-off-by: rodneyosodo <[email protected]>

* Remove Redirect

Signed-off-by: rodneyosodo <[email protected]>

* Update CLI

Signed-off-by: rodneyosodo <[email protected]>

* Use Switch Statement

Signed-off-by: rodneyosodo <[email protected]>

* Uncomment Commented Parts

Signed-off-by: rodneyosodo <[email protected]>

* Add Empty Line

Signed-off-by: rodneyosodo <[email protected]>

* Remove Unused gRPC Req and Resp

Signed-off-by: rodneyosodo <[email protected]>

* Fix Listing of Policies

Signed-off-by: rodneyosodo <[email protected]>

* Rename Authorize Functions For Users and Things Service

Signed-off-by: rodneyosodo <[email protected]>

* Add Authorize To CLI

Signed-off-by: rodneyosodo <[email protected]>

---------

Signed-off-by: rodneyosodo <[email protected]>
  • Loading branch information
rodneyosodo authored Jul 28, 2023
1 parent 7758f42 commit 7cccba9
Show file tree
Hide file tree
Showing 77 changed files with 1,118 additions and 2,157 deletions.
1 change: 1 addition & 0 deletions bootstrap/api/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ func newThingsServer(csvc clients.Service, gsvc groups.Service, psvc tpolicies.S
capi.MakeHandler(csvc, mux, logger, instanceID)
gapi.MakeHandler(gsvc, mux, logger)
papi.MakeHandler(csvc, psvc, mux, logger)

return httptest.NewServer(mux)
}

Expand Down
6 changes: 3 additions & 3 deletions bootstrap/mocks/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (svc *mainfluxChannels) CreateGroups(ctx context.Context, token string, chs
svc.mu.Lock()
defer svc.mu.Unlock()

userID, err := svc.auth.Identify(ctx, &upolicies.Token{Value: token})
userID, err := svc.auth.Identify(ctx, &upolicies.IdentifyReq{Token: token})
if err != nil {
return []mfgroups.Group{}, errors.ErrAuthentication
}
Expand Down Expand Up @@ -74,7 +74,7 @@ func (svc *mainfluxChannels) EnableGroup(ctx context.Context, token, id string)
svc.mu.Lock()
defer svc.mu.Unlock()

userID, err := svc.auth.Identify(ctx, &upolicies.Token{Value: token})
userID, err := svc.auth.Identify(ctx, &upolicies.IdentifyReq{Token: token})
if err != nil {
return mfgroups.Group{}, errors.ErrAuthentication
}
Expand All @@ -93,7 +93,7 @@ func (svc *mainfluxChannels) DisableGroup(ctx context.Context, token, id string)
svc.mu.Lock()
defer svc.mu.Unlock()

userID, err := svc.auth.Identify(ctx, &upolicies.Token{Value: token})
userID, err := svc.auth.Identify(ctx, &upolicies.IdentifyReq{Token: token})
if err != nil {
return mfgroups.Group{}, errors.ErrAuthentication
}
Expand Down
4 changes: 2 additions & 2 deletions bootstrap/mocks/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (svc *mainfluxPolicies) AddPolicy(ctx context.Context, token string, p tpol
svc.mu.Lock()
defer svc.mu.Unlock()

if _, err := svc.auth.Identify(ctx, &upolicies.Token{Value: token}); err != nil {
if _, err := svc.auth.Identify(ctx, &upolicies.IdentifyReq{Token: token}); err != nil {
return tpolicies.Policy{}, errors.ErrAuthentication
}
svc.connections[fmt.Sprintf("%s:%s", p.Subject, p.Object)] = p
Expand All @@ -46,7 +46,7 @@ func (svc *mainfluxPolicies) DeletePolicy(ctx context.Context, token string, p t
svc.mu.Lock()
defer svc.mu.Unlock()

if _, err := svc.auth.Identify(ctx, &upolicies.Token{Value: token}); err != nil {
if _, err := svc.auth.Identify(ctx, &upolicies.IdentifyReq{Token: token}); err != nil {
return errors.ErrAuthentication
}

Expand Down
12 changes: 6 additions & 6 deletions bootstrap/mocks/things.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ func NewThingsService(things map[string]mfclients.Client, auth upolicies.AuthSer
}
}

func (svc *mainfluxThings) CreateThings(ctx context.Context, owner string, ths ...mfclients.Client) ([]mfclients.Client, error) {
func (svc *mainfluxThings) CreateThings(ctx context.Context, token string, ths ...mfclients.Client) ([]mfclients.Client, error) {
svc.mu.Lock()
defer svc.mu.Unlock()

userID, err := svc.auth.Identify(ctx, &upolicies.Token{Value: owner})
userID, err := svc.auth.Identify(ctx, &upolicies.IdentifyReq{Token: token})
if err != nil {
return []mfclients.Client{}, errors.ErrAuthentication
}
Expand All @@ -51,11 +51,11 @@ func (svc *mainfluxThings) CreateThings(ctx context.Context, owner string, ths .
return ths, nil
}

func (svc *mainfluxThings) ViewClient(ctx context.Context, owner, id string) (mfclients.Client, error) {
func (svc *mainfluxThings) ViewClient(ctx context.Context, token, id string) (mfclients.Client, error) {
svc.mu.Lock()
defer svc.mu.Unlock()

userID, err := svc.auth.Identify(ctx, &upolicies.Token{Value: owner})
userID, err := svc.auth.Identify(ctx, &upolicies.IdentifyReq{Token: token})
if err != nil {
return mfclients.Client{}, errors.ErrAuthentication
}
Expand All @@ -72,7 +72,7 @@ func (svc *mainfluxThings) EnableClient(ctx context.Context, token, id string) (
svc.mu.Lock()
defer svc.mu.Unlock()

userID, err := svc.auth.Identify(ctx, &upolicies.Token{Value: token})
userID, err := svc.auth.Identify(ctx, &upolicies.IdentifyReq{Token: token})
if err != nil {
return mfclients.Client{}, errors.ErrAuthentication
}
Expand All @@ -91,7 +91,7 @@ func (svc *mainfluxThings) DisableClient(ctx context.Context, token, id string)
svc.mu.Lock()
defer svc.mu.Unlock()

userID, err := svc.auth.Identify(ctx, &upolicies.Token{Value: token})
userID, err := svc.auth.Identify(ctx, &upolicies.IdentifyReq{Token: token})
if err != nil {
return mfclients.Client{}, errors.ErrAuthentication
}
Expand Down
23 changes: 3 additions & 20 deletions bootstrap/mocks/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,13 @@ func NewAuthClient(users map[string]string) policies.AuthServiceClient {
return &serviceMock{users}
}

func (svc serviceMock) Identify(ctx context.Context, in *policies.Token, opts ...grpc.CallOption) (*policies.UserIdentity, error) {
if id, ok := svc.users[in.GetValue()]; ok {
return &policies.UserIdentity{Id: id}, nil
}
return nil, errors.ErrAuthentication
}

func (svc serviceMock) Issue(ctx context.Context, in *policies.IssueReq, opts ...grpc.CallOption) (*policies.Token, error) {
if id, ok := svc.users[in.GetEmail()]; ok {
return &policies.Token{Value: id}, nil
func (svc serviceMock) Identify(ctx context.Context, req *policies.IdentifyReq, opts ...grpc.CallOption) (*policies.IdentifyRes, error) {
if id, ok := svc.users[req.GetToken()]; ok {
return &policies.IdentifyRes{Id: id}, nil
}
return nil, errors.ErrAuthentication
}

func (svc serviceMock) Authorize(ctx context.Context, req *policies.AuthorizeReq, _ ...grpc.CallOption) (r *policies.AuthorizeRes, err error) {
panic("not implemented")
}

func (svc serviceMock) AddPolicy(ctx context.Context, req *policies.AddPolicyReq, _ ...grpc.CallOption) (r *policies.AddPolicyRes, err error) {
panic("not implemented")
}
func (svc serviceMock) DeletePolicy(ctx context.Context, req *policies.DeletePolicyReq, _ ...grpc.CallOption) (r *policies.DeletePolicyRes, err error) {
panic("not implemented")
}
func (svc serviceMock) ListPolicies(ctx context.Context, req *policies.ListPoliciesReq, _ ...grpc.CallOption) (r *policies.ListPoliciesRes, err error) {
panic("not implemented")
}
2 changes: 1 addition & 1 deletion bootstrap/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func (bs bootstrapService) identify(ctx context.Context, token string) (string,
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()

res, err := bs.auth.Identify(ctx, &policies.Token{Value: token})
res, err := bs.auth.Identify(ctx, &policies.IdentifyReq{Token: token})
if err != nil {
return "", errors.ErrAuthentication
}
Expand Down
1 change: 1 addition & 0 deletions bootstrap/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func newThingsServer(csvc clients.Service, gsvc groups.Service, psvc tpolicies.S
capi.MakeHandler(csvc, mux, logger, instanceID)
gapi.MakeHandler(gsvc, mux, logger)
papi.MakeHandler(csvc, psvc, mux, logger)

return httptest.NewServer(mux)
}

Expand Down
6 changes: 3 additions & 3 deletions certs/mocks/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (svc *mainfluxChannels) CreateGroups(ctx context.Context, token string, chs
svc.mu.Lock()
defer svc.mu.Unlock()

userID, err := svc.auth.Identify(ctx, &upolicies.Token{Value: token})
userID, err := svc.auth.Identify(ctx, &upolicies.IdentifyReq{Token: token})
if err != nil {
return []mfgroups.Group{}, errors.ErrAuthentication
}
Expand Down Expand Up @@ -74,7 +74,7 @@ func (svc *mainfluxChannels) EnableGroup(ctx context.Context, token, id string)
svc.mu.Lock()
defer svc.mu.Unlock()

userID, err := svc.auth.Identify(ctx, &upolicies.Token{Value: token})
userID, err := svc.auth.Identify(ctx, &upolicies.IdentifyReq{Token: token})
if err != nil {
return mfgroups.Group{}, errors.ErrAuthentication
}
Expand All @@ -93,7 +93,7 @@ func (svc *mainfluxChannels) DisableGroup(ctx context.Context, token, id string)
svc.mu.Lock()
defer svc.mu.Unlock()

userID, err := svc.auth.Identify(ctx, &upolicies.Token{Value: token})
userID, err := svc.auth.Identify(ctx, &upolicies.IdentifyReq{Token: token})
if err != nil {
return mfgroups.Group{}, errors.ErrAuthentication
}
Expand Down
4 changes: 2 additions & 2 deletions certs/mocks/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (svc *mainfluxPolicies) AddPolicy(ctx context.Context, token string, p tpol
svc.mu.Lock()
defer svc.mu.Unlock()

if _, err := svc.auth.Identify(ctx, &upolicies.Token{Value: token}); err != nil {
if _, err := svc.auth.Identify(ctx, &upolicies.IdentifyReq{Token: token}); err != nil {
return tpolicies.Policy{}, errors.ErrAuthentication
}
svc.connections[fmt.Sprintf("%s:%s", p.Subject, p.Object)] = p
Expand All @@ -46,7 +46,7 @@ func (svc *mainfluxPolicies) DeletePolicy(ctx context.Context, token string, p t
svc.mu.Lock()
defer svc.mu.Unlock()

if _, err := svc.auth.Identify(ctx, &upolicies.Token{Value: token}); err != nil {
if _, err := svc.auth.Identify(ctx, &upolicies.IdentifyReq{Token: token}); err != nil {
return errors.ErrAuthentication
}

Expand Down
12 changes: 6 additions & 6 deletions certs/mocks/things.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ func NewThingsService(things map[string]mfclients.Client, auth upolicies.AuthSer
}
}

func (svc *mainfluxThings) CreateThings(ctx context.Context, owner string, ths ...mfclients.Client) ([]mfclients.Client, error) {
func (svc *mainfluxThings) CreateThings(ctx context.Context, token string, ths ...mfclients.Client) ([]mfclients.Client, error) {
svc.mu.Lock()
defer svc.mu.Unlock()

userID, err := svc.auth.Identify(ctx, &upolicies.Token{Value: owner})
userID, err := svc.auth.Identify(ctx, &upolicies.IdentifyReq{Token: token})
if err != nil {
return []mfclients.Client{}, errors.ErrAuthentication
}
Expand All @@ -51,11 +51,11 @@ func (svc *mainfluxThings) CreateThings(ctx context.Context, owner string, ths .
return ths, nil
}

func (svc *mainfluxThings) ViewClient(ctx context.Context, owner, id string) (mfclients.Client, error) {
func (svc *mainfluxThings) ViewClient(ctx context.Context, token, id string) (mfclients.Client, error) {
svc.mu.Lock()
defer svc.mu.Unlock()

userID, err := svc.auth.Identify(ctx, &upolicies.Token{Value: owner})
userID, err := svc.auth.Identify(ctx, &upolicies.IdentifyReq{Token: token})
if err != nil {
return mfclients.Client{}, errors.ErrAuthentication
}
Expand All @@ -72,7 +72,7 @@ func (svc *mainfluxThings) EnableClient(ctx context.Context, token, id string) (
svc.mu.Lock()
defer svc.mu.Unlock()

userID, err := svc.auth.Identify(ctx, &upolicies.Token{Value: token})
userID, err := svc.auth.Identify(ctx, &upolicies.IdentifyReq{Token: token})
if err != nil {
return mfclients.Client{}, errors.ErrAuthentication
}
Expand All @@ -91,7 +91,7 @@ func (svc *mainfluxThings) DisableClient(ctx context.Context, token, id string)
svc.mu.Lock()
defer svc.mu.Unlock()

userID, err := svc.auth.Identify(ctx, &upolicies.Token{Value: token})
userID, err := svc.auth.Identify(ctx, &upolicies.IdentifyReq{Token: token})
if err != nil {
return mfclients.Client{}, errors.ErrAuthentication
}
Expand Down
10 changes: 5 additions & 5 deletions certs/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ type Cert struct {
}

func (cs *certsService) IssueCert(ctx context.Context, token, thingID string, ttl string) (Cert, error) {
owner, err := cs.auth.Identify(ctx, &policies.Token{Value: token})
owner, err := cs.auth.Identify(ctx, &policies.IdentifyReq{Token: token})
if err != nil {
return Cert{}, err
}
Expand Down Expand Up @@ -113,7 +113,7 @@ func (cs *certsService) IssueCert(ctx context.Context, token, thingID string, tt

func (cs *certsService) RevokeCert(ctx context.Context, token, thingID string) (Revoke, error) {
var revoke Revoke
u, err := cs.auth.Identify(ctx, &policies.Token{Value: token})
u, err := cs.auth.Identify(ctx, &policies.IdentifyReq{Token: token})
if err != nil {
return revoke, err
}
Expand Down Expand Up @@ -144,7 +144,7 @@ func (cs *certsService) RevokeCert(ctx context.Context, token, thingID string) (
}

func (cs *certsService) ListCerts(ctx context.Context, token, thingID string, offset, limit uint64) (Page, error) {
u, err := cs.auth.Identify(ctx, &policies.Token{Value: token})
u, err := cs.auth.Identify(ctx, &policies.IdentifyReq{Token: token})
if err != nil {
return Page{}, err
}
Expand All @@ -167,7 +167,7 @@ func (cs *certsService) ListCerts(ctx context.Context, token, thingID string, of
}

func (cs *certsService) ListSerials(ctx context.Context, token, thingID string, offset, limit uint64) (Page, error) {
u, err := cs.auth.Identify(ctx, &policies.Token{Value: token})
u, err := cs.auth.Identify(ctx, &policies.IdentifyReq{Token: token})
if err != nil {
return Page{}, err
}
Expand All @@ -176,7 +176,7 @@ func (cs *certsService) ListSerials(ctx context.Context, token, thingID string,
}

func (cs *certsService) ViewCert(ctx context.Context, token, serialID string) (Cert, error) {
u, err := cs.auth.Identify(ctx, &policies.Token{Value: token})
u, err := cs.auth.Identify(ctx, &policies.IdentifyReq{Token: token})
if err != nil {
return Cert{}, err
}
Expand Down
Loading

0 comments on commit 7cccba9

Please sign in to comment.