forked from ory/hydra
/
warden_local.go
170 lines (148 loc) · 4.41 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Aeneas Rekkas <aeneas+oss@aeneas.io>
* @copyright 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
* @license Apache-2.0
*/
package warden
import (
"context"
"net/http"
"time"
"github.com/ory/fosite"
"github.com/ory/hydra/firewall"
"github.com/ory/hydra/oauth2"
"github.com/ory/hydra/warden/group"
"github.com/ory/ladon"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type LocalWarden struct {
Warden ladon.Warden
OAuth2 fosite.OAuth2Provider
Groups group.Manager
AccessTokenLifespan time.Duration
Issuer string
L logrus.FieldLogger
}
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 {
w.L.WithFields(logrus.Fields{
"subject": a.Subject,
"request": a,
"reason": "The policy decision point denied the request",
}).WithError(err).Infof("Access denied")
return err
}
w.L.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 {
w.L.WithFields(logrus.Fields{
"request": a,
"scopes": scopes,
"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 {
w.L.WithFields(logrus.Fields{
"scopes": scopes,
"subject": session.GetSubject(),
"client_id": 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)
w.L.WithFields(logrus.Fields{
"subject": c.Subject,
"client_id": 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.FindGroupsByMember(a.Subject, 10000, 0)
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.ID,
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,
ClientID: auth.GetClient().GetID(),
IssuedAt: auth.GetRequestedAt(),
ExpiresAt: exp,
Extra: session.Extra,
}
return c
}