| 1 | package provision |
| 2 | |
| 3 | import ( |
| 4 | "crypto/rsa" |
| 5 | "reflect" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/smallstep/cli/crypto/pemutil" |
| 10 | "github.com/smallstep/cli/jose" |
| 11 | "github.com/smallstep/cli/token" |
| 12 | "github.com/stretchr/testify/assert" |
| 13 | ) |
| 14 | |
| 15 | func withFixedTime(tok *Token, t time.Time) { |
| 16 | if tok == nil { |
| 17 | return |
| 18 | } |
| 19 | tok.claims.IssuedAt = jose.NewNumericDate(t) |
| 20 | tok.claims.NotBefore = jose.NewNumericDate(t) |
| 21 | tok.claims.Expiry = jose.NewNumericDate(t.Add(5 * time.Minute)) |
| 22 | } |
| 23 | |
| 24 | func TestNew(t *testing.T) { |
| 25 | type args struct { |
| 26 | subject string |
| 27 | opts []token.Options |
| 28 | } |
| 29 | |
| 30 | now := time.Now() |
| 31 | want := &Token{ |
| 32 | claims: token.DefaultClaims(), |
| 33 | } |
| 34 | wantWithOptions := &Token{ |
| 35 | claims: token.DefaultClaims(), |
| 36 | } |
| 37 | |
| 38 | want.claims.Subject = "test.domain" |
| 39 | wantWithOptions.claims.Subject = "test.domain" |
| 40 | wantWithOptions.claims.Issuer = "new-issuer" |
| 41 | wantWithOptions.claims.ExtraClaims = map[string]interface{}{"sha": "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c"} |
| 42 | |
| 43 | tests := []struct { |
| 44 | name string |
| 45 | args args |
| 46 | want *Token |
| 47 | wantErr bool |
| 48 | }{ |
| 49 | {"ok", args{"test.domain", nil}, want, false}, |
| 50 | {"ok empty options", args{"test.domain", []token.Options{}}, want, false}, |
| 51 | {"ok with options", args{"test.domain", []token.Options{token.WithIssuer("new-issuer"), token.WithClaim("sha", "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c")}}, wantWithOptions, false}, |
| 52 | {"fail no subject", args{"", []token.Options{}}, nil, true}, |
| 53 | {"fail bad option", args{"test.domain", []token.Options{token.WithIssuer("")}}, nil, true}, |
| 54 | } |
| 55 | for _, tt := range tests { |
| 56 | t.Run(tt.name, func(t *testing.T) { |
| 57 | got, err := New(tt.args.subject, tt.args.opts...) |
| 58 | withFixedTime(got, now) |
| 59 | if (err != nil) != tt.wantErr { |
| 60 | t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr) |
| 61 | return |
| 62 | } |
| 63 | assert.Equal(t, got, tt.want) |
| 64 | if !reflect.DeepEqual(got, tt.want) { |
| 65 | t.Errorf("New() = %v, want %v", got, tt.want) |
| 66 | } |
| 67 | }) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func TestToken_SignedString(t *testing.T) { |
| 72 | type fields struct { |
| 73 | claims *token.Claims |
| 74 | } |
| 75 | type args struct { |
| 76 | sigAlg string |
| 77 | key interface{} |
| 78 | } |
| 79 | |
| 80 | rsaKey, err := pemutil.Read("../../crypto/pemutil/testdata/openssl.rsa1024.pem") |
| 81 | if err != nil { |
| 82 | t.Fatal(err) |
| 83 | } |
| 84 | |
| 85 | rsaPublic := rsaKey.(*rsa.PrivateKey).Public() |
| 86 | expected := "eyJhbGciOiJSUzI1NiIsImtpZCI6Im50U2lnZFFZNHRLOFlmTDdHQjZjNGRuZzhvSGVGOU5VMkl0QUlVOGtHZGciLCJ0eXAiOiJKV1QifQ.e30.spzx_GFrhXg_LTPBIE3z3uWaA-GH7G0rbPdskxbUahJnXRLwF8S_AAQMTjtsWY9iELwOQQUXW7aPES-jONCebTpXl00RYP7maiS87wcGW6nZ0ICmsbS5NnCDJIKpV4Ei3MZ4MXfZ4vLaONaR5BunHYkicMDqWif_2v8yvxebh7c" |
| 87 | |
| 88 | tests := []struct { |
| 89 | name string |
| 90 | fields fields |
| 91 | args args |
| 92 | want string |
| 93 | wantErr bool |
| 94 | }{ |
| 95 | {"ok", fields{&token.Claims{}}, args{"RS256", rsaKey}, expected, false}, |
| 96 | {"fail bad alg", fields{&token.Claims{}}, args{"ES256", rsaKey}, "", true}, |
| 97 | {"fail with public", fields{&token.Claims{}}, args{"RS256", rsaPublic}, "", true}, |
| 98 | } |
| 99 | for _, tt := range tests { |
| 100 | t.Run(tt.name, func(t *testing.T) { |
| 101 | tok := &Token{ |
| 102 | claims: tt.fields.claims, |
| 103 | } |
| 104 | got, err := tok.SignedString(tt.args.sigAlg, tt.args.key) |
| 105 | if (err != nil) != tt.wantErr { |
| 106 | t.Errorf("Token.SignedString() error = %v, wantErr %v", err, tt.wantErr) |
| 107 | return |
| 108 | } |
| 109 | if got != tt.want { |
| 110 | t.Errorf("Token.SignedString() = %v, want %v", got, tt.want) |
| 111 | } |
| 112 | }) |
| 113 | } |
| 114 | } |