github.com/djarvur/go-swagger@v0.18.0/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/go-swagger/go-swagger/examples/composed-auth/restapi/operations"
    16  
    17  	auth "github.com/go-swagger/go-swagger/examples/composed-auth/auth"
    18  	models "github.com/go-swagger/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("AddOrder called with params: %s, and principal: %s",
    71  				spew.Sdump(params.Order), spew.Sdump(principal))
    72  			return middleware.NotImplemented("operation .AddOrder has not yet been implemented")
    73  		})
    74  	api.GetAccountHandler = operations.GetAccountHandlerFunc(
    75  		func(params operations.GetAccountParams, principal *models.Principal) middleware.Responder {
    76  			log.Printf("GetAccount called with NO params, and principal: %s", spew.Sdump(principal))
    77  			return middleware.NotImplemented("operation .GetAccount has not yet been implemented")
    78  		})
    79  	api.GetItemsHandler = operations.GetItemsHandlerFunc(
    80  		func(params operations.GetItemsParams) middleware.Responder {
    81  			log.Printf("GetItems called with NO params and NO principal")
    82  			return middleware.NotImplemented("operation .GetItems has not yet been implemented")
    83  		})
    84  	api.GetOrderHandler = operations.GetOrderHandlerFunc(
    85  		func(params operations.GetOrderParams, principal *models.Principal) middleware.Responder {
    86  			log.Printf("GetOrder called with params: %s, and principal: %s",
    87  				spew.Sdump(params.OrderID), spew.Sdump(principal))
    88  			return middleware.NotImplemented("operation .GetOrder has not yet been implemented")
    89  		})
    90  	api.GetOrdersForItemHandler = operations.GetOrdersForItemHandlerFunc(
    91  		func(params operations.GetOrdersForItemParams, principal *models.Principal) middleware.Responder {
    92  			log.Printf("GetOrdersForItem called with params: %v, and principal: %v",
    93  				spew.Sdump(params.ItemID), spew.Sdump(principal))
    94  			return middleware.NotImplemented("operation .GetOrdersForItem has not yet been implemented")
    95  		})
    96  
    97  	api.ServerShutdown = func() {}
    98  
    99  	return setupGlobalMiddleware(api.Serve(setupMiddlewares))
   100  }
   101  
   102  // The TLS configuration before HTTPS server starts.
   103  func configureTLS(tlsConfig *tls.Config) {
   104  	// Make all necessary changes to the TLS configuration here.
   105  }
   106  
   107  // As soon as server is initialized but not run yet, this function will be called.
   108  // If you need to modify a config, store server instance to stop it individually later, this is the place.
   109  // This function can be called multiple times, depending on the number of serving schemes.
   110  // scheme value will be set accordingly: "http", "https" or "unix"
   111  func configureServer(s *http.Server, scheme, addr string) {
   112  }
   113  
   114  // The middleware configuration is for the handler executors. These do not apply to the swagger.json document.
   115  // The middleware executes after routing but before authentication, binding and validation
   116  func setupMiddlewares(handler http.Handler) http.Handler {
   117  	return handler
   118  }
   119  
   120  // The middleware configuration happens before anything, this middleware also applies to serving the
   121  // swagger.json document.
   122  // So this is a good place to plug in a panic handling middleware, logging and metrics
   123  func setupGlobalMiddleware(handler http.Handler) http.Handler {
   124  	return handler
   125  }