github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/examples/composed-auth/restapi/configure_multi_auth_example.go (about) 1 // This file is safe to edit. Once it exists it will not be overwritten 2 3 package restapi 4 5 import ( 6 "crypto/tls" 7 "log" 8 "net/http" 9 10 "github.com/davecgh/go-spew/spew" 11 errors "github.com/go-openapi/errors" 12 runtime "github.com/go-openapi/runtime" 13 middleware "github.com/go-openapi/runtime/middleware" 14 15 "github.com/thetreep/go-swagger/examples/composed-auth/restapi/operations" 16 17 auth "github.com/thetreep/go-swagger/examples/composed-auth/auth" 18 models "github.com/thetreep/go-swagger/examples/composed-auth/models" 19 ) 20 21 //go:generate swagger generate server --target .. --name multi-auth-example --spec ../swagger.yml --principal models.Principal 22 23 func configureFlags(api *operations.MultiAuthExampleAPI) { 24 // api.CommandLineOptionsGroups = []swag.CommandLineOptionsGroup{ ... } 25 } 26 27 func configureAPI(api *operations.MultiAuthExampleAPI) http.Handler { 28 // configure the api here 29 api.ServeError = errors.ServeError 30 31 // Set your custom logger if needed. Default one is log.Printf 32 // Expected interface func(string, ...interface{}) 33 api.Logger = log.Printf 34 35 api.JSONConsumer = runtime.JSONConsumer() 36 37 api.JSONProducer = runtime.JSONProducer() 38 39 api.HasRoleAuth = func(token string, scopes []string) (*models.Principal, error) { 40 // The header: Authorization: Bearer {base64 string} (or ?access_token={base 64 string} param) has already 41 // been decoded by the runtime as a token 42 api.Logger("HasRoleAuth handler called") 43 return auth.HasRole(token, scopes) 44 } 45 // Applies when the Authorization header is set with the Basic scheme 46 api.IsRegisteredAuth = func(user string, pass string) (*models.Principal, error) { 47 // The header: Authorization: Basic {base64 string} has already been decoded by the runtime as a 48 // username:password pair 49 api.Logger("IsRegisteredAuth handler called") 50 return auth.IsRegistered(user, pass) 51 } 52 // Applies when the "X-Custom-Key" header is set 53 api.IsResellerAuth = func(token string) (*models.Principal, error) { 54 api.Logger("IsResellerAuth handler called") 55 return auth.IsReseller(token) 56 } 57 // Applies when the "CustomKeyAsQuery" query is set 58 api.IsResellerQueryAuth = func(token string) (*models.Principal, error) { 59 api.Logger("ResellerQueryAuth handler called") 60 return auth.IsReseller(token) 61 } 62 63 // Set your custom authorizer if needed. Default one is security.Authorized() 64 // Expected interface runtime.Authorizer 65 // 66 // Example: 67 // api.APIAuthorizer = security.Authorized() 68 api.AddOrderHandler = operations.AddOrderHandlerFunc( 69 func(params operations.AddOrderParams, principal *models.Principal) middleware.Responder { 70 log.Printf( 71 "AddOrder called with params: %s, and principal: %s", 72 spew.Sdump(params.Order), spew.Sdump(principal), 73 ) 74 return middleware.NotImplemented("operation .AddOrder has not yet been implemented") 75 }, 76 ) 77 api.GetAccountHandler = operations.GetAccountHandlerFunc( 78 func(params operations.GetAccountParams, principal *models.Principal) middleware.Responder { 79 log.Printf("GetAccount called with NO params, and principal: %s", spew.Sdump(principal)) 80 return middleware.NotImplemented("operation .GetAccount has not yet been implemented") 81 }, 82 ) 83 api.GetItemsHandler = operations.GetItemsHandlerFunc( 84 func(params operations.GetItemsParams) middleware.Responder { 85 log.Printf("GetItems called with NO params and NO principal") 86 return middleware.NotImplemented("operation .GetItems has not yet been implemented") 87 }, 88 ) 89 api.GetOrderHandler = operations.GetOrderHandlerFunc( 90 func(params operations.GetOrderParams, principal *models.Principal) middleware.Responder { 91 log.Printf( 92 "GetOrder called with params: %s, and principal: %s", 93 spew.Sdump(params.OrderID), spew.Sdump(principal), 94 ) 95 return middleware.NotImplemented("operation .GetOrder has not yet been implemented") 96 }, 97 ) 98 api.GetOrdersForItemHandler = operations.GetOrdersForItemHandlerFunc( 99 func(params operations.GetOrdersForItemParams, principal *models.Principal) middleware.Responder { 100 log.Printf( 101 "GetOrdersForItem called with params: %v, and principal: %v", 102 spew.Sdump(params.ItemID), spew.Sdump(principal), 103 ) 104 return middleware.NotImplemented("operation .GetOrdersForItem has not yet been implemented") 105 }, 106 ) 107 108 api.ServerShutdown = func() {} 109 110 return setupGlobalMiddleware(api.Serve(setupMiddlewares)) 111 } 112 113 // The TLS configuration before HTTPS server starts. 114 func configureTLS(tlsConfig *tls.Config) { 115 // Make all necessary changes to the TLS configuration here. 116 } 117 118 // As soon as server is initialized but not run yet, this function will be called. 119 // If you need to modify a config, store server instance to stop it individually later, this is the place. 120 // This function can be called multiple times, depending on the number of serving schemes. 121 // scheme value will be set accordingly: "http", "https" or "unix". 122 func configureServer(s *http.Server, scheme, addr string) { 123 } 124 125 // The middleware configuration is for the handler executors. These do not apply to the swagger.json document. 126 // The middleware executes after routing but before authentication, binding and validation. 127 func setupMiddlewares(handler http.Handler) http.Handler { 128 return handler 129 } 130 131 // The middleware configuration happens before anything, this middleware also applies to serving the 132 // swagger.json document. 133 // So this is a good place to plug in a panic handling middleware, logging and metrics. 134 func setupGlobalMiddleware(handler http.Handler) http.Handler { 135 return handler 136 }