go-chi / jwtauth

JWT authentication middleware for Go HTTP services
MIT License
543 stars 91 forks source link

Add Custom Parser #36

Closed n33pm closed 3 years ago

n33pm commented 5 years ago

Add custom Parser with custom Claims struct 😬

type Parser struct {
    orgParser *jwt.Parser
}

func (p *Parser) Parse(tokenString string, keyFunc jwt.Keyfunc) (*jwt.Token, error) {
    return p.orgParser.ParseWithClaims(tokenString, &JWTClaims{}, keyFunc)
}

type JWTClaims struct {
    User User `json:"user"`
    jwt.StandardClaims
}

type User struct {
    Name string   `json:"name"`
    Email       string   `json:"email"`
}

func init() {
    parser := &Parser{
        &jwt.Parser{},
    }
    TokenAuth = jwtauth.NewWithParser(jwt.SigningMethodHS256.Name, parser, []byte("secret"), nil)
}
pkieltyka commented 3 years ago

underlying jwt library has changed, see HEAD