1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-29 13:49:28 +08:00
Dušan Borovčanin 39133b06a4
Fix standalone mode (#1497)
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

Co-authored-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com>
2021-11-16 13:02:31 +01:00

77 lines
2.4 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package standalone
import (
"context"
"errors"
"github.com/golang/protobuf/ptypes/empty"
"github.com/mainflux/mainflux"
"github.com/mainflux/mainflux/things"
"google.golang.org/grpc"
)
var errUnsupported = errors.New("not supported in standalone mode")
var _ mainflux.AuthServiceClient = (*singleUserRepo)(nil)
type singleUserRepo struct {
email string
token string
}
// NewAuthService creates single user repository for constrained environments.
func NewAuthService(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) {
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) {
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) {
if repo.email != req.Sub {
return &mainflux.AuthorizeRes{}, errUnsupported
}
return &mainflux.AuthorizeRes{Authorized: true}, nil
}
func (repo singleUserRepo) AddPolicy(ctx context.Context, req *mainflux.AddPolicyReq, opts ...grpc.CallOption) (*mainflux.AddPolicyRes, error) {
if repo.email != req.Sub {
return &mainflux.AddPolicyRes{}, errUnsupported
}
return &mainflux.AddPolicyRes{Authorized: true}, nil
}
func (repo singleUserRepo) DeletePolicy(ctx context.Context, req *mainflux.DeletePolicyReq, opts ...grpc.CallOption) (*mainflux.DeletePolicyRes, error) {
if repo.email != req.Sub {
return &mainflux.DeletePolicyRes{}, errUnsupported
}
return &mainflux.DeletePolicyRes{Deleted: true}, nil
}
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
}