File: key_test.go

package info (click to toggle)
golang-github-coreos-go-oidc 0.0~git20160926.0.16c5ecc-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 436 kB
  • sloc: sh: 40; makefile: 5
file content (103 lines) | stat: -rw-r--r-- 2,075 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package key

import (
	"crypto/rsa"
	"math/big"
	"reflect"
	"testing"
	"time"

	"github.com/coreos/go-oidc/jose"
)

func TestPrivateRSAKeyJWK(t *testing.T) {
	n := big.NewInt(int64(17))
	if n == nil {
		panic("NewInt returned nil")
	}

	k := &PrivateKey{
		KeyID: "foo",
		PrivateKey: &rsa.PrivateKey{
			PublicKey: rsa.PublicKey{N: n, E: 65537},
		},
	}

	want := jose.JWK{
		ID:       "foo",
		Type:     "RSA",
		Alg:      "RS256",
		Use:      "sig",
		Modulus:  n,
		Exponent: 65537,
	}

	got := k.JWK()
	if !reflect.DeepEqual(want, got) {
		t.Fatalf("JWK mismatch: want=%#v got=%#v", want, got)
	}
}

func TestPublicKeySetKey(t *testing.T) {
	n := big.NewInt(int64(17))
	if n == nil {
		panic("NewInt returned nil")
	}

	k := jose.JWK{
		ID:       "foo",
		Type:     "RSA",
		Alg:      "RS256",
		Use:      "sig",
		Modulus:  n,
		Exponent: 65537,
	}
	now := time.Now().UTC()
	ks := NewPublicKeySet([]jose.JWK{k}, now)

	want := &PublicKey{jwk: k}
	got := ks.Key("foo")
	if !reflect.DeepEqual(want, got) {
		t.Errorf("Unexpected response from PublicKeySet.Key: want=%#v got=%#v", want, got)
	}

	got = ks.Key("bar")
	if got != nil {
		t.Errorf("Expected nil response from PublicKeySet.Key, got %#v", got)
	}
}

func TestPublicKeyMarshalJSON(t *testing.T) {
	k := jose.JWK{
		ID:       "foo",
		Type:     "RSA",
		Alg:      "RS256",
		Use:      "sig",
		Modulus:  big.NewInt(int64(17)),
		Exponent: 65537,
	}
	want := `{"kid":"foo","kty":"RSA","alg":"RS256","use":"sig","e":"AQAB","n":"EQ=="}`
	pubKey := NewPublicKey(k)
	gotBytes, err := pubKey.MarshalJSON()
	if err != nil {
		t.Fatalf("failed to marshal public key: %v", err)
	}
	got := string(gotBytes)
	if got != want {
		t.Errorf("got != want:\n%s\n%s", got, want)
	}
}

func TestGeneratePrivateKeyIDs(t *testing.T) {
	key1, err := GeneratePrivateKey()
	if err != nil {
		t.Fatalf("GeneratePrivateKey(): %v", err)
	}
	key2, err := GeneratePrivateKey()
	if err != nil {
		t.Fatalf("GeneratePrivateKey(): %v", err)
	}
	if key1.KeyID == key2.KeyID {
		t.Fatalf("expected different keys to have different key IDs")
	}
}