mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-01 13:48:56 +08:00

* fix error package errors Signed-off-by: aryan <aryangodara03@gmail.com> * fix bootstap and bootstrap api Signed-off-by: aryan <aryangodara03@gmail.com> * fix certs Signed-off-by: aryan <aryangodara03@gmail.com> * fix consumers Signed-off-by: aryan <aryangodara03@gmail.com> * fix http Signed-off-by: aryan <aryangodara03@gmail.com> * fix provision Signed-off-by: aryan <aryangodara03@gmail.com> * fix readers Signed-off-by: aryan <aryangodara03@gmail.com> * fix twins Signed-off-by: aryan <aryangodara03@gmail.com> * fix things Signed-off-by: aryan <aryangodara03@gmail.com> * fix users Signed-off-by: aryan <aryangodara03@gmail.com> * fix sdk excpet channel policies users things Signed-off-by: aryan <aryangodara03@gmail.com> * tests passing, but logging not working for things and users Signed-off-by: aryan <aryangodara03@gmail.com> * fix sdk tests, and other failing tests Signed-off-by: aryan <aryangodara03@gmail.com> * fix comment Signed-off-by: aryan <aryangodara03@gmail.com> * fix errors acc to pr review Signed-off-by: aryan <aryangodara03@gmail.com> * fix errror wrapping in consumers api Signed-off-by: aryan <aryangodara03@gmail.com> * all tests running Signed-off-by: aryan <aryangodara03@gmail.com> * fix encodeError Signed-off-by: aryan <aryangodara03@gmail.com> * fix minor issues Signed-off-by: aryan <aryangodara03@gmail.com> * fix failing sdk policy tests Signed-off-by: aryan <aryangodara03@gmail.com> * fix errors in things test sdk Signed-off-by: aryan <aryangodara03@gmail.com> * update things service Signed-off-by: aryan <aryangodara03@gmail.com> * update usrs service Signed-off-by: aryan <aryangodara03@gmail.com> * fix things and users sdk Signed-off-by: aryan <aryangodara03@gmail.com> * fix sdk for channels groups policies things users Signed-off-by: aryan <aryangodara03@gmail.com> * fix remaining services and sdk Signed-off-by: aryan <aryangodara03@gmail.com> * fix bootstrap twins Signed-off-by: aryan <aryangodara03@gmail.com> * resolve conflicts Signed-off-by: aryan <aryangodara03@gmail.com> * Shift errmalformedentity to pkg/errors Signed-off-by: aryan <aryangodara03@gmail.com> * Fix bootstrap service Signed-off-by: aryan <aryangodara03@gmail.com> * Add errors.Unwrap and use in encodeError Signed-off-by: aryan <aryangodara03@gmail.com> * Fix type in print statement for policies_test Signed-off-by: aryan <aryangodara03@gmail.com> * Fix ordering of errvalidation wrapping and encodeError Signed-off-by: aryan <aryangodara03@gmail.com> * Fix failing tests Signed-off-by: aryan <aryangodara03@gmail.com> --------- Signed-off-by: aryan <aryangodara03@gmail.com>
328 lines
9.2 KiB
Go
328 lines
9.2 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package api
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/go-kit/kit/endpoint"
|
|
"github.com/mainflux/mainflux/internal/apiutil"
|
|
mfclients "github.com/mainflux/mainflux/pkg/clients"
|
|
"github.com/mainflux/mainflux/pkg/errors"
|
|
"github.com/mainflux/mainflux/users/clients"
|
|
)
|
|
|
|
func registrationEndpoint(svc clients.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(createClientReq)
|
|
if err := req.validate(); err != nil {
|
|
return createClientRes{}, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
client, err := svc.RegisterClient(ctx, req.token, req.client)
|
|
if err != nil {
|
|
return createClientRes{}, err
|
|
}
|
|
ucr := createClientRes{
|
|
Client: client,
|
|
created: true,
|
|
}
|
|
|
|
return ucr, nil
|
|
}
|
|
}
|
|
|
|
func viewClientEndpoint(svc clients.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(viewClientReq)
|
|
if err := req.validate(); err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
|
|
client, err := svc.ViewClient(ctx, req.token, req.id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return viewClientRes{Client: client}, nil
|
|
}
|
|
}
|
|
|
|
func viewProfileEndpoint(svc clients.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(viewProfileReq)
|
|
if err := req.validate(); err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
|
|
client, err := svc.ViewProfile(ctx, req.token)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return viewClientRes{
|
|
Client: client,
|
|
}, nil
|
|
}
|
|
}
|
|
|
|
func listClientsEndpoint(svc clients.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(listClientsReq)
|
|
if err := req.validate(); err != nil {
|
|
return mfclients.ClientsPage{}, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
|
|
pm := mfclients.Page{
|
|
SharedBy: req.sharedBy,
|
|
Status: req.status,
|
|
Offset: req.offset,
|
|
Limit: req.limit,
|
|
Owner: req.owner,
|
|
Name: req.name,
|
|
Tag: req.tag,
|
|
Metadata: req.metadata,
|
|
Identity: req.identity,
|
|
}
|
|
page, err := svc.ListClients(ctx, req.token, pm)
|
|
if err != nil {
|
|
return mfclients.ClientsPage{}, err
|
|
}
|
|
|
|
res := clientsPageRes{
|
|
pageRes: pageRes{
|
|
Total: page.Total,
|
|
Offset: page.Offset,
|
|
Limit: page.Limit,
|
|
},
|
|
Clients: []viewClientRes{},
|
|
}
|
|
for _, client := range page.Clients {
|
|
res.Clients = append(res.Clients, viewClientRes{Client: client})
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
}
|
|
|
|
func listMembersEndpoint(svc clients.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(listMembersReq)
|
|
if err := req.validate(); err != nil {
|
|
return memberPageRes{}, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
page, err := svc.ListMembers(ctx, req.token, req.groupID, req.Page)
|
|
if err != nil {
|
|
return memberPageRes{}, err
|
|
}
|
|
return buildMembersResponse(page), nil
|
|
}
|
|
}
|
|
|
|
func updateClientEndpoint(svc clients.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(updateClientReq)
|
|
if err := req.validate(); err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
|
|
client := mfclients.Client{
|
|
ID: req.id,
|
|
Name: req.Name,
|
|
Metadata: req.Metadata,
|
|
}
|
|
client, err := svc.UpdateClient(ctx, req.token, client)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return updateClientRes{Client: client}, nil
|
|
}
|
|
}
|
|
|
|
func updateClientTagsEndpoint(svc clients.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(updateClientTagsReq)
|
|
if err := req.validate(); err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
|
|
client := mfclients.Client{
|
|
ID: req.id,
|
|
Tags: req.Tags,
|
|
}
|
|
client, err := svc.UpdateClientTags(ctx, req.token, client)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return updateClientRes{Client: client}, nil
|
|
}
|
|
}
|
|
|
|
func updateClientIdentityEndpoint(svc clients.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(updateClientIdentityReq)
|
|
if err := req.validate(); err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
client, err := svc.UpdateClientIdentity(ctx, req.token, req.id, req.Identity)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return updateClientRes{Client: client}, nil
|
|
}
|
|
}
|
|
|
|
// Password reset request endpoint.
|
|
// When successful password reset link is generated.
|
|
// Link is generated using MF_TOKEN_RESET_ENDPOINT env.
|
|
// and value from Referer header for host.
|
|
// {Referer}+{MF_TOKEN_RESET_ENDPOINT}+{token=TOKEN}
|
|
// http://mainflux.com/reset-request?token=xxxxxxxxxxx.
|
|
// Email with a link is being sent to the user.
|
|
// When user clicks on a link it should get the ui with form to
|
|
// enter new password, when form is submitted token and new password
|
|
// must be sent as PUT request to 'password/reset' passwordResetEndpoint.
|
|
func passwordResetRequestEndpoint(svc clients.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(passwResetReq)
|
|
if err := req.validate(); err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
if err := svc.GenerateResetToken(ctx, req.Email, req.Host); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return passwResetReqRes{Msg: MailSent}, nil
|
|
}
|
|
}
|
|
|
|
// This is endpoint that actually sets new password in password reset flow.
|
|
// When user clicks on a link in email finally ends on this endpoint as explained in
|
|
// the comment above.
|
|
func passwordResetEndpoint(svc clients.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(resetTokenReq)
|
|
if err := req.validate(); err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
if err := svc.ResetSecret(ctx, req.Token, req.Password); err != nil {
|
|
return nil, err
|
|
}
|
|
return passwChangeRes{}, nil
|
|
}
|
|
}
|
|
|
|
func updateClientSecretEndpoint(svc clients.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(updateClientSecretReq)
|
|
if err := req.validate(); err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
client, err := svc.UpdateClientSecret(ctx, req.token, req.OldSecret, req.NewSecret)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return updateClientRes{Client: client}, nil
|
|
}
|
|
}
|
|
|
|
func updateClientOwnerEndpoint(svc clients.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(updateClientOwnerReq)
|
|
if err := req.validate(); err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
|
|
client := mfclients.Client{
|
|
ID: req.id,
|
|
Owner: req.Owner,
|
|
}
|
|
|
|
client, err := svc.UpdateClientOwner(ctx, req.token, client)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return updateClientRes{Client: client}, nil
|
|
}
|
|
}
|
|
|
|
func issueTokenEndpoint(svc clients.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(loginClientReq)
|
|
if err := req.validate(); err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
|
|
token, err := svc.IssueToken(ctx, req.Identity, req.Secret)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return tokenRes{
|
|
AccessToken: token.AccessToken,
|
|
RefreshToken: token.RefreshToken,
|
|
AccessType: token.AccessType,
|
|
}, nil
|
|
}
|
|
}
|
|
|
|
func refreshTokenEndpoint(svc clients.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(tokenReq)
|
|
if err := req.validate(); err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
|
|
token, err := svc.RefreshToken(ctx, req.RefreshToken)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return tokenRes{
|
|
AccessToken: token.AccessToken,
|
|
RefreshToken: token.RefreshToken,
|
|
AccessType: token.AccessType,
|
|
}, nil
|
|
}
|
|
}
|
|
|
|
func enableClientEndpoint(svc clients.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(changeClientStatusReq)
|
|
if err := req.validate(); err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
client, err := svc.EnableClient(ctx, req.token, req.id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return deleteClientRes{Client: client}, nil
|
|
}
|
|
}
|
|
|
|
func disableClientEndpoint(svc clients.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(changeClientStatusReq)
|
|
if err := req.validate(); err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
client, err := svc.DisableClient(ctx, req.token, req.id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return deleteClientRes{Client: client}, nil
|
|
}
|
|
}
|
|
|
|
func buildMembersResponse(cp mfclients.MembersPage) memberPageRes {
|
|
res := memberPageRes{
|
|
pageRes: pageRes{
|
|
Total: cp.Total,
|
|
Offset: cp.Offset,
|
|
Limit: cp.Limit,
|
|
},
|
|
Members: []viewMembersRes{},
|
|
}
|
|
for _, client := range cp.Members {
|
|
res.Members = append(res.Members, viewMembersRes{Client: client})
|
|
}
|
|
return res
|
|
}
|