forked from ory/hydra
/
handler.go
175 lines (147 loc) · 4.2 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
package warden
import (
"encoding/json"
"net/http"
"strings"
"github.com/go-errors/errors"
"github.com/julienschmidt/httprouter"
"github.com/ory-am/hydra/config"
"github.com/ory-am/hydra/firewall"
"github.com/ory-am/hydra/herodot"
"github.com/ory-am/ladon"
)
const (
// TokenValidHandlerPath points to the token validation endpoint.
TokenValidHandlerPath = "/warden/token/valid"
// 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 {
*ladon.Request
*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(TokenValidHandlerPath, h.TokenValid)
r.POST(TokenAllowedHandlerPath, h.TokenAllowed)
r.POST(AllowedHandlerPath, h.Allowed)
}
func (h *WardenHandler) TokenValid(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
ctx := herodot.NewContext()
clientCtx, err := h.Warden.TokenAllowed(ctx, h.Warden.TokenFromRequest(r), &ladon.Request{
Resource: "rn:hydra:warden:token:valid",
Action: "decide",
}, "hydra.warden")
if err != nil {
h.H.WriteError(ctx, w, r, err)
return
}
var ar wardenAuthorizedRequest
if err := json.NewDecoder(r.Body).Decode(&ar); err != nil {
h.H.WriteError(ctx, w, r, err)
return
}
defer r.Body.Close()
authContext, err := h.Warden.TokenValid(ctx, ar.Token, ar.Scopes...)
if err != nil {
h.H.Write(ctx, w, r, &invalid)
return
}
authContext.Audience = clientCtx.Subject
h.H.Write(ctx, w, r, struct {
*firewall.Context
Valid bool `json:"valid"`
}{
Context: authContext,
Valid: true,
})
}
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), &ladon.Request{
Resource: "rn:hydra:warden:allowed",
Action: "decide",
}, "hydra.warden"); err != nil {
h.H.WriteError(ctx, w, r, err)
return
}
var access = new(ladon.Request)
if err := json.NewDecoder(r.Body).Decode(access); err != nil {
h.H.WriteError(ctx, w, r, errors.New(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()
clientCtx, err := h.Warden.TokenAllowed(ctx, h.Warden.TokenFromRequest(r), &ladon.Request{
Resource: "rn:hydra:warden:token:allowed",
Action: "decide",
}, "warden.token")
if err != nil {
h.H.WriteError(ctx, w, r, err)
return
}
var ar = wardenAccessRequest{
Request: new(ladon.Request),
wardenAuthorizedRequest: new(wardenAuthorizedRequest),
}
if err := json.NewDecoder(r.Body).Decode(&ar); err != nil {
h.H.WriteError(ctx, w, r, errors.New(err))
return
}
defer r.Body.Close()
authContext, err := h.Warden.TokenAllowed(ctx, ar.Token, ar.Request, ar.Scopes...)
if err != nil {
h.H.Write(ctx, w, r, ¬Allowed)
return
}
authContext.Audience = clientCtx.Subject
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]
}