-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
195 lines (169 loc) · 5.1 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
package trust
import (
"encoding/json"
"net/http"
"time"
"github.com/google/uuid"
"github.com/ory/x/errorsx"
"github.com/ory/x/pagination"
"github.com/ory/hydra/x"
"github.com/julienschmidt/httprouter"
)
const (
grantJWTBearerPath = "/trust/grants/jwt-bearer/issuers"
)
type Handler struct {
registry InternalRegistry
}
func NewHandler(r InternalRegistry) *Handler {
return &Handler{registry: r}
}
func (h *Handler) SetRoutes(admin *x.RouterAdmin) {
admin.GET(grantJWTBearerPath+"/:id", h.Get)
admin.GET(grantJWTBearerPath, h.List)
admin.POST(grantJWTBearerPath, h.Create)
admin.DELETE(grantJWTBearerPath+"/:id", h.Delete)
}
// swagger:route POST /trust/grants/jwt-bearer/issuers admin trustJwtGrantIssuer
//
// Trust an OAuth2 JWT Bearer Grant Type Issuer
//
// Use this endpoint to establish a trust relationship for a JWT issuer
// to perform JSON Web Token (JWT) Profile for OAuth 2.0 Client Authentication
// and Authorization Grants [RFC7523](https://datatracker.ietf.org/doc/html/rfc7523).
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 201: trustedJwtGrantIssuer
// 400: genericError
// 409: genericError
// 500: genericError
func (h *Handler) Create(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var grantRequest createGrantRequest
if err := json.NewDecoder(r.Body).Decode(&grantRequest); err != nil {
h.registry.Writer().WriteError(w, r, errorsx.WithStack(err))
return
}
if err := h.registry.GrantValidator().Validate(grantRequest); err != nil {
h.registry.Writer().WriteError(w, r, err)
return
}
grant := Grant{
ID: uuid.New().String(),
Issuer: grantRequest.Issuer,
Subject: grantRequest.Subject,
Scope: grantRequest.Scope,
PublicKey: PublicKey{
Set: grantRequest.Issuer, // group all keys by issuer, so set=issuer
KeyID: grantRequest.PublicKeyJWK.KeyID,
},
CreatedAt: time.Now().UTC().Round(time.Second),
ExpiresAt: grantRequest.ExpiresAt.UTC().Round(time.Second),
}
if err := h.registry.GrantManager().CreateGrant(r.Context(), grant, grantRequest.PublicKeyJWK); err != nil {
h.registry.Writer().WriteError(w, r, err)
return
}
h.registry.Writer().WriteCreated(w, r, grantJWTBearerPath+"/"+grant.ID, &grant)
}
// swagger:route GET /trust/grants/jwt-bearer/issuers/{id} admin getTrustedJwtGrantIssuer
//
// Get a Trusted OAuth2 JWT Bearer Grant Type Issuer
//
// Use this endpoint to get a trusted JWT Bearer Grant Type Issuer. The ID is the one returned when you
// created the trust relationship.
///
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 200: trustedJwtGrantIssuer
// 404: genericError
// 500: genericError
func (h *Handler) Get(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var id = ps.ByName("id")
grant, err := h.registry.GrantManager().GetConcreteGrant(r.Context(), id)
if err != nil {
h.registry.Writer().WriteError(w, r, err)
return
}
h.registry.Writer().Write(w, r, grant)
}
// swagger:route DELETE /trust/grants/jwt-bearer/issuers/{id} admin deleteTrustedJwtGrantIssuer
//
// Delete a Trusted OAuth2 JWT Bearer Grant Type Issuer
//
// Use this endpoint to delete trusted JWT Bearer Grant Type Issuer. The ID is the one returned when you
// created the trust relationship.
//
// Once deleted, the associated issuer will no longer be able to perform the JSON Web Token (JWT) Profile
// for OAuth 2.0 Client Authentication and Authorization Grant.
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 204: emptyResponse
// 404: genericError
// 500: genericError
func (h *Handler) Delete(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var id = ps.ByName("id")
if err := h.registry.GrantManager().DeleteGrant(r.Context(), id); err != nil {
h.registry.Writer().WriteError(w, r, err)
return
}
w.WriteHeader(http.StatusNoContent)
}
// swagger:route GET /trust/grants/jwt-bearer/issuers admin listTrustedJwtGrantIssuers
//
// List Trusted OAuth2 JWT Bearer Grant Type Issuers
//
// Use this endpoint to list all trusted JWT Bearer Grant Type Issuers.
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 200: trustedJwtGrantIssuers
// 500: genericError
func (h *Handler) List(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
limit, offset := pagination.Parse(r, 100, 0, 500)
optionalIssuer := r.URL.Query().Get("issuer")
grants, err := h.registry.GrantManager().GetGrants(r.Context(), limit, offset, optionalIssuer)
if err != nil {
h.registry.Writer().WriteError(w, r, err)
return
}
n, err := h.registry.GrantManager().CountGrants(r.Context())
if err != nil {
h.registry.Writer().WriteError(w, r, err)
return
}
pagination.Header(w, r.URL, n, limit, offset)
if grants == nil {
grants = []Grant{}
}
h.registry.Writer().Write(w, r, grants)
}