-
Notifications
You must be signed in to change notification settings - Fork 1
/
controllers.go
385 lines (349 loc) · 14.7 KB
/
controllers.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// Code generated by goagen v1.3.1, DO NOT EDIT.
//
// API "identity provider": Application Controllers
//
// Command:
// $ goagen
// --design=github.com/Microkubes/identity-provider/design
// --out=$(GOPATH)/src/github.com/Microkubes/identity-provider
// --version=v1.3.1
package app
import (
"context"
"github.com/keitaroinc/goa"
"github.com/keitaroinc/goa/cors"
"net/http"
)
// initService sets up the service encoders, decoders and mux.
func initService(service *goa.Service) {
// Setup encoders and decoders
service.Encoder.Register(goa.NewJSONEncoder, "application/json")
service.Encoder.Register(goa.NewGobEncoder, "application/gob", "application/x-gob")
service.Encoder.Register(goa.NewXMLEncoder, "application/xml")
service.Decoder.Register(goa.NewJSONDecoder, "application/json")
service.Decoder.Register(goa.NewGobDecoder, "application/gob", "application/x-gob")
service.Decoder.Register(goa.NewXMLDecoder, "application/xml")
// Setup default encoder and decoder
service.Encoder.Register(goa.NewJSONEncoder, "*/*")
service.Decoder.Register(goa.NewJSONDecoder, "*/*")
}
// IdpController is the controller interface for the Idp actions.
type IdpController interface {
goa.Muxer
AddServiceProvider(*AddServiceProviderIdpContext) error
DeleteServiceProvider(*DeleteServiceProviderIdpContext) error
DeleteSession(*DeleteSessionIdpContext) error
GetGoogleMetadata(*GetGoogleMetadataIdpContext) error
GetMetadata(*GetMetadataIdpContext) error
GetServiceProviders(*GetServiceProvidersIdpContext) error
GetSessions(*GetSessionsIdpContext) error
LoginUser(*LoginUserIdpContext) error
ServeLogin(*ServeLoginIdpContext) error
ServeLoginUser(*ServeLoginUserIdpContext) error
ServeSSO(*ServeSSOIdpContext) error
}
// MountIdpController "mounts" a Idp resource controller on the given service.
func MountIdpController(service *goa.Service, ctrl IdpController) {
initService(service)
var h goa.Handler
service.Mux.Handle("OPTIONS", "/saml/idp/services", ctrl.MuxHandler("preflight", handleIdpOrigin(cors.HandlePreflight()), nil))
service.Mux.Handle("OPTIONS", "/saml/idp/sessions", ctrl.MuxHandler("preflight", handleIdpOrigin(cors.HandlePreflight()), nil))
service.Mux.Handle("OPTIONS", "/saml/idp/metadata/google", ctrl.MuxHandler("preflight", handleIdpOrigin(cors.HandlePreflight()), nil))
service.Mux.Handle("OPTIONS", "/saml/idp/metadata", ctrl.MuxHandler("preflight", handleIdpOrigin(cors.HandlePreflight()), nil))
service.Mux.Handle("OPTIONS", "/saml/idp/login", ctrl.MuxHandler("preflight", handleIdpOrigin(cors.HandlePreflight()), nil))
service.Mux.Handle("OPTIONS", "/saml/idp/sso", ctrl.MuxHandler("preflight", handleIdpOrigin(cors.HandlePreflight()), nil))
h = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
// Check if there was an error loading the request
if err := goa.ContextError(ctx); err != nil {
return err
}
// Build the context
rctx, err := NewAddServiceProviderIdpContext(ctx, req, service)
if err != nil {
return err
}
return ctrl.AddServiceProvider(rctx)
}
h = handleIdpOrigin(h)
service.Mux.Handle("POST", "/saml/idp/services", ctrl.MuxHandler("addServiceProvider", h, nil))
service.LogInfo("mount", "ctrl", "Idp", "action", "AddServiceProvider", "route", "POST /saml/idp/services")
h = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
// Check if there was an error loading the request
if err := goa.ContextError(ctx); err != nil {
return err
}
// Build the context
rctx, err := NewDeleteServiceProviderIdpContext(ctx, req, service)
if err != nil {
return err
}
// Build the payload
if rawPayload := goa.ContextRequest(ctx).Payload; rawPayload != nil {
rctx.Payload = rawPayload.(*DeleteSPPayload)
} else {
return goa.MissingPayloadError()
}
return ctrl.DeleteServiceProvider(rctx)
}
h = handleIdpOrigin(h)
service.Mux.Handle("DELETE", "/saml/idp/services", ctrl.MuxHandler("deleteServiceProvider", h, unmarshalDeleteServiceProviderIdpPayload))
service.LogInfo("mount", "ctrl", "Idp", "action", "DeleteServiceProvider", "route", "DELETE /saml/idp/services")
h = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
// Check if there was an error loading the request
if err := goa.ContextError(ctx); err != nil {
return err
}
// Build the context
rctx, err := NewDeleteSessionIdpContext(ctx, req, service)
if err != nil {
return err
}
// Build the payload
if rawPayload := goa.ContextRequest(ctx).Payload; rawPayload != nil {
rctx.Payload = rawPayload.(*DeleteSessionPayload)
} else {
return goa.MissingPayloadError()
}
return ctrl.DeleteSession(rctx)
}
h = handleIdpOrigin(h)
service.Mux.Handle("DELETE", "/saml/idp/sessions", ctrl.MuxHandler("deleteSession", h, unmarshalDeleteSessionIdpPayload))
service.LogInfo("mount", "ctrl", "Idp", "action", "DeleteSession", "route", "DELETE /saml/idp/sessions")
h = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
// Check if there was an error loading the request
if err := goa.ContextError(ctx); err != nil {
return err
}
// Build the context
rctx, err := NewGetGoogleMetadataIdpContext(ctx, req, service)
if err != nil {
return err
}
return ctrl.GetGoogleMetadata(rctx)
}
h = handleIdpOrigin(h)
service.Mux.Handle("GET", "/saml/idp/metadata/google", ctrl.MuxHandler("getGoogleMetadata", h, nil))
service.LogInfo("mount", "ctrl", "Idp", "action", "GetGoogleMetadata", "route", "GET /saml/idp/metadata/google")
h = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
// Check if there was an error loading the request
if err := goa.ContextError(ctx); err != nil {
return err
}
// Build the context
rctx, err := NewGetMetadataIdpContext(ctx, req, service)
if err != nil {
return err
}
return ctrl.GetMetadata(rctx)
}
h = handleIdpOrigin(h)
service.Mux.Handle("GET", "/saml/idp/metadata", ctrl.MuxHandler("getMetadata", h, nil))
service.LogInfo("mount", "ctrl", "Idp", "action", "GetMetadata", "route", "GET /saml/idp/metadata")
h = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
// Check if there was an error loading the request
if err := goa.ContextError(ctx); err != nil {
return err
}
// Build the context
rctx, err := NewGetServiceProvidersIdpContext(ctx, req, service)
if err != nil {
return err
}
return ctrl.GetServiceProviders(rctx)
}
h = handleIdpOrigin(h)
service.Mux.Handle("GET", "/saml/idp/services", ctrl.MuxHandler("getServiceProviders", h, nil))
service.LogInfo("mount", "ctrl", "Idp", "action", "GetServiceProviders", "route", "GET /saml/idp/services")
h = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
// Check if there was an error loading the request
if err := goa.ContextError(ctx); err != nil {
return err
}
// Build the context
rctx, err := NewGetSessionsIdpContext(ctx, req, service)
if err != nil {
return err
}
return ctrl.GetSessions(rctx)
}
h = handleIdpOrigin(h)
service.Mux.Handle("GET", "/saml/idp/sessions", ctrl.MuxHandler("getSessions", h, nil))
service.LogInfo("mount", "ctrl", "Idp", "action", "GetSessions", "route", "GET /saml/idp/sessions")
h = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
// Check if there was an error loading the request
if err := goa.ContextError(ctx); err != nil {
return err
}
// Build the context
rctx, err := NewLoginUserIdpContext(ctx, req, service)
if err != nil {
return err
}
return ctrl.LoginUser(rctx)
}
h = handleIdpOrigin(h)
service.Mux.Handle("GET", "/saml/idp/login", ctrl.MuxHandler("loginUser", h, nil))
service.LogInfo("mount", "ctrl", "Idp", "action", "LoginUser", "route", "GET /saml/idp/login")
h = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
// Check if there was an error loading the request
if err := goa.ContextError(ctx); err != nil {
return err
}
// Build the context
rctx, err := NewServeLoginIdpContext(ctx, req, service)
if err != nil {
return err
}
return ctrl.ServeLogin(rctx)
}
h = handleIdpOrigin(h)
service.Mux.Handle("POST", "/saml/idp/sso", ctrl.MuxHandler("serveLogin", h, nil))
service.LogInfo("mount", "ctrl", "Idp", "action", "ServeLogin", "route", "POST /saml/idp/sso")
h = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
// Check if there was an error loading the request
if err := goa.ContextError(ctx); err != nil {
return err
}
// Build the context
rctx, err := NewServeLoginUserIdpContext(ctx, req, service)
if err != nil {
return err
}
return ctrl.ServeLoginUser(rctx)
}
h = handleIdpOrigin(h)
service.Mux.Handle("POST", "/saml/idp/login", ctrl.MuxHandler("serveLoginUser", h, nil))
service.LogInfo("mount", "ctrl", "Idp", "action", "ServeLoginUser", "route", "POST /saml/idp/login")
h = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
// Check if there was an error loading the request
if err := goa.ContextError(ctx); err != nil {
return err
}
// Build the context
rctx, err := NewServeSSOIdpContext(ctx, req, service)
if err != nil {
return err
}
return ctrl.ServeSSO(rctx)
}
h = handleIdpOrigin(h)
service.Mux.Handle("GET", "/saml/idp/sso", ctrl.MuxHandler("serveSSO", h, nil))
service.LogInfo("mount", "ctrl", "Idp", "action", "ServeSSO", "route", "GET /saml/idp/sso")
}
// handleIdpOrigin applies the CORS response headers corresponding to the origin.
func handleIdpOrigin(h goa.Handler) goa.Handler {
return func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
origin := req.Header.Get("Origin")
if origin == "" {
// Not a CORS request
return h(ctx, rw, req)
}
if cors.MatchOrigin(origin, "*") {
ctx = goa.WithLogContext(ctx, "origin", origin)
rw.Header().Set("Access-Control-Allow-Origin", origin)
rw.Header().Set("Access-Control-Allow-Credentials", "false")
if acrm := req.Header.Get("Access-Control-Request-Method"); acrm != "" {
// We are handling a preflight request
rw.Header().Set("Access-Control-Allow-Methods", "OPTIONS")
}
return h(ctx, rw, req)
}
return h(ctx, rw, req)
}
}
// unmarshalDeleteServiceProviderIdpPayload unmarshals the request body into the context request data Payload field.
func unmarshalDeleteServiceProviderIdpPayload(ctx context.Context, service *goa.Service, req *http.Request) error {
payload := &deleteSPPayload{}
if err := service.DecodeRequest(req, payload); err != nil {
return err
}
if err := payload.Validate(); err != nil {
// Initialize payload with private data structure so it can be logged
goa.ContextRequest(ctx).Payload = payload
return err
}
goa.ContextRequest(ctx).Payload = payload.Publicize()
return nil
}
// unmarshalDeleteSessionIdpPayload unmarshals the request body into the context request data Payload field.
func unmarshalDeleteSessionIdpPayload(ctx context.Context, service *goa.Service, req *http.Request) error {
payload := &deleteSessionPayload{}
if err := service.DecodeRequest(req, payload); err != nil {
return err
}
if err := payload.Validate(); err != nil {
// Initialize payload with private data structure so it can be logged
goa.ContextRequest(ctx).Payload = payload
return err
}
goa.ContextRequest(ctx).Payload = payload.Publicize()
return nil
}
// PublicController is the controller interface for the Public actions.
type PublicController interface {
goa.Muxer
goa.FileServer
}
// MountPublicController "mounts" a Public resource controller on the given service.
func MountPublicController(service *goa.Service, ctrl PublicController) {
initService(service)
var h goa.Handler
service.Mux.Handle("OPTIONS", "/saml/css/*filepath", ctrl.MuxHandler("preflight", handlePublicOrigin(cors.HandlePreflight()), nil))
service.Mux.Handle("OPTIONS", "/saml/js/*filepath", ctrl.MuxHandler("preflight", handlePublicOrigin(cors.HandlePreflight()), nil))
h = ctrl.FileHandler("/saml/css/*filepath", "public/css")
h = handlePublicOrigin(h)
service.Mux.Handle("GET", "/saml/css/*filepath", ctrl.MuxHandler("serve", h, nil))
service.LogInfo("mount", "ctrl", "Public", "files", "public/css", "route", "GET /saml/css/*filepath")
h = ctrl.FileHandler("/saml/js/*filepath", "public/js")
h = handlePublicOrigin(h)
service.Mux.Handle("GET", "/saml/js/*filepath", ctrl.MuxHandler("serve", h, nil))
service.LogInfo("mount", "ctrl", "Public", "files", "public/js", "route", "GET /saml/js/*filepath")
h = ctrl.FileHandler("/saml/css/", "public/css/index.html")
h = handlePublicOrigin(h)
service.Mux.Handle("GET", "/saml/css/", ctrl.MuxHandler("serve", h, nil))
service.LogInfo("mount", "ctrl", "Public", "files", "public/css/index.html", "route", "GET /saml/css/")
h = ctrl.FileHandler("/saml/js/", "public/js/index.html")
h = handlePublicOrigin(h)
service.Mux.Handle("GET", "/saml/js/", ctrl.MuxHandler("serve", h, nil))
service.LogInfo("mount", "ctrl", "Public", "files", "public/js/index.html", "route", "GET /saml/js/")
}
// handlePublicOrigin applies the CORS response headers corresponding to the origin.
func handlePublicOrigin(h goa.Handler) goa.Handler {
return func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
origin := req.Header.Get("Origin")
if origin == "" {
// Not a CORS request
return h(ctx, rw, req)
}
if cors.MatchOrigin(origin, "*") {
ctx = goa.WithLogContext(ctx, "origin", origin)
rw.Header().Set("Access-Control-Allow-Origin", origin)
rw.Header().Set("Access-Control-Allow-Credentials", "false")
if acrm := req.Header.Get("Access-Control-Request-Method"); acrm != "" {
// We are handling a preflight request
rw.Header().Set("Access-Control-Allow-Methods", "GET, POST")
}
return h(ctx, rw, req)
}
return h(ctx, rw, req)
}
}
// SwaggerController is the controller interface for the Swagger actions.
type SwaggerController interface {
goa.Muxer
goa.FileServer
}
// MountSwaggerController "mounts" a Swagger resource controller on the given service.
func MountSwaggerController(service *goa.Service, ctrl SwaggerController) {
initService(service)
var h goa.Handler
h = ctrl.FileHandler("/swagger-ui/*filepath", "swagger-ui/dist")
service.Mux.Handle("GET", "/swagger-ui/*filepath", ctrl.MuxHandler("serve", h, nil))
service.LogInfo("mount", "ctrl", "Swagger", "files", "swagger-ui/dist", "route", "GET /swagger-ui/*filepath")
h = ctrl.FileHandler("/swagger.json", "swagger/swagger.json")
service.Mux.Handle("GET", "/swagger.json", ctrl.MuxHandler("serve", h, nil))
service.LogInfo("mount", "ctrl", "Swagger", "files", "swagger/swagger.json", "route", "GET /swagger.json")
h = ctrl.FileHandler("/swagger-ui/", "swagger-ui/dist/index.html")
service.Mux.Handle("GET", "/swagger-ui/", ctrl.MuxHandler("serve", h, nil))
service.LogInfo("mount", "ctrl", "Swagger", "files", "swagger-ui/dist/index.html", "route", "GET /swagger-ui/")
}