-
Notifications
You must be signed in to change notification settings - Fork 13
/
auth.go
91 lines (73 loc) · 2.69 KB
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package mocks
import (
"context"
"github.com/MainfluxLabs/mainflux"
"github.com/MainfluxLabs/mainflux/pkg/errors"
"github.com/MainfluxLabs/mainflux/users"
"github.com/golang/protobuf/ptypes/empty"
"google.golang.org/grpc"
)
var _ mainflux.AuthServiceClient = (*authServiceMock)(nil)
type authServiceMock struct {
roles map[string]string
usersByEmail map[string]users.User
}
// NewAuthService creates mock of users service.
func NewAuthService(adminID string, userList []users.User) mainflux.AuthServiceClient {
usersByEmail := make(map[string]users.User)
roles := map[string]string{"root": adminID}
for _, user := range userList {
usersByEmail[user.Email] = user
}
return &authServiceMock{
roles: roles,
usersByEmail: usersByEmail,
}
}
func (svc authServiceMock) Identify(ctx context.Context, in *mainflux.Token, opts ...grpc.CallOption) (*mainflux.UserIdentity, error) {
if u, ok := svc.usersByEmail[in.Value]; ok {
return &mainflux.UserIdentity{Id: u.ID, Email: u.Email}, nil
}
return nil, errors.ErrAuthentication
}
func (svc authServiceMock) Issue(ctx context.Context, in *mainflux.IssueReq, opts ...grpc.CallOption) (*mainflux.Token, error) {
if u, ok := svc.usersByEmail[in.GetEmail()]; ok {
switch in.Type {
default:
return &mainflux.Token{Value: u.Email}, nil
}
}
return nil, errors.ErrAuthentication
}
func (svc authServiceMock) Authorize(ctx context.Context, req *mainflux.AuthorizeReq, _ ...grpc.CallOption) (r *empty.Empty, err error) {
u, ok := svc.usersByEmail[req.Token]
if !ok {
return &empty.Empty{}, errors.ErrAuthentication
}
switch req.Subject {
case "root":
if svc.roles["root"] != u.ID {
return &empty.Empty{}, errors.ErrAuthorization
}
default:
return &empty.Empty{}, errors.ErrAuthorization
}
return &empty.Empty{}, nil
}
func (svc authServiceMock) Members(ctx context.Context, req *mainflux.MembersReq, _ ...grpc.CallOption) (r *mainflux.MembersRes, err error) {
panic("not implemented")
}
func (svc authServiceMock) Assign(ctx context.Context, req *mainflux.Assignment, _ ...grpc.CallOption) (r *empty.Empty, err error) {
panic("not implemented")
}
func (svc authServiceMock) AddPolicy(ctx context.Context, in *mainflux.PolicyReq, opts ...grpc.CallOption) (r *empty.Empty, err error) {
panic("not implemented")
}
func (svc authServiceMock) AssignRole(ctx context.Context, in *mainflux.AssignRoleReq, opts ...grpc.CallOption) (r *empty.Empty, err error) {
panic("not implemented")
}
func (svc authServiceMock) RetrieveRole(ctx context.Context, req *mainflux.RetrieveRoleReq, _ ...grpc.CallOption) (r *mainflux.RetrieveRoleRes, err error) {
panic("not implemented")
}