forked from cperfect/hydra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
135 lines (112 loc) · 3.24 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
package warden
import (
"encoding/json"
"net/http"
"strings"
"github.com/julienschmidt/httprouter"
"github.com/ory-am/hydra/config"
"github.com/ory-am/hydra/firewall"
"github.com/ory-am/hydra/herodot"
"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 []string `json:"scopes"`
Token string `json:"token"`
}
type wardenAccessRequest struct {
*firewall.TokenAccessRequest
*wardenAuthorizedRequest
}
var notAllowed = struct {
Allowed bool `json:"allowed"`
}{Allowed: false}
var invalid = struct {
Valid bool `json:"valid"`
}{Valid: false}
// WardenHandler is capable of handling HTTP request and validating access tokens and access requests.
type WardenHandler struct {
H herodot.Herodot
Warden firewall.Firewall
}
func NewHandler(c *config.Config, router *httprouter.Router) *WardenHandler {
ctx := c.Context()
h := &WardenHandler{
H: &herodot.JSON{},
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)
}
func (h *WardenHandler) Allowed(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var ctx = herodot.NewContext()
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(ctx, w, r, err)
return
}
var access = new(firewall.AccessRequest)
if err := json.NewDecoder(r.Body).Decode(access); err != nil {
h.H.WriteError(ctx, w, r, errors.Wrap(err, ""))
return
}
defer r.Body.Close()
if err := h.Warden.IsAllowed(ctx, access); err != nil {
h.H.Write(ctx, w, r, ¬Allowed)
return
}
res := notAllowed
res.Allowed = true
h.H.Write(ctx, w, r, &res)
}
func (h *WardenHandler) TokenAllowed(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
ctx := herodot.NewContext()
_, 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(ctx, 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(ctx, w, r, errors.Wrap(err, ""))
return
}
defer r.Body.Close()
authContext, err := h.Warden.TokenAllowed(ctx, ar.Token, ar.TokenAccessRequest, ar.Scopes...)
if err != nil {
h.H.Write(ctx, w, r, ¬Allowed)
return
}
h.H.Write(ctx, 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]
}