github.com/dirkm/go-swagger@v0.19.0/examples/authentication/README.md (about)

     1  # Authentication sample
     2  
     3  Generate the code with a security principal:
     4  
     5  ```shell
     6  swagger generate server -A AuthSample -P models.Principal -f ./swagger.yml
     7  ```
     8  
     9  Edit the ./restapi/configure_auth_sample.go file
    10  
    11  ```go
    12  func configureAPI(api *operations.AuthSampleAPI) http.Handler {
    13  	// configure the api here
    14  	api.ServeError = errors.ServeError
    15  
    16  	// Set your custom logger if needed. Default one is log.Printf
    17  	// Expected interface func(string, ...interface{})
    18  	//
    19  	// Example:
    20  	api.Logger = log.Printf
    21  
    22  	api.JSONConsumer = runtime.JSONConsumer()
    23  
    24  	api.JSONProducer = runtime.JSONProducer()
    25  
    26  	// Applies when the "x-token" header is set
    27  	api.KeyAuth = func(token string) (*models.Principal, error) {
    28  		if token == "abcdefuvwxyz" {
    29  			prin := models.Principal(token)
    30  			return &prin, nil
    31  		}
    32  		api.Logger("Access attempt with incorrect api key auth: %s", token)
    33  		return nil, errors.New(401, "incorrect api key auth")
    34  	}
    35  
    36  	api.CustomersCreateHandler = customers.CreateHandlerFunc(func(params customers.CreateParams, principal *models.Principal) middleware.Responder {
    37  		return middleware.NotImplemented("operation customers.Create has not yet been implemented")
    38  	})
    39  	api.CustomersGetIDHandler = customers.GetIDHandlerFunc(func(params customers.GetIDParams, principal *models.Principal) middleware.Responder {
    40  		return middleware.NotImplemented("operation customers.GetID has not yet been implemented")
    41  	})
    42  
    43  	api.ServerShutdown = func() {}
    44  
    45  	return setupGlobalMiddleware(api.Serve(setupMiddlewares))
    46  }
    47  ```
    48  
    49  Run the server:
    50  
    51  ```shell
    52  go run ./cmd/auth-sample-server/main.go --port 35307
    53  ```
    54  
    55  Exercise auth:
    56  
    57  ```shellsession
    58  ± ivan@avalon:~  
    59   » curl -i -H 'Content-Type: application/keyauth.api.v1+json' -H 'X-Token: abcdefuvwxyz' http://127.0.0.1:35307/api/customers
    60  ```
    61  ```http
    62  HTTP/1.1 501 Not Implemented
    63  Content-Type: application/keyauth.api.v1+json
    64  Date: Fri, 25 Nov 2016 19:14:14 GMT
    65  Content-Length: 57
    66  
    67  "operation customers.GetID has not yet been implemented"
    68  ```
    69  ```shellsession
    70  ± ivan@avalon:~  
    71   » curl -i -H 'Content-Type: application/keyauth.api.v1+json' -H 'X-Token: abcdefu' http://127.0.0.1:35307/api/customers
    72  ```
    73  ```http
    74  HTTP/1.1 401 Unauthorized
    75  Content-Type: application/keyauth.api.v1+json
    76  Date: Fri, 25 Nov 2016 19:16:49 GMT
    77  Content-Length: 47
    78  
    79  {"code":401,"message":"incorrect api key auth"}       
    80  ```