321 lines
5.5 KiB
Go
321 lines
5.5 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package api
|
|
|
|
import (
|
|
"github.com/mainflux/mainflux/internal/api"
|
|
"github.com/mainflux/mainflux/internal/apiutil"
|
|
mfclients "github.com/mainflux/mainflux/pkg/clients"
|
|
)
|
|
|
|
const maxLimitSize = 100
|
|
|
|
type createClientReq struct {
|
|
client mfclients.Client
|
|
token string
|
|
}
|
|
|
|
func (req createClientReq) validate() error {
|
|
if len(req.client.Name) > api.MaxNameSize {
|
|
return apiutil.ErrNameSize
|
|
}
|
|
|
|
return req.client.Validate()
|
|
}
|
|
|
|
type viewClientReq struct {
|
|
token string
|
|
id string
|
|
}
|
|
|
|
func (req viewClientReq) validate() error {
|
|
if req.token == "" {
|
|
return apiutil.ErrBearerToken
|
|
}
|
|
if req.id == "" {
|
|
return apiutil.ErrMissingID
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type viewProfileReq struct {
|
|
token string
|
|
}
|
|
|
|
func (req viewProfileReq) validate() error {
|
|
if req.token == "" {
|
|
return apiutil.ErrBearerToken
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type listClientsReq struct {
|
|
token string
|
|
status mfclients.Status
|
|
offset uint64
|
|
limit uint64
|
|
name string
|
|
tag string
|
|
identity string
|
|
visibility string
|
|
owner string
|
|
sharedBy string
|
|
metadata mfclients.Metadata
|
|
}
|
|
|
|
func (req listClientsReq) validate() error {
|
|
if req.token == "" {
|
|
return apiutil.ErrBearerToken
|
|
}
|
|
if req.limit > maxLimitSize || req.limit < 1 {
|
|
return apiutil.ErrLimitSize
|
|
}
|
|
if req.visibility != "" &&
|
|
req.visibility != api.AllVisibility &&
|
|
req.visibility != api.MyVisibility &&
|
|
req.visibility != api.SharedVisibility {
|
|
return apiutil.ErrInvalidVisibilityType
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type listMembersReq struct {
|
|
mfclients.Page
|
|
token string
|
|
groupID string
|
|
}
|
|
|
|
func (req listMembersReq) validate() error {
|
|
if req.token == "" {
|
|
return apiutil.ErrBearerToken
|
|
}
|
|
if req.groupID == "" {
|
|
return apiutil.ErrMissingID
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type updateClientReq struct {
|
|
token string
|
|
id string
|
|
Name string `json:"name,omitempty"`
|
|
Metadata mfclients.Metadata `json:"metadata,omitempty"`
|
|
}
|
|
|
|
func (req updateClientReq) validate() error {
|
|
if req.token == "" {
|
|
return apiutil.ErrBearerToken
|
|
}
|
|
if req.id == "" {
|
|
return apiutil.ErrMissingID
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type updateClientTagsReq struct {
|
|
id string
|
|
token string
|
|
Tags []string `json:"tags,omitempty"`
|
|
}
|
|
|
|
func (req updateClientTagsReq) validate() error {
|
|
if req.token == "" {
|
|
return apiutil.ErrBearerToken
|
|
}
|
|
if req.id == "" {
|
|
return apiutil.ErrMissingID
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type updateClientOwnerReq struct {
|
|
id string
|
|
token string
|
|
Owner string `json:"owner,omitempty"`
|
|
}
|
|
|
|
func (req updateClientOwnerReq) validate() error {
|
|
if req.token == "" {
|
|
return apiutil.ErrBearerToken
|
|
}
|
|
if req.id == "" {
|
|
return apiutil.ErrMissingID
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type updateClientIdentityReq struct {
|
|
token string
|
|
id string
|
|
Identity string `json:"identity,omitempty"`
|
|
}
|
|
|
|
func (req updateClientIdentityReq) validate() error {
|
|
if req.token == "" {
|
|
return apiutil.ErrBearerToken
|
|
}
|
|
if req.id == "" {
|
|
return apiutil.ErrMissingID
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func (req changeClientStatusReq) validate() error {
|
|
if req.token == "" {
|
|
return apiutil.ErrBearerToken
|
|
}
|
|
if req.id == "" {
|
|
return apiutil.ErrMissingID
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type loginClientReq struct {
|
|
Identity string `json:"identity,omitempty"`
|
|
Secret string `json:"secret,omitempty"`
|
|
}
|
|
|
|
func (req loginClientReq) validate() error {
|
|
if req.Identity == "" {
|
|
return apiutil.ErrMissingIdentity
|
|
}
|
|
if req.Secret == "" {
|
|
return apiutil.ErrMissingSecret
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type tokenReq struct {
|
|
RefreshToken string `json:"refresh_token,omitempty"`
|
|
}
|
|
|
|
func (req tokenReq) validate() error {
|
|
if req.RefreshToken == "" {
|
|
return apiutil.ErrBearerToken
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type passwResetReq struct {
|
|
Email string `json:"email"`
|
|
Host string `json:"host"`
|
|
}
|
|
|
|
func (req passwResetReq) validate() error {
|
|
if req.Email == "" {
|
|
return apiutil.ErrMissingEmail
|
|
}
|
|
if req.Host == "" {
|
|
return apiutil.ErrMissingHost
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type resetTokenReq struct {
|
|
Token string `json:"token"`
|
|
Password string `json:"password"`
|
|
ConfPass string `json:"confirm_password"`
|
|
}
|
|
|
|
func (req resetTokenReq) validate() error {
|
|
if req.Password == "" {
|
|
return apiutil.ErrMissingPass
|
|
}
|
|
if req.ConfPass == "" {
|
|
return apiutil.ErrMissingConfPass
|
|
}
|
|
if req.Token == "" {
|
|
return apiutil.ErrBearerToken
|
|
}
|
|
if req.Password != req.ConfPass {
|
|
return apiutil.ErrInvalidResetPass
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type assignUsersReq struct {
|
|
token string
|
|
groupID string
|
|
Relation string `json:"relation"`
|
|
UserIDs []string `json:"user_ids"`
|
|
}
|
|
|
|
func (req assignUsersReq) validate() error {
|
|
if req.token == "" {
|
|
return apiutil.ErrBearerToken
|
|
}
|
|
|
|
if req.Relation == "" {
|
|
return apiutil.ErrMissingRelation
|
|
}
|
|
|
|
if req.groupID == "" {
|
|
return apiutil.ErrMissingID
|
|
}
|
|
|
|
if len(req.UserIDs) == 0 {
|
|
return apiutil.ErrEmptyList
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type unassignUsersReq struct {
|
|
token string
|
|
groupID string
|
|
Relation string `json:"relation"`
|
|
UserIDs []string `json:"user_ids"`
|
|
}
|
|
|
|
func (req unassignUsersReq) validate() error {
|
|
if req.token == "" {
|
|
return apiutil.ErrBearerToken
|
|
}
|
|
|
|
if req.groupID == "" {
|
|
return apiutil.ErrMissingID
|
|
}
|
|
|
|
if req.Relation == "" {
|
|
return apiutil.ErrMissingRelation
|
|
}
|
|
if len(req.UserIDs) == 0 {
|
|
return apiutil.ErrEmptyList
|
|
}
|
|
|
|
return nil
|
|
}
|