/
warden_local.go
148 lines (127 loc) · 3.62 KB
/
warden_local.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package warden
import (
"net/http"
"time"
"github.com/Sirupsen/logrus"
"github.com/ory-am/fosite"
"github.com/ory-am/hydra/firewall"
"github.com/ory-am/hydra/oauth2"
"github.com/ory-am/hydra/warden/group"
"github.com/ory-am/ladon"
"golang.org/x/net/context"
"github.com/pkg/errors"
)
type LocalWarden struct {
Warden ladon.Warden
OAuth2 fosite.OAuth2Provider
Groups group.Manager
AccessTokenLifespan time.Duration
Issuer string
}
func (w *LocalWarden) TokenFromRequest(r *http.Request) string {
return fosite.AccessTokenFromRequest(r)
}
func (w *LocalWarden) IsAllowed(ctx context.Context, a *firewall.AccessRequest) error {
if err := w.isAllowed(ctx, &ladon.Request{
Resource: a.Resource,
Action: a.Action,
Subject: a.Subject,
Context: a.Context,
}); err != nil {
logrus.WithFields(logrus.Fields{
"subject": a.Subject,
"request": a,
"reason": "The policy decision point denied the request",
}).WithError(err).Infof("Access denied")
return err
}
logrus.WithFields(logrus.Fields{
"subject": a.Subject,
"request": a,
"reason": "The policy decision point allowed the request",
}).Infof("Access allowed")
return nil
}
func (w *LocalWarden) TokenAllowed(ctx context.Context, token string, a *firewall.TokenAccessRequest, scopes ...string) (*firewall.Context, error) {
var auth, err = w.OAuth2.IntrospectToken(ctx, token, fosite.AccessToken, oauth2.NewSession(""), scopes...)
if err != nil {
logrus.WithFields(logrus.Fields{
"request": a,
"reason": "Token is expired, malformed or missing",
}).WithError(err).Infof("Access denied")
return nil, err
}
session := auth.GetSession()
if err := w.isAllowed(ctx, &ladon.Request{
Resource: a.Resource,
Action: a.Action,
Subject: session.GetSubject(),
Context: a.Context,
}); err != nil {
logrus.WithFields(logrus.Fields{
"scopes": scopes,
"subject": session.GetSubject(),
"audience": auth.GetClient().GetID(),
"request": a,
"reason": "The policy decision point denied the request",
}).WithError(err).Infof("Access denied")
return nil, err
}
c := w.newContext(auth)
logrus.WithFields(logrus.Fields{
"subject": c.Subject,
"audience": auth.GetClient().GetID(),
"request": auth,
"result": c,
}).Infof("Access granted")
return c, nil
}
func (w *LocalWarden) isAllowed(ctx context.Context, a *ladon.Request) error {
groups, err := w.Groups.FindGroupNames(a.Subject)
if err != nil {
return err
}
errs := make([]error, len(groups) + 1)
errs[0] = w.Warden.IsAllowed(&ladon.Request{
Resource: a.Resource,
Action: a.Action,
Subject: a.Subject,
Context: a.Context,
})
for k, g := range groups {
errs[k+1] = w.Warden.IsAllowed(&ladon.Request{
Resource: a.Resource,
Action: a.Action,
Subject: g,
Context: a.Context,
})
}
for _, err := range errs {
if errors.Cause(err) == ladon.ErrRequestForcefullyDenied {
return errors.Wrap(fosite.ErrRequestForbidden, err.Error())
}
}
for _, err := range errs {
if err == nil {
return nil
}
}
return errors.Wrap(fosite.ErrRequestForbidden, ladon.ErrRequestDenied.Error())
}
func (w *LocalWarden) newContext(auth fosite.AccessRequester) *firewall.Context {
session := auth.GetSession().(*oauth2.Session)
exp := auth.GetSession().GetExpiresAt(fosite.AccessToken)
if exp.IsZero() {
exp = auth.GetRequestedAt().Add(w.AccessTokenLifespan)
}
c := &firewall.Context{
Subject: session.Subject,
GrantedScopes: auth.GetGrantedScopes(),
Issuer: w.Issuer,
Audience: auth.GetClient().GetID(),
IssuedAt: auth.GetRequestedAt(),
ExpiresAt: exp,
Extra: session.Extra,
}
return c
}