github.com/arunkumar7540/cli@v6.45.0+incompatible/integration/helpers/token.go (about)

     1  package helpers
     2  
     3  import (
     4  	"strings"
     5  	"time"
     6  
     7  	"github.com/SermoDigital/jose/crypto"
     8  	"github.com/SermoDigital/jose/jws"
     9  	"github.com/SermoDigital/jose/jwt"
    10  	. "github.com/onsi/gomega"
    11  )
    12  
    13  // BuildTokenString returns a string typed JSON web token with the specified expiration time
    14  func BuildTokenString(expiration time.Time) string {
    15  	c := jws.Claims{}
    16  	c.SetExpiration(expiration)
    17  	c.Set("user_name", "some-user")
    18  	token := jws.NewJWT(c, crypto.Unsecured)
    19  	tokenBytes, err := token.Serialize(nil)
    20  	Expect(err).NotTo(HaveOccurred())
    21  	return string(tokenBytes)
    22  }
    23  
    24  // ParseTokenString takes a string typed token and returns a jwt.JWT struct representation of that token
    25  func ParseTokenString(token string) jwt.JWT {
    26  	strippedToken := strings.TrimPrefix(token, "bearer ")
    27  	jwt, err := jws.ParseJWT([]byte(strippedToken))
    28  	Expect(err).NotTo(HaveOccurred())
    29  	return jwt
    30  }