Is it possible to bind the data decoded from the api_key authentication process to the request which gets passed further down into handlers?
func AuthorizeRequest(token string) (interface{}, error) {
if !strings.HasPrefix(token, "s_") {
return nil, errors.New("Invalid authentication token")
}
decodedToken, err := ValidateToken(strings.TrimPrefix(token, "s_"))
if err != nil {
return nil, errors.New("Invalid authentication token")
}
return decodedToken, nil
}
Basically, I'm using the api_key authentication as JWT authentication, since I couldn't find any way to define a schema for JWT authentication (Due to it not being available in 2.0.0. Weird, huh?)
With the JWT token authenticated, I receive basic data about the user, this allows me to handle the request further down with that user in mind.
The documentation is kind of poor in regards as to what's going on, and lots of code reading is in order. However, so far I haven't figured out what happens to the returns of the authentication function.
Where does the interface{} go? Does it get passed to anything relevant that I can mess about with? Does it get bound to Context or something? What's happening?
The only other way to solve this I figure would be to write custom middleware to validate tokens, based on the route. Which kind of defeats some of the purpose of this lib.
swagger version: 2.0.0
go version: 1.10
OS: Mac OSX
Right now my only solution is to validate the token in the Header again in my handlers, and get the information out of it that way.
Yes you can pass the information through the principal model.
See the example there which should correspond to your use case: https://github.com/go-swagger/go-swagger/tree/master/examples/composed-auth
Cheers!
This example has been pushed recently and the doc site has not been updated yet.
You should have everything in here to play with JWT with different types of auth (API keys or even OAuth2).
Feel free to contribute to the repo and enrich this example with new findings if you are working with this.