Go-swagger: Token Authentication post auth data

Created on 26 Apr 2018  路  4Comments  路  Source: go-swagger/go-swagger

Problem statement

Is it possible to bind the data decoded from the api_key authentication process to the request which gets passed further down into handlers?

Steps to reproduce

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.

Environment

swagger version: 2.0.0
go version: 1.10
OS: Mac OSX

auth question

All 4 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alexec picture alexec  路  3Comments

flier picture flier  路  5Comments

Ragnar-BY picture Ragnar-BY  路  3Comments

alihalabyah picture alihalabyah  路  4Comments

piotrkowalczuk picture piotrkowalczuk  路  4Comments