github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/auth/token/spec/token_source.go (about)

     1  package spec
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/golang-jwt/jwt"
     8  	testkeys "github.com/qri-io/qri/auth/key/test"
     9  	"github.com/qri-io/qri/auth/token"
    10  	"github.com/qri-io/qri/profile"
    11  )
    12  
    13  // AssertTokenSourceSpec ensures a TokenSource implementation behaves as
    14  // expected
    15  func AssertTokenSourceSpec(t *testing.T, newTokenSource func(ctx context.Context) token.Source) {
    16  	ctx, cancel := context.WithCancel(context.Background())
    17  	defer cancel()
    18  
    19  	source := newTokenSource(ctx)
    20  
    21  	p1 := &profile.Profile{
    22  		ID:       profile.IDB58DecodeOrEmpty(testkeys.GetKeyData(1).EncodedPeerID),
    23  		Peername: "username",
    24  	}
    25  
    26  	raw, err := source.CreateToken(p1, 0)
    27  	if err != nil {
    28  		t.Errorf("source should allow creating key with valid profile & zero duration. got: %q", err)
    29  	}
    30  
    31  	p := &jwt.Parser{
    32  		UseJSONNumber:        true,
    33  		SkipClaimsValidation: false,
    34  	}
    35  	if _, _, err := p.ParseUnverified(raw, &token.Claims{}); err != nil {
    36  		t.Errorf("created token must parse with token.Claims. got: %q", err)
    37  	}
    38  
    39  	if _, err := token.Parse(raw, source); err != nil {
    40  		t.Errorf("source must create tokens that parse with it's own verification keys. error: %q", err)
    41  	}
    42  }