golang.org/x/oauth2@v0.18.0/jws/jws_test.go (about) 1 // Copyright 2016 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package jws 6 7 import ( 8 "crypto/rand" 9 "crypto/rsa" 10 "testing" 11 ) 12 13 func TestSignAndVerify(t *testing.T) { 14 header := &Header{ 15 Algorithm: "RS256", 16 Typ: "JWT", 17 } 18 payload := &ClaimSet{ 19 Iss: "http://google.com/", 20 Aud: "", 21 Exp: 3610, 22 Iat: 10, 23 } 24 25 privateKey, err := rsa.GenerateKey(rand.Reader, 2048) 26 if err != nil { 27 t.Fatal(err) 28 } 29 30 token, err := Encode(header, payload, privateKey) 31 if err != nil { 32 t.Fatal(err) 33 } 34 35 err = Verify(token, &privateKey.PublicKey) 36 if err != nil { 37 t.Fatal(err) 38 } 39 } 40 41 func TestVerifyFailsOnMalformedClaim(t *testing.T) { 42 err := Verify("abc.def", nil) 43 if err == nil { 44 t.Error("got no errors; want improperly formed JWT not to be verified") 45 } 46 }