2019-10-07 08:14:47 -06:00
|
|
|
// Copyright (c) Mainflux
|
2019-05-17 16:06:21 +02:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2021-10-11 16:08:26 +02:00
|
|
|
package standalone
|
2019-05-17 16:06:21 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-12-29 23:02:35 +01:00
|
|
|
"github.com/golang/protobuf/ptypes/empty"
|
2019-05-17 16:06:21 +02:00
|
|
|
"github.com/mainflux/mainflux"
|
2022-01-27 17:03:57 +01:00
|
|
|
"github.com/mainflux/mainflux/pkg/errors"
|
2019-05-17 16:06:21 +02:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
2021-10-11 16:08:26 +02:00
|
|
|
var errUnsupported = errors.New("not supported in standalone mode")
|
2020-12-29 23:02:35 +01:00
|
|
|
|
|
|
|
var _ mainflux.AuthServiceClient = (*singleUserRepo)(nil)
|
2019-05-17 16:06:21 +02:00
|
|
|
|
|
|
|
type singleUserRepo struct {
|
|
|
|
email string
|
|
|
|
token string
|
|
|
|
}
|
|
|
|
|
2021-10-11 16:08:26 +02:00
|
|
|
// NewAuthService creates single user repository for constrained environments.
|
|
|
|
func NewAuthService(email, token string) mainflux.AuthServiceClient {
|
2019-05-17 16:06:21 +02:00
|
|
|
return singleUserRepo{
|
|
|
|
email: email,
|
|
|
|
token: token,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-16 16:22:09 +01:00
|
|
|
func (repo singleUserRepo) Issue(ctx context.Context, req *mainflux.IssueReq, opts ...grpc.CallOption) (*mainflux.Token, error) {
|
2020-10-27 19:42:53 +01:00
|
|
|
if repo.token != req.GetEmail() {
|
2022-02-01 17:33:23 +01:00
|
|
|
return nil, errors.ErrAuthentication
|
2019-12-16 16:22:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return &mainflux.Token{Value: repo.token}, nil
|
|
|
|
}
|
|
|
|
|
2020-10-27 19:42:53 +01:00
|
|
|
func (repo singleUserRepo) Identify(ctx context.Context, token *mainflux.Token, opts ...grpc.CallOption) (*mainflux.UserIdentity, error) {
|
2019-05-17 16:06:21 +02:00
|
|
|
if repo.token != token.GetValue() {
|
2022-02-01 17:33:23 +01:00
|
|
|
return nil, errors.ErrAuthentication
|
2019-05-17 16:06:21 +02:00
|
|
|
}
|
|
|
|
|
2020-10-27 19:42:53 +01:00
|
|
|
return &mainflux.UserIdentity{Id: repo.email, Email: repo.email}, nil
|
2019-05-17 16:06:21 +02:00
|
|
|
}
|
2020-12-29 23:02:35 +01:00
|
|
|
|
|
|
|
func (repo singleUserRepo) Authorize(ctx context.Context, req *mainflux.AuthorizeReq, _ ...grpc.CallOption) (r *mainflux.AuthorizeRes, err error) {
|
2021-11-16 13:02:31 +01:00
|
|
|
if repo.email != req.Sub {
|
|
|
|
return &mainflux.AuthorizeRes{}, errUnsupported
|
|
|
|
}
|
|
|
|
return &mainflux.AuthorizeRes{Authorized: true}, nil
|
2020-12-29 23:02:35 +01:00
|
|
|
}
|
|
|
|
|
2021-11-16 13:02:31 +01:00
|
|
|
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
|
2021-10-27 00:38:28 +02:00
|
|
|
}
|
|
|
|
|
2021-11-16 13:02:31 +01:00
|
|
|
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
|
2021-10-27 00:38:28 +02:00
|
|
|
}
|
|
|
|
|
2021-11-19 16:32:38 +03:00
|
|
|
func (repo singleUserRepo) ListPolicies(ctx context.Context, in *mainflux.ListPoliciesReq, opts ...grpc.CallOption) (*mainflux.ListPoliciesRes, error) {
|
|
|
|
return &mainflux.ListPoliciesRes{}, errUnsupported
|
|
|
|
}
|
|
|
|
|
2020-12-29 23:02:35 +01:00
|
|
|
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
|
|
|
|
}
|