mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-02 22:17:10 +08:00

* Move Things and Users to Clients Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Signed-off-by: rodneyosodo <blackd0t@protonmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Update Add and Delete Policies (#1792) * Remove Policy Action Ranks Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Fix Rebase Issues Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Fix CI Test Errors Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Adding Check on Subject For Clients Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Remove Check Client Exists Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Check When Sharing Clients Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Only Add User to Group When Sharing Things Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Remove clientType Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Minor Fix on ShareClient and Fix Tests Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Fix Policies Tests Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Clean Up Things Authorization Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Fix Tests on RetrieveAll Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Test ShareThing Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Fix Merge Conflicts Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Remove Adding Policies. Only Use Ownership Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Check If Subject is same as Object Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Move Back To Union As Sometimes Policy is Empty and Fails to Evaluate on Ownership Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Fix Entity Type For Failing Tests Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Fix BUG in policy evaluation Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Fix Tests Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Add Comments Regarding checkAdmin Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Fix Tests On Rebase Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Combine Authorize For Things and Users Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Fix Tests On Rebase Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Error on Things SVC `unsupported protocol scheme` Signed-off-by: rodneyosodo <blackd0t@protonmail.com> --------- Signed-off-by: rodneyosodo <blackd0t@protonmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix Bug on Things Authorization Cache (#1810) Signed-off-by: rodneyosodo <blackd0t@protonmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Use Password instead of username in MQTT handler Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Simplify MQTT authorization Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix MQTT tests Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Add More Functions to SDK (#1811) * Add More Functions to SDK Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Add Examples to GoDoc Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Update Unassign Interface Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Pass Subject as ID and Not Token on List Channels By Thing Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Fix Bootstrap Errors For Element Check Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Add empty line Before Return Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Reorder URLS in things mux Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Fix Listing Things Policies Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Fix Share Thing Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Add Examples to CLI Docs Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Fix Update Identity To Update Another User Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Fix Identify an Update Policies on Things Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Fix Update Things Policies Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Fix GoDocs on Disconnect Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Fix Tests Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Change Authorize To Use AccessRequest Signed-off-by: rodneyosodo <blackd0t@protonmail.com> --------- Signed-off-by: rodneyosodo <blackd0t@protonmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * For Evaluate Policy Use AccessRequest (#1814) Signed-off-by: rodneyosodo <blackd0t@protonmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Add SDK Tests (#1812) * Add Things Tests Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Add Channel Tests Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Add Certs Tests Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Add Consumer Tests Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Enrich Group Tests Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Add Tests For Health Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Add Tests For Tokens Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Rename SDK for Tests Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Add Policies Tests Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Fix Linter Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Fix Tests Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Make Variable Defination Inline Signed-off-by: rodneyosodo <blackd0t@protonmail.com> --------- Signed-off-by: rodneyosodo <blackd0t@protonmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Make Cache Key Duration Configurable (#1815) * Make Cache Key Duration Configurable Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Rename ENV Var Signed-off-by: rodneyosodo <blackd0t@protonmail.com> --------- Signed-off-by: rodneyosodo <blackd0t@protonmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Update GoDocs (#1816) * Add GoDocs Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Add Missing GoDoc Files Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Enable godot Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Add License Information Signed-off-by: rodneyosodo <blackd0t@protonmail.com> --------- Signed-off-by: rodneyosodo <blackd0t@protonmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Add Call Home Client to Mainflux services (#1751) * Move Things and Users to Clients Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Signed-off-by: rodneyosodo <blackd0t@protonmail.com> Signed-off-by: SammyOina <sammyoina@gmail.com> * collect and send data package Signed-off-by: SammyOina <sammyoina@gmail.com> * create telemetry migrations Signed-off-by: SammyOina <sammyoina@gmail.com> * add telemetry endpoints Signed-off-by: SammyOina <sammyoina@gmail.com> * add transport Signed-off-by: SammyOina <sammyoina@gmail.com> * create service Signed-off-by: SammyOina <sammyoina@gmail.com> * remove homing server Signed-off-by: SammyOina <sammyoina@gmail.com> * add call home to adapters Signed-off-by: SammyOina <sammyoina@gmail.com> * add last seen Signed-off-by: SammyOina <sammyoina@gmail.com> * rename logger Signed-off-by: SammyOina <sammyoina@gmail.com> * remove homing client Signed-off-by: SammyOina <sammyoina@gmail.com> * use unmerged repo Signed-off-by: SammyOina <sammyoina@gmail.com> * use renamed module Signed-off-by: SammyOina <sammyoina@gmail.com> * update call home version Signed-off-by: SammyOina <sammyoina@gmail.com> * edit documentation Signed-off-by: SammyOina <sammyoina@gmail.com> * align table Signed-off-by: SammyOina <sammyoina@gmail.com> * use alias for call home client Signed-off-by: SammyOina <sammyoina@gmail.com> * update callhome Signed-off-by: SammyOina <sammyoina@gmail.com> * update call home pkg Signed-off-by: SammyOina <sammyoina@gmail.com> * update call home Signed-off-by: SammyOina <sammyoina@gmail.com> * fix modules Signed-off-by: SammyOina <sammyoina@gmail.com> * use mf build version Signed-off-by: SammyOina <sammyoina@gmail.com> * use mf build version Signed-off-by: SammyOina <sammyoina@gmail.com> * restore default Signed-off-by: SammyOina <sammyoina@gmail.com> * add call home for users and things Signed-off-by: SammyOina <sammyoina@gmail.com> * enable opting on call home Signed-off-by: SammyOina <sammyoina@gmail.com> * remove full stops Signed-off-by: SammyOina <sammyoina@gmail.com> * update callhome client Signed-off-by: SammyOina <sammyoina@gmail.com> * add call home to all services Signed-off-by: SammyOina <sammyoina@gmail.com> * fix build Signed-off-by: SammyOina <sammyoina@gmail.com> * restore sdk tests Signed-off-by: SammyOina <sammyoina@gmail.com> * remove unnecessary changes Signed-off-by: SammyOina <sammyoina@gmail.com> * restore health_test.go Signed-off-by: SammyOina <sammyoina@gmail.com> --------- Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Signed-off-by: rodneyosodo <blackd0t@protonmail.com> Signed-off-by: SammyOina <sammyoina@gmail.com> Co-authored-by: b1ackd0t <blackd0t@protonmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> --------- Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Signed-off-by: rodneyosodo <blackd0t@protonmail.com> Signed-off-by: SammyOina <sammyoina@gmail.com> Co-authored-by: b1ackd0t <blackd0t@protonmail.com> Co-authored-by: Sammy Kerata Oina <44265300+SammyOina@users.noreply.github.com>
326 lines
8.6 KiB
Go
326 lines
8.6 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package api
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/go-kit/kit/endpoint"
|
|
mfclients "github.com/mainflux/mainflux/pkg/clients"
|
|
"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{}, 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, 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, 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{}, 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{}, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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
|
|
}
|