github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/usertoken/usertoken.go (about)

     1  package usertoken
     2  
     3  import (
     4  	jwt "github.com/dgrijalva/jwt-go"
     5  	"github.com/hashicorp/errwrap"
     6  )
     7  
     8  // JSONWebToken for the user
     9  type JSONWebToken struct {
    10  	Email string `json:"sub"`
    11  	UID   string `json:"uid"`
    12  }
    13  
    14  type jsonWebToken JSONWebToken
    15  
    16  // Valid function for the JWT token
    17  func (j jsonWebToken) Valid() error {
    18  	return nil
    19  }
    20  
    21  // ParseUnsignedJSONWebToken to retrieve an user info without checking signature
    22  func ParseUnsignedJSONWebToken(accessToken string) (JSONWebToken, error) {
    23  	var claims = jsonWebToken{}
    24  	_, err := jwt.ParseWithClaims(accessToken, &claims, keyFunc)
    25  	err = filterInvalidSignatureError(err)
    26  	return JSONWebToken(claims), err
    27  }
    28  
    29  func keyFunc(token *jwt.Token) (interface{}, error) {
    30  	return []byte{}, nil
    31  }
    32  
    33  func filterInvalidSignatureError(err error) error {
    34  	// if only the bitmask for the 'signature invalid' is detected, ignore
    35  	ev, ok := err.(*jwt.ValidationError)
    36  	if ok && ev.Errors == jwt.ValidationErrorSignatureInvalid {
    37  		return nil
    38  	}
    39  
    40  	return errwrap.Wrapf("error parsing token: {{err}}", err)
    41  }