github.com/dirkm/go-swagger@v0.19.0/examples/oauth2/restapi/operations/oauth_sample_api.go (about) 1 // Code generated by go-swagger; DO NOT EDIT. 2 3 package operations 4 5 // This file was generated by the swagger tool. 6 // Editing this file might prove futile when you re-run the swagger generate command 7 8 import ( 9 "fmt" 10 "net/http" 11 "strings" 12 13 errors "github.com/go-openapi/errors" 14 loads "github.com/go-openapi/loads" 15 runtime "github.com/go-openapi/runtime" 16 middleware "github.com/go-openapi/runtime/middleware" 17 security "github.com/go-openapi/runtime/security" 18 spec "github.com/go-openapi/spec" 19 strfmt "github.com/go-openapi/strfmt" 20 "github.com/go-openapi/swag" 21 22 "github.com/go-swagger/go-swagger/examples/oauth2/restapi/operations/customers" 23 24 models "github.com/go-swagger/go-swagger/examples/oauth2/models" 25 ) 26 27 // NewOauthSampleAPI creates a new OauthSample instance 28 func NewOauthSampleAPI(spec *loads.Document) *OauthSampleAPI { 29 return &OauthSampleAPI{ 30 handlers: make(map[string]map[string]http.Handler), 31 formats: strfmt.Default, 32 defaultConsumes: "application/json", 33 defaultProduces: "application/json", 34 customConsumers: make(map[string]runtime.Consumer), 35 customProducers: make(map[string]runtime.Producer), 36 ServerShutdown: func() {}, 37 spec: spec, 38 ServeError: errors.ServeError, 39 BasicAuthenticator: security.BasicAuth, 40 APIKeyAuthenticator: security.APIKeyAuth, 41 BearerAuthenticator: security.BearerAuth, 42 JSONConsumer: runtime.JSONConsumer(), 43 JSONProducer: runtime.JSONProducer(), 44 GetAuthCallbackHandler: GetAuthCallbackHandlerFunc(func(params GetAuthCallbackParams) middleware.Responder { 45 return middleware.NotImplemented("operation GetAuthCallback has not yet been implemented") 46 }), 47 GetLoginHandler: GetLoginHandlerFunc(func(params GetLoginParams) middleware.Responder { 48 return middleware.NotImplemented("operation GetLogin has not yet been implemented") 49 }), 50 CustomersCreateHandler: customers.CreateHandlerFunc(func(params customers.CreateParams, principal *models.Principal) middleware.Responder { 51 return middleware.NotImplemented("operation CustomersCreate has not yet been implemented") 52 }), 53 CustomersGetIDHandler: customers.GetIDHandlerFunc(func(params customers.GetIDParams, principal *models.Principal) middleware.Responder { 54 return middleware.NotImplemented("operation CustomersGetID has not yet been implemented") 55 }), 56 57 OauthSecurityAuth: func(token string, scopes []string) (*models.Principal, error) { 58 return nil, errors.NotImplemented("oauth2 bearer auth (OauthSecurity) has not yet been implemented") 59 }, 60 61 // default authorizer is authorized meaning no requests are blocked 62 APIAuthorizer: security.Authorized(), 63 } 64 } 65 66 /*OauthSampleAPI the oauth sample API */ 67 type OauthSampleAPI struct { 68 spec *loads.Document 69 context *middleware.Context 70 handlers map[string]map[string]http.Handler 71 formats strfmt.Registry 72 customConsumers map[string]runtime.Consumer 73 customProducers map[string]runtime.Producer 74 defaultConsumes string 75 defaultProduces string 76 Middleware func(middleware.Builder) http.Handler 77 78 // BasicAuthenticator generates a runtime.Authenticator from the supplied basic auth function. 79 // It has a default implementation in the security package, however you can replace it for your particular usage. 80 BasicAuthenticator func(security.UserPassAuthentication) runtime.Authenticator 81 // APIKeyAuthenticator generates a runtime.Authenticator from the supplied token auth function. 82 // It has a default implementation in the security package, however you can replace it for your particular usage. 83 APIKeyAuthenticator func(string, string, security.TokenAuthentication) runtime.Authenticator 84 // BearerAuthenticator generates a runtime.Authenticator from the supplied bearer token auth function. 85 // It has a default implementation in the security package, however you can replace it for your particular usage. 86 BearerAuthenticator func(string, security.ScopedTokenAuthentication) runtime.Authenticator 87 88 // JSONConsumer registers a consumer for a "application/json" mime type 89 JSONConsumer runtime.Consumer 90 91 // JSONProducer registers a producer for a "application/json" mime type 92 JSONProducer runtime.Producer 93 94 // OauthSecurityAuth registers a function that takes an access token and a collection of required scopes and returns a principal 95 // it performs authentication based on an oauth2 bearer token provided in the request 96 OauthSecurityAuth func(string, []string) (*models.Principal, error) 97 98 // APIAuthorizer provides access control (ACL/RBAC/ABAC) by providing access to the request and authenticated principal 99 APIAuthorizer runtime.Authorizer 100 101 // GetAuthCallbackHandler sets the operation handler for the get auth callback operation 102 GetAuthCallbackHandler GetAuthCallbackHandler 103 // GetLoginHandler sets the operation handler for the get login operation 104 GetLoginHandler GetLoginHandler 105 // CustomersCreateHandler sets the operation handler for the create operation 106 CustomersCreateHandler customers.CreateHandler 107 // CustomersGetIDHandler sets the operation handler for the get Id operation 108 CustomersGetIDHandler customers.GetIDHandler 109 110 // ServeError is called when an error is received, there is a default handler 111 // but you can set your own with this 112 ServeError func(http.ResponseWriter, *http.Request, error) 113 114 // ServerShutdown is called when the HTTP(S) server is shut down and done 115 // handling all active connections and does not accept connections any more 116 ServerShutdown func() 117 118 // Custom command line argument groups with their descriptions 119 CommandLineOptionsGroups []swag.CommandLineOptionsGroup 120 121 // User defined logger function. 122 Logger func(string, ...interface{}) 123 } 124 125 // SetDefaultProduces sets the default produces media type 126 func (o *OauthSampleAPI) SetDefaultProduces(mediaType string) { 127 o.defaultProduces = mediaType 128 } 129 130 // SetDefaultConsumes returns the default consumes media type 131 func (o *OauthSampleAPI) SetDefaultConsumes(mediaType string) { 132 o.defaultConsumes = mediaType 133 } 134 135 // SetSpec sets a spec that will be served for the clients. 136 func (o *OauthSampleAPI) SetSpec(spec *loads.Document) { 137 o.spec = spec 138 } 139 140 // DefaultProduces returns the default produces media type 141 func (o *OauthSampleAPI) DefaultProduces() string { 142 return o.defaultProduces 143 } 144 145 // DefaultConsumes returns the default consumes media type 146 func (o *OauthSampleAPI) DefaultConsumes() string { 147 return o.defaultConsumes 148 } 149 150 // Formats returns the registered string formats 151 func (o *OauthSampleAPI) Formats() strfmt.Registry { 152 return o.formats 153 } 154 155 // RegisterFormat registers a custom format validator 156 func (o *OauthSampleAPI) RegisterFormat(name string, format strfmt.Format, validator strfmt.Validator) { 157 o.formats.Add(name, format, validator) 158 } 159 160 // Validate validates the registrations in the OauthSampleAPI 161 func (o *OauthSampleAPI) Validate() error { 162 var unregistered []string 163 164 if o.JSONConsumer == nil { 165 unregistered = append(unregistered, "JSONConsumer") 166 } 167 168 if o.JSONProducer == nil { 169 unregistered = append(unregistered, "JSONProducer") 170 } 171 172 if o.OauthSecurityAuth == nil { 173 unregistered = append(unregistered, "OauthSecurityAuth") 174 } 175 176 if o.GetAuthCallbackHandler == nil { 177 unregistered = append(unregistered, "GetAuthCallbackHandler") 178 } 179 180 if o.GetLoginHandler == nil { 181 unregistered = append(unregistered, "GetLoginHandler") 182 } 183 184 if o.CustomersCreateHandler == nil { 185 unregistered = append(unregistered, "customers.CreateHandler") 186 } 187 188 if o.CustomersGetIDHandler == nil { 189 unregistered = append(unregistered, "customers.GetIDHandler") 190 } 191 192 if len(unregistered) > 0 { 193 return fmt.Errorf("missing registration: %s", strings.Join(unregistered, ", ")) 194 } 195 196 return nil 197 } 198 199 // ServeErrorFor gets a error handler for a given operation id 200 func (o *OauthSampleAPI) ServeErrorFor(operationID string) func(http.ResponseWriter, *http.Request, error) { 201 return o.ServeError 202 } 203 204 // AuthenticatorsFor gets the authenticators for the specified security schemes 205 func (o *OauthSampleAPI) AuthenticatorsFor(schemes map[string]spec.SecurityScheme) map[string]runtime.Authenticator { 206 207 result := make(map[string]runtime.Authenticator) 208 for name, scheme := range schemes { 209 switch name { 210 211 case "OauthSecurity": 212 213 result[name] = o.BearerAuthenticator(scheme.Name, func(token string, scopes []string) (interface{}, error) { 214 return o.OauthSecurityAuth(token, scopes) 215 }) 216 217 } 218 } 219 return result 220 221 } 222 223 // Authorizer returns the registered authorizer 224 func (o *OauthSampleAPI) Authorizer() runtime.Authorizer { 225 226 return o.APIAuthorizer 227 228 } 229 230 // ConsumersFor gets the consumers for the specified media types 231 func (o *OauthSampleAPI) ConsumersFor(mediaTypes []string) map[string]runtime.Consumer { 232 233 result := make(map[string]runtime.Consumer) 234 for _, mt := range mediaTypes { 235 switch mt { 236 237 case "application/json": 238 result["application/json"] = o.JSONConsumer 239 240 } 241 242 if c, ok := o.customConsumers[mt]; ok { 243 result[mt] = c 244 } 245 } 246 return result 247 248 } 249 250 // ProducersFor gets the producers for the specified media types 251 func (o *OauthSampleAPI) ProducersFor(mediaTypes []string) map[string]runtime.Producer { 252 253 result := make(map[string]runtime.Producer) 254 for _, mt := range mediaTypes { 255 switch mt { 256 257 case "application/json": 258 result["application/json"] = o.JSONProducer 259 260 } 261 262 if p, ok := o.customProducers[mt]; ok { 263 result[mt] = p 264 } 265 } 266 return result 267 268 } 269 270 // HandlerFor gets a http.Handler for the provided operation method and path 271 func (o *OauthSampleAPI) HandlerFor(method, path string) (http.Handler, bool) { 272 if o.handlers == nil { 273 return nil, false 274 } 275 um := strings.ToUpper(method) 276 if _, ok := o.handlers[um]; !ok { 277 return nil, false 278 } 279 if path == "/" { 280 path = "" 281 } 282 h, ok := o.handlers[um][path] 283 return h, ok 284 } 285 286 // Context returns the middleware context for the oauth sample API 287 func (o *OauthSampleAPI) Context() *middleware.Context { 288 if o.context == nil { 289 o.context = middleware.NewRoutableContext(o.spec, o, nil) 290 } 291 292 return o.context 293 } 294 295 func (o *OauthSampleAPI) initHandlerCache() { 296 o.Context() // don't care about the result, just that the initialization happened 297 298 if o.handlers == nil { 299 o.handlers = make(map[string]map[string]http.Handler) 300 } 301 302 if o.handlers["GET"] == nil { 303 o.handlers["GET"] = make(map[string]http.Handler) 304 } 305 o.handlers["GET"]["/auth/callback"] = NewGetAuthCallback(o.context, o.GetAuthCallbackHandler) 306 307 if o.handlers["GET"] == nil { 308 o.handlers["GET"] = make(map[string]http.Handler) 309 } 310 o.handlers["GET"]["/login"] = NewGetLogin(o.context, o.GetLoginHandler) 311 312 if o.handlers["POST"] == nil { 313 o.handlers["POST"] = make(map[string]http.Handler) 314 } 315 o.handlers["POST"]["/customers"] = customers.NewCreate(o.context, o.CustomersCreateHandler) 316 317 if o.handlers["GET"] == nil { 318 o.handlers["GET"] = make(map[string]http.Handler) 319 } 320 o.handlers["GET"]["/customers"] = customers.NewGetID(o.context, o.CustomersGetIDHandler) 321 322 } 323 324 // Serve creates a http handler to serve the API over HTTP 325 // can be used directly in http.ListenAndServe(":8000", api.Serve(nil)) 326 func (o *OauthSampleAPI) Serve(builder middleware.Builder) http.Handler { 327 o.Init() 328 329 if o.Middleware != nil { 330 return o.Middleware(builder) 331 } 332 return o.context.APIHandler(builder) 333 } 334 335 // Init allows you to just initialize the handler cache, you can then recompose the middleware as you see fit 336 func (o *OauthSampleAPI) Init() { 337 if len(o.handlers) == 0 { 338 o.initHandlerCache() 339 } 340 } 341 342 // RegisterConsumer allows you to add (or override) a consumer for a media type. 343 func (o *OauthSampleAPI) RegisterConsumer(mediaType string, consumer runtime.Consumer) { 344 o.customConsumers[mediaType] = consumer 345 } 346 347 // RegisterProducer allows you to add (or override) a producer for a media type. 348 func (o *OauthSampleAPI) RegisterProducer(mediaType string, producer runtime.Producer) { 349 o.customProducers[mediaType] = producer 350 }