github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/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  	c.Set("user_id", "some-guid")
    19  	c.Set("origin", "uaa")
    20  	token := jws.NewJWT(c, crypto.Unsecured)
    21  	tokenBytes, err := token.Serialize(nil)
    22  	Expect(err).NotTo(HaveOccurred())
    23  	return string(tokenBytes)
    24  }
    25  
    26  // ParseTokenString takes a string typed token and returns a jwt.JWT struct representation of that token
    27  func ParseTokenString(token string) jwt.JWT {
    28  	strippedToken := strings.TrimPrefix(token, "bearer ")
    29  	jwt, err := jws.ParseJWT([]byte(strippedToken))
    30  	Expect(err).NotTo(HaveOccurred())
    31  	return jwt
    32  }