-
Notifications
You must be signed in to change notification settings - Fork 0
/
svc_get_auth_info.go
58 lines (46 loc) · 1.28 KB
/
svc_get_auth_info.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
package authz
import (
"net/http"
"github.com/joomcode/errorx"
"github.com/stretchr/testify/mock"
)
type AuthInfoGetter interface {
GetAuthInfo(r *http.Request) (*AuthzInfo, error)
}
type AuthInfoGetterMock struct {
mock.Mock
}
func (m *AuthInfoGetterMock) GetAuthInfo(r *http.Request) (*AuthzInfo, error) {
args := m.Called(r)
return args.Get(0).(*AuthzInfo), args.Error(1)
}
func (m *AuthInfoGetterMock) GetAuthInfoFromRequest(r *http.Request) (*AuthzInfo, error) {
args := m.Called(r)
return args.Get(0).(*AuthzInfo), args.Error(1)
}
type AuthInfoGetterImpl struct {
UserGetter
RoleGetter
AccountGetter
}
// GetAuthInfo returns all the authentication information for a JWT token.
// It takes care of validating both token and membership of the user in the account (if provided)
func (s *AuthInfoGetterImpl) GetAuthInfo(r *http.Request) (*AuthzInfo, error) {
user, err := s.GetUser(r)
if err != nil {
return nil, errorx.Decorate(err, "token is invalid")
}
role, err := s.GetRole(r)
if err != nil {
return nil, errorx.Decorate(err, "role cannot be calculated")
}
account, err := s.GetAccount(r)
if err != nil {
return nil, errorx.Decorate(err, "account cannot be calculated")
}
return &AuthzInfo{
UserId: user,
RoleId: role,
AccountId: account,
}, nil
}