// Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 // Package users contains implementation for users service in // single user scenario. package users import ( "context" "errors" "time" "github.com/golang/protobuf/ptypes/empty" "github.com/mainflux/mainflux/things" "github.com/mainflux/mainflux" "google.golang.org/grpc" ) var ( errUnsupported = errors.New("not supported in single user mode") ) var _ mainflux.AuthServiceClient = (*singleUserRepo)(nil) type singleUserRepo struct { email string token string } // NewSingleUserService creates single user repository for constrained environments. func NewSingleUserService(email, token string) mainflux.AuthServiceClient { return singleUserRepo{ email: email, token: token, } } func (repo singleUserRepo) Issue(ctx context.Context, req *mainflux.IssueReq, opts ...grpc.CallOption) (*mainflux.Token, error) { ctx, cancel := context.WithTimeout(ctx, time.Second) defer cancel() if repo.token != req.GetEmail() { return nil, things.ErrUnauthorizedAccess } return &mainflux.Token{Value: repo.token}, nil } func (repo singleUserRepo) Identify(ctx context.Context, token *mainflux.Token, opts ...grpc.CallOption) (*mainflux.UserIdentity, error) { ctx, cancel := context.WithTimeout(ctx, time.Second) defer cancel() if repo.token != token.GetValue() { return nil, things.ErrUnauthorizedAccess } return &mainflux.UserIdentity{Id: repo.email, Email: repo.email}, nil } func (repo singleUserRepo) Authorize(ctx context.Context, req *mainflux.AuthorizeReq, _ ...grpc.CallOption) (r *mainflux.AuthorizeRes, err error) { return &mainflux.AuthorizeRes{}, errUnsupported } func (repo singleUserRepo) Members(ctx context.Context, req *mainflux.MembersReq, _ ...grpc.CallOption) (r *mainflux.MembersRes, err error) { return &mainflux.MembersRes{}, errUnsupported } func (repo singleUserRepo) Assign(ctx context.Context, req *mainflux.Assignment, _ ...grpc.CallOption) (r *empty.Empty, err error) { return &empty.Empty{}, errUnsupported }