Skip to content

Commit

Permalink
NOSSUE - Sync API Dcoumentation (#1754)
Browse files Browse the repository at this point in the history
* Sync Things API Documentation

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Fix Update Secret SDK

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Fix Update Password CLI

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Fix decodeUpdateClientCredentials Omission

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Fix Failing Update Client Tests

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

---------

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
  • Loading branch information
rodneyosodo authored and dborovcanin committed Apr 17, 2023
1 parent fa536b0 commit 46e2e51
Show file tree
Hide file tree
Showing 17 changed files with 1,442 additions and 504 deletions.
1,833 changes: 1,369 additions & 464 deletions api/openapi/things.yml

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions cli/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,16 +250,16 @@ var cmdUsers = []cobra.Command{
},
},
{
Use: "password <user_id> <old_password> <password> <user_auth_token>",
Use: "password <old_password> <password> <user_auth_token>",
Short: "Update password",
Long: `Update user password`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 4 {
if len(args) != 3 {
logUsage(cmd.Use)
return
}

user, err := sdk.UpdatePassword(args[0], args[1], args[2], args[3])
user, err := sdk.UpdatePassword(args[0], args[1], args[2])
if err != nil {
logError(err)
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/sdk/go/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type updateClientSecretReq struct {
}

type updateThingSecretReq struct {
Secret string `json:"key,omitempty"`
Secret string `json:"secret,omitempty"`
}

// updateClientIdentityReq is used to update the client identity
Expand Down
2 changes: 1 addition & 1 deletion pkg/sdk/go/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ type SDK interface {
UpdateUserOwner(user User, token string) (User, errors.SDKError)

// UpdatePassword updates user password.
UpdatePassword(id, oldPass, newPass, token string) (User, errors.SDKError)
UpdatePassword(oldPass, newPass, token string) (User, errors.SDKError)

// EnableUser changes the status of the user to enabled.
EnableUser(id, token string) (User, errors.SDKError)
Expand Down
2 changes: 1 addition & 1 deletion pkg/sdk/go/things.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (sdk mfSDK) UpdateThingSecret(id, secret, token string) (Thing, errors.SDKE
return Thing{}, errors.NewSDKError(err)
}

url := fmt.Sprintf("%s/%s/%s/key", sdk.thingsURL, thingsEndpoint, id)
url := fmt.Sprintf("%s/%s/%s/secret", sdk.thingsURL, thingsEndpoint, id)

_, body, sdkerr := sdk.processRequest(http.MethodPatch, url, token, string(CTJSON), data, http.StatusOK)
if sdkerr != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/sdk/go/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,15 @@ func (sdk mfSDK) UpdateUserIdentity(user User, token string) (User, errors.SDKEr
}

// UpdatePassword updates user password.
func (sdk mfSDK) UpdatePassword(id, oldPass, newPass, token string) (User, errors.SDKError) {
func (sdk mfSDK) UpdatePassword(oldPass, newPass, token string) (User, errors.SDKError) {
var ucsr = updateClientSecretReq{OldSecret: oldPass, NewSecret: newPass}

data, err := json.Marshal(ucsr)
if err != nil {
return User{}, errors.NewSDKError(err)
}

url := fmt.Sprintf("%s/%s/%s/secret", sdk.usersURL, usersEndpoint, id)
url := fmt.Sprintf("%s/%s/secret", sdk.usersURL, usersEndpoint)

_, body, sdkerr := sdk.processRequest(http.MethodPatch, url, token, string(CTJSON), data, http.StatusOK)
if sdkerr != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/sdk/go/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ func TestUpdateClientSecret(t *testing.T) {
repoCall := cRepo.On("RetrieveByID", mock.Anything, user.ID).Return(convertClient(tc.response), tc.err)
repoCall1 := cRepo.On("RetrieveByIdentity", mock.Anything, user.Credentials.Identity).Return(convertClient(tc.response), tc.err)
repoCall2 := cRepo.On("UpdateSecret", mock.Anything, mock.Anything).Return(convertClient(tc.response), tc.err)
uClient, err := clientSDK.UpdatePassword(user.ID, tc.oldSecret, tc.newSecret, tc.token)
uClient, err := clientSDK.UpdatePassword(tc.oldSecret, tc.newSecret, tc.token)
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected error %s, got %s", tc.desc, tc.err, err))
assert.Equal(t, tc.response, uClient, fmt.Sprintf("%s: expected %v got %v\n", tc.desc, tc.response, uClient))
if tc.err == nil {
Expand Down
2 changes: 1 addition & 1 deletion things/clients/api/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (req updateClientOwnerReq) validate() error {
type updateClientCredentialsReq struct {
token string
id string
Secret string `json:"key,omitempty"`
Secret string `json:"secret,omitempty"`
}

func (req updateClientCredentialsReq) validate() error {
Expand Down
2 changes: 1 addition & 1 deletion things/clients/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func MakeHandler(svc clients.Service, mux *bone.Mux, logger mflog.Logger) http.H
opts...,
))

mux.Patch("/things/:id/key", kithttp.NewServer(
mux.Patch("/things/:id/secret", kithttp.NewServer(
otelkit.EndpointMiddleware(otelkit.WithOperation("update_thing_secret"))(updateClientSecretEndpoint(svc)),
decodeUpdateClientCredentials,
api.EncodeResponse,
Expand Down
1 change: 1 addition & 0 deletions things/groups/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func MakeHandler(svc groups.Service, mux *bone.Mux, logger logger.Logger) http.H
api.EncodeResponse,
opts...,
))

mux.Post("/channels/:id/enable", kithttp.NewServer(
otelkit.EndpointMiddleware(otelkit.WithOperation("enable_channel"))(enableGroupEndpoint(svc)),
decodeChangeGroupStatus,
Expand Down
8 changes: 5 additions & 3 deletions things/policies/api/http/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func connectEndpoint(svc policies.Service) endpoint.Endpoint {
return nil, err
}

return policyRes{policy, true}, nil
return policyRes{[]policies.Policy{policy}, true}, nil
}
}

Expand All @@ -94,6 +94,7 @@ func connectThingsEndpoint(svc policies.Service) endpoint.Endpoint {
if err := cr.validate(); err != nil {
return nil, err
}
ps := []policies.Policy{}
for _, tid := range cr.ClientIDs {
for _, cid := range cr.GroupIDs {
if len(cr.Actions) == 0 {
Expand All @@ -107,10 +108,11 @@ func connectThingsEndpoint(svc policies.Service) endpoint.Endpoint {
if _, err := svc.AddPolicy(ctx, cr.token, policy); err != nil {
return nil, err
}
ps = append(ps, policy)
}
}

return policyRes{created: true}, nil
return policyRes{created: true, Policies: ps}, nil
}
}

Expand All @@ -131,7 +133,7 @@ func updatePolicyEndpoint(svc policies.Service) endpoint.Endpoint {
return nil, err
}

return policyRes{policy, true}, nil
return policyRes{[]policies.Policy{policy}, true}, nil
}
}

Expand Down
4 changes: 2 additions & 2 deletions things/policies/api/http/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ var (
)

type policyRes struct {
policies.Policy
created bool
Policies []policies.Policy `json:"policies"`
created bool
}

func (res policyRes) Code() int {
Expand Down
5 changes: 5 additions & 0 deletions things/policies/api/http/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ func decodeDisconnectThing(_ context.Context, r *http.Request) (interface{}, err
GroupID: bone.GetValue(r, "chanId"),
ClientID: bone.GetValue(r, "thingId"),
}
if r.Body != http.NoBody {
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
return nil, errors.Wrap(errors.ErrMalformedEntity, err)
}
}

return req, nil
}
Expand Down
10 changes: 5 additions & 5 deletions things/policies/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ type Page struct {
Total uint64
Offset uint64
Limit uint64
OwnerID string
Subject string
Object string
Action string
Metadata Metadata
OwnerID string `json:"owner,omitempty"`
Subject string `json:"subject,omitempty"`
Object string `json:"object,omitempty"`
Action string `json:"action,omitempty"`
Metadata Metadata `json:"metadata,omitempty"`
}

// Validate check page actions.
Expand Down
4 changes: 2 additions & 2 deletions users/clients/api/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func updateClientTagsEndpoint(svc clients.Service) endpoint.Endpoint {

func updateClientIdentityEndpoint(svc clients.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(updateClientCredentialsReq)
req := request.(updateClientIdentityReq)
if err := req.validate(); err != nil {
return nil, err
}
Expand Down Expand Up @@ -209,7 +209,7 @@ func passwordResetEndpoint(svc clients.Service) endpoint.Endpoint {

func updateClientSecretEndpoint(svc clients.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(updateClientCredentialsReq)
req := request.(updateClientSecretReq)
if err := req.validate(); err != nil {
return nil, err
}
Expand Down
25 changes: 18 additions & 7 deletions users/clients/api/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,13 @@ func (req updateClientOwnerReq) validate() error {
return nil
}

type updateClientCredentialsReq struct {
token string
id string
Identity string `json:"identity,omitempty"`
OldSecret string `json:"old_secret,omitempty"`
NewSecret string `json:"new_secret,omitempty"`
type updateClientIdentityReq struct {
token string
id string
Identity string `json:"identity,omitempty"`
}

func (req updateClientCredentialsReq) validate() error {
func (req updateClientIdentityReq) validate() error {
if req.token == "" {
return apiutil.ErrBearerToken
}
Expand All @@ -152,6 +150,19 @@ func (req updateClientCredentialsReq) validate() error {
return nil
}

type updateClientSecretReq struct {
token string
OldSecret string `json:"old_secret,omitempty"`
NewSecret string `json:"new_secret,omitempty"`
}

func (req updateClientSecretReq) validate() error {
if req.token == "" {
return apiutil.ErrBearerToken
}
return nil
}

type changeClientStatusReq struct {
token string
id string
Expand Down
34 changes: 24 additions & 10 deletions users/clients/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ func MakeClientsHandler(svc clients.Service, mux *bone.Mux, logger mflog.Logger)
opts...,
))

mux.Patch("/users/secret", kithttp.NewServer(
otelkit.EndpointMiddleware(otelkit.WithOperation("update_client_secret"))(updateClientSecretEndpoint(svc)),
decodeUpdateClientSecret,
api.EncodeResponse,
opts...,
))

mux.Patch("/users/:id", kithttp.NewServer(
otelkit.EndpointMiddleware(otelkit.WithOperation("update_client_name_and_metadata"))(updateClientEndpoint(svc)),
decodeUpdateClient,
Expand All @@ -75,7 +82,7 @@ func MakeClientsHandler(svc clients.Service, mux *bone.Mux, logger mflog.Logger)

mux.Patch("/users/:id/identity", kithttp.NewServer(
otelkit.EndpointMiddleware(otelkit.WithOperation("update_client_identity"))(updateClientIdentityEndpoint(svc)),
decodeUpdateClientCredentials,
decodeUpdateClientIdentity,
api.EncodeResponse,
opts...,
))
Expand All @@ -94,13 +101,6 @@ func MakeClientsHandler(svc clients.Service, mux *bone.Mux, logger mflog.Logger)
opts...,
))

mux.Patch("/users/:id/secret", kithttp.NewServer(
otelkit.EndpointMiddleware(otelkit.WithOperation("update_client_secret"))(updateClientSecretEndpoint(svc)),
decodeUpdateClientCredentials,
api.EncodeResponse,
opts...,
))

mux.Patch("/users/:id/owner", kithttp.NewServer(
otelkit.EndpointMiddleware(otelkit.WithOperation("update_client_owner"))(updateClientOwnerEndpoint(svc)),
decodeUpdateClientOwner,
Expand Down Expand Up @@ -254,11 +254,11 @@ func decodeUpdateClientTags(_ context.Context, r *http.Request) (interface{}, er
return req, nil
}

func decodeUpdateClientCredentials(_ context.Context, r *http.Request) (interface{}, error) {
func decodeUpdateClientIdentity(_ context.Context, r *http.Request) (interface{}, error) {
if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) {
return nil, errors.ErrUnsupportedContentType
}
req := updateClientCredentialsReq{
req := updateClientIdentityReq{
token: apiutil.ExtractBearerToken(r),
id: bone.GetValue(r, "id"),
}
Expand All @@ -269,6 +269,20 @@ func decodeUpdateClientCredentials(_ context.Context, r *http.Request) (interfac
return req, nil
}

func decodeUpdateClientSecret(_ context.Context, r *http.Request) (interface{}, error) {
if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) {
return nil, errors.ErrUnsupportedContentType
}
req := updateClientSecretReq{
token: apiutil.ExtractBearerToken(r),
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
return nil, errors.Wrap(errors.ErrMalformedEntity, err)
}

return req, nil
}

func decodePasswordResetRequest(_ context.Context, r *http.Request) (interface{}, error) {
if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) {
return nil, errors.ErrUnsupportedContentType
Expand Down

0 comments on commit 46e2e51

Please sign in to comment.