github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/util/testhelpers/configuration/access_token.go (about)

     1  package configuration
     2  
     3  import (
     4  	"encoding/base64"
     5  	"encoding/json"
     6  	"fmt"
     7  	"time"
     8  
     9  	"github.com/SermoDigital/jose/crypto"
    10  	"github.com/SermoDigital/jose/jws"
    11  
    12  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    13  )
    14  
    15  func EncodeAccessToken(tokenInfo coreconfig.TokenInfo) (accessToken string, err error) {
    16  	tokenInfoBytes, err := json.Marshal(tokenInfo)
    17  	if err != nil {
    18  		return
    19  	}
    20  	encodedTokenInfo := base64.StdEncoding.EncodeToString(tokenInfoBytes)
    21  	accessToken = fmt.Sprintf("BEARER my_access_token.%s.baz", encodedTokenInfo)
    22  	return
    23  }
    24  
    25  // BuildTokenString builds a minimal JWT with the given time as expiration claim.
    26  func BuildTokenString(expiration time.Time) string {
    27  	c := jws.Claims{}
    28  	c.SetExpiration(expiration)
    29  	token := jws.NewJWT(c, crypto.Unsecured)
    30  	tokenBytes, _ := token.Serialize(nil)
    31  	return string(tokenBytes)
    32  }