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
|
|
|
"errors"
|
2019-07-18 15:01:09 +02:00
|
|
|
"time"
|
2019-05-17 16:06:21 +02:00
|
|
|
|
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"
|
2021-10-11 16:08:26 +02:00
|
|
|
"github.com/mainflux/mainflux/things"
|
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) {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, time.Second)
|
|
|
|
defer cancel()
|
2020-10-27 19:42:53 +01:00
|
|
|
if repo.token != req.GetEmail() {
|
2019-12-16 16:22:09 +01:00
|
|
|
return nil, things.ErrUnauthorizedAccess
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-18 15:01:09 +02:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2019-05-17 16:06:21 +02:00
|
|
|
if repo.token != token.GetValue() {
|
|
|
|
return nil, things.ErrUnauthorizedAccess
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
return &mainflux.AuthorizeRes{}, errUnsupported
|
|
|
|
}
|
|
|
|
|
2021-10-27 00:38:28 +02:00
|
|
|
func (repo singleUserRepo) AddPolicy(ctx context.Context, in *mainflux.AddPolicyReq, opts ...grpc.CallOption) (*mainflux.AddPolicyRes, error) {
|
|
|
|
return &mainflux.AddPolicyRes{}, errUnsupported
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo singleUserRepo) DeletePolicy(ctx context.Context, in *mainflux.DeletePolicyReq, opts ...grpc.CallOption) (*mainflux.DeletePolicyRes, error) {
|
|
|
|
return &mainflux.DeletePolicyRes{}, 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
|
|
|
|
}
|