-
Notifications
You must be signed in to change notification settings - Fork 28
/
authz.go
75 lines (61 loc) · 2 KB
/
authz.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
package service
import (
"context"
"errors"
"github.com/authgear/authgear-server/pkg/api/apierrors"
"github.com/authgear/authgear-server/pkg/portal/model"
"github.com/authgear/authgear-server/pkg/portal/session"
)
var ErrForbidden = apierrors.Forbidden.WithReason("Forbidden").New("forbidden")
var ErrUnauthenticated = apierrors.Unauthorized.WithReason("Unauthenticated").New("unauthenticated")
type AuthzConfigService interface {
GetStaticAppIDs() ([]string, error)
}
type AuthzCollaboratorService interface {
NewCollaborator(appID string, userID string, role model.CollaboratorRole) *model.Collaborator
CreateCollaborator(c *model.Collaborator) error
ListCollaboratorsByUser(userID string) ([]*model.Collaborator, error)
GetCollaboratorByAppAndUser(appID string, userID string) (*model.Collaborator, error)
}
type AuthzService struct {
Context context.Context
Configs AuthzConfigService
Collaborators AuthzCollaboratorService
}
func (s *AuthzService) ListAuthorizedApps(userID string) ([]string, error) {
appIDs, err := s.Configs.GetStaticAppIDs()
if errors.Is(err, ErrGetStaticAppIDsNotSupported) {
var cs []*model.Collaborator
cs, err = s.Collaborators.ListCollaboratorsByUser(userID)
if err == nil {
appIDs = make([]string, len(cs))
for i, c := range cs {
appIDs[i] = c.AppID
}
}
}
if err != nil {
return nil, err
}
return appIDs, nil
}
func (s *AuthzService) AddAuthorizedUser(appID string, userID string, role model.CollaboratorRole) error {
c := s.Collaborators.NewCollaborator(appID, userID, role)
return s.Collaborators.CreateCollaborator(c)
}
func (s *AuthzService) CheckAccessOfViewer(appID string) (userID string, err error) {
sessionInfo := session.GetValidSessionInfo(s.Context)
if sessionInfo == nil {
err = ErrUnauthenticated
return
}
userID = sessionInfo.UserID
_, err = s.Collaborators.GetCollaboratorByAppAndUser(appID, userID)
if errors.Is(err, ErrCollaboratorNotFound) {
err = ErrForbidden
return
} else if err != nil {
return
}
return
}