-
Notifications
You must be signed in to change notification settings - Fork 0
/
default_auth.go
87 lines (81 loc) · 1.99 KB
/
default_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
package authz
import (
"context"
"fmt"
"github.com/bhatti/PlexAuthZ/api/v1/services"
"github.com/bhatti/PlexAuthZ/api/v1/types"
"github.com/bhatti/PlexAuthZ/internal/domain"
"github.com/bhatti/PlexAuthZ/internal/service"
)
// DefaultAuthorizer for defining authorization rules.
type DefaultAuthorizer struct {
authAdminService service.AuthAdminService
}
// NewDefaultAuthorizer constructor
func NewDefaultAuthorizer(
authAdminService service.AuthAdminService,
) Authorizer {
return &DefaultAuthorizer{
authAdminService: authAdminService,
}
}
// Authorize checks access for principal, action and resource.
func (a *DefaultAuthorizer) Authorize(
ctx context.Context,
req *services.AuthRequest,
) (*services.AuthResponse, error) {
principal, err := a.authAdminService.GetPrincipalExt(
ctx,
req.OrganizationId,
req.Namespace,
req.PrincipalId)
if err != nil {
return nil, err
}
res, err := principal.CheckPermission(
req,
)
if err != nil {
return nil, err
}
return res, nil
}
// Check ensures constraints matches for the principal.
func (a *DefaultAuthorizer) Check(
ctx context.Context,
req *services.CheckConstraintsRequest,
) (*services.CheckConstraintsResponse, error) {
if req.Constraints == "" {
return nil, domain.NewValidationError(
fmt.Sprintf("constraints is not defined"))
}
principal, err := a.authAdminService.GetPrincipalExt(
ctx,
req.OrganizationId,
req.Namespace,
req.PrincipalId)
if err != nil {
return nil, err
}
matched, output, err := principal.CheckConstraints(
&services.AuthRequest{
OrganizationId: req.OrganizationId,
Namespace: req.Namespace,
PrincipalId: req.PrincipalId,
Context: req.Context,
},
&types.Resource{},
req.Constraints,
)
if err != nil {
return nil, err
}
if !matched {
return nil, domain.NewAuthError(
fmt.Sprintf("constraints '%s' not matched with context %v", req.Constraints, req.Context))
}
return &services.CheckConstraintsResponse{
Matched: matched,
Output: output,
}, nil
}