-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
207 lines (184 loc) · 5.35 KB
/
handler.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package warden
import (
"encoding/json"
"net/http"
"strings"
"github.com/julienschmidt/httprouter"
"github.com/ory/herodot"
"github.com/ory/hydra/config"
"github.com/ory/hydra/firewall"
"github.com/pkg/errors"
)
const (
// TokenAllowedHandlerPath points to the token access request validation endpoint.
TokenAllowedHandlerPath = "/warden/token/allowed"
// AllowedHandlerPath points to the access request validation endpoint.
AllowedHandlerPath = "/warden/allowed"
)
type wardenAuthorizedRequest struct {
// Scopes is an array of scopes that are requried.
Scopes []string `json:"scopes"`
// Token is the token to introspect.
Token string `json:"token"`
}
type wardenAccessRequest struct {
*firewall.TokenAccessRequest
*wardenAuthorizedRequest
}
var notAllowed = struct {
Allowed bool `json:"allowed"`
}{Allowed: false}
// WardenHandler is capable of handling HTTP request and validating access tokens and access requests.
type WardenHandler struct {
H herodot.Writer
Warden firewall.Firewall
}
func NewHandler(c *config.Config, router *httprouter.Router) *WardenHandler {
ctx := c.Context()
h := &WardenHandler{
H: herodot.NewJSONWriter(c.GetLogger()),
Warden: ctx.Warden,
}
h.SetRoutes(router)
return h
}
func (h *WardenHandler) SetRoutes(r *httprouter.Router) {
r.POST(TokenAllowedHandlerPath, h.TokenAllowed)
r.POST(AllowedHandlerPath, h.Allowed)
}
// swagger:route POST /warden/allowed warden wardenAllowed
//
// Check if a subject is allowed to do something
//
// Checks if an arbitrary subject is allowed to perform an action on a resource. This endpoint requires a subject,
// a resource name, an action name and a context.If the subject is not allowed to perform the action on the resource,
// this endpoint returns a 200 response with `{ "allowed": false} }`.
//
// The subject making the request needs to be assigned to a policy containing:
//
// ```
// {
// "resources": ["rn:hydra:warden:allowed"],
// "actions": ["decide"],
// "effect": "allow"
// }
// ```
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Security:
// oauth2: hydra.warden
//
// Responses:
// 200: wardenAllowedResponse
// 401: genericError
// 403: genericError
// 500: genericError
func (h *WardenHandler) Allowed(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var ctx = r.Context()
if _, err := h.Warden.TokenAllowed(ctx, h.Warden.TokenFromRequest(r), &firewall.TokenAccessRequest{
Resource: "rn:hydra:warden:allowed",
Action: "decide",
}, "hydra.warden"); err != nil {
h.H.WriteError(w, r, err)
return
}
var access = new(firewall.AccessRequest)
if err := json.NewDecoder(r.Body).Decode(access); err != nil {
h.H.WriteError(w, r, errors.WithStack(err))
return
}
defer r.Body.Close()
if err := h.Warden.IsAllowed(ctx, access); err != nil {
h.H.Write(w, r, ¬Allowed)
return
}
res := notAllowed
res.Allowed = true
h.H.Write(w, r, &res)
}
// swagger:route POST /warden/token/allowed warden wardenTokenAllowed
//
// Check if the subject of a token is allowed to do something
//
// Checks if a token is valid and if the token owner is allowed to perform an action on a resource.
// This endpoint requires a token, a scope, a resource name, an action name and a context.
//
// If a token is expired/invalid, has not been granted the requested scope or the subject is not allowed to
// perform the action on the resource, this endpoint returns a 200 response with `{ "allowed": false} }`.
//
// Extra data set through the `at_ext` claim in the consent response will be included in the response.
// The `id_ext` claim will never be returned by this endpoint.
//
// The subject making the request needs to be assigned to a policy containing:
//
// ```
// {
// "resources": ["rn:hydra:warden:token:allowed"],
// "actions": ["decide"],
// "effect": "allow"
// }
// ```
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Security:
// oauth2: hydra.warden
//
// Responses:
// 200: wardenTokenAllowedResponse
// 401: genericError
// 403: genericError
// 500: genericError
func (h *WardenHandler) TokenAllowed(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
ctx := r.Context()
_, err := h.Warden.TokenAllowed(ctx, h.Warden.TokenFromRequest(r), &firewall.TokenAccessRequest{
Resource: "rn:hydra:warden:token:allowed",
Action: "decide",
}, "hydra.warden")
if err != nil {
h.H.WriteError(w, r, err)
return
}
var ar = wardenAccessRequest{
TokenAccessRequest: new(firewall.TokenAccessRequest),
wardenAuthorizedRequest: new(wardenAuthorizedRequest),
}
if err := json.NewDecoder(r.Body).Decode(&ar); err != nil {
h.H.WriteError(w, r, errors.WithStack(err))
return
}
defer r.Body.Close()
authContext, err := h.Warden.TokenAllowed(ctx, ar.Token, ar.TokenAccessRequest, ar.Scopes...)
if err != nil {
h.H.Write(w, r, ¬Allowed)
return
}
h.H.Write(w, r, struct {
*firewall.Context
Allowed bool `json:"allowed"`
}{
Context: authContext,
Allowed: true,
})
}
func TokenFromRequest(r *http.Request) string {
auth := r.Header.Get("Authorization")
split := strings.SplitN(auth, " ", 2)
if len(split) != 2 || !strings.EqualFold(split[0], "bearer") {
return ""
}
return split[1]
}