123456789101112131415161718192021222324252627282930313233343536373839 |
- package main
- import (
- "fmt"
- "github.com/dgrijalva/jwt-go"
- "strconv"
- )
- type TokenClaims struct {
- ID float64 `json:"id"`
- jwt.StandardClaims
- }
- func main() {
- c, err := parseToken("eyJhbGciOiJIUzI1NiIsImlhdCI6MTUzNzg0Njc4MywiZXhwIjoxNTM5MDU2MzgzfQ.eyJpZCI6MzI0NTEzNH0.RqRoad8H5oAGY6L3qMLcxCUYE5Fl7-MXtpSyfSU4aqU")
- if nil != err {
- println(err)
- }
- fmt.Printf("%v\n", c)
- fmt.Printf("%v", strconv.FormatFloat(c.ID, 'f', 0, 64))
- }
- func parseToken(tokenString string) (*TokenClaims, error) {
- token, err := jwt.ParseWithClaims(tokenString, &TokenClaims{}, func(token *jwt.Token) (interface{}, error) {
- if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
- return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
- }
- return []byte("hard to guess string"), nil
- })
- if err != nil {
- return nil, err
- }
- if claims, ok := token.Claims.(*TokenClaims); ok && token.Valid {
- return claims, nil
- }
- return nil, nil
- }
|