/*-
 * Copyright 2016 Zbigniew Mandziejewicz
 * Copyright 2016 Square, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jwt

import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"encoding/hex"
	"encoding/pem"
	"errors"
	"io"
	"reflect"
	"sort"
	"testing"
	"time"

	"github.com/go-jose/go-jose/v4/testutils/assert"
	"github.com/go-jose/go-jose/v4/testutils/require"

	"github.com/go-jose/go-jose/v4"
	"github.com/go-jose/go-jose/v4/json"
)

type testClaims struct {
	Subject string `json:"sub"`
}

func (tc1 testClaims) Equal(tc2 testClaims) bool {
	return tc1.Subject == tc2.Subject
}

type invalidMarshalClaims struct {
}

var errInvalidMarshalClaims = errors.New("failed marshaling invalid claims")

func (c invalidMarshalClaims) MarshalJSON() ([]byte, error) {
	return nil, errInvalidMarshalClaims
}

var sampleClaims = Claims{
	Subject:  "42",
	IssuedAt: NewNumericDate(time.Date(2016, 1, 1, 0, 0, 0, 0, time.UTC)),
	Issuer:   "issuer",
	Audience: Audience{"a1", "a2"},
}

type numberClaims struct {
	Int   int64   `json:"int"`
	Float float64 `json:"float"`
}

func TestIntegerAndFloatsNormalize(t *testing.T) {
	c := numberClaims{1 << 60, 12345.6789}

	normalized, err := normalize(c)
	if err != nil {
		t.Fatal(err)
	}

	ni, err := (normalized["int"].(json.Number)).Int64()
	if err != nil {
		t.Fatal(err)
	}

	nf, err := (normalized["float"].(json.Number)).Float64()
	if err != nil {
		t.Fatal(err)
	}

	if ni != c.Int {
		t.Errorf("normalize failed to preserve int64 (got %v, wanted %v, type %s)", normalized["int"], c.Int, reflect.TypeOf(normalized["int"]))
	}
	if nf != c.Float {
		t.Errorf("normalize failed to preserve float64 (got %v, wanted %v, type %s)", normalized["float"], c.Float, reflect.TypeOf(normalized["float"]))
	}
}

func TestBuilderCustomClaimsNonPointer(t *testing.T) {
	jwt, err := Signed(rsaSigner).Claims(testClaims{"foo"}).Serialize()
	require.NoError(t, err, "Error creating JWT.")

	parsed, err := ParseSigned(jwt, []jose.SignatureAlgorithm{jose.RS256})
	require.NoError(t, err, "Error parsing JWT.")

	out := &testClaims{}
	if assert.NoError(t, parsed.Claims(&testPrivRSAKey1.PublicKey, out), "Error unmarshaling claims.") {
		assert.Equal(t, "foo", out.Subject)
	}
}

func TestBuilderCustomClaimsPointer(t *testing.T) {
	jwt, err := Signed(rsaSigner).Claims(&testClaims{"foo"}).Serialize()
	require.NoError(t, err, "Error creating JWT.")

	parsed, err := ParseSigned(jwt, []jose.SignatureAlgorithm{jose.RS256})
	require.NoError(t, err, "Error parsing JWT.")

	out := &testClaims{}
	if assert.NoError(t, parsed.Claims(&testPrivRSAKey1.PublicKey, out), "Error unmarshaling claims.") {
		assert.Equal(t, "foo", out.Subject)
	}
}

func TestBuilderMergeClaims(t *testing.T) {
	jwt, err := Signed(rsaSigner).
		Claims(&Claims{
			Subject: "42",
		}).
		Claims(map[string]interface{}{
			"Scopes": []string{"read:users"},
		}).
		Serialize()
	require.NoError(t, err, "Error creating JWT.")

	parsed, err := ParseSigned(jwt, []jose.SignatureAlgorithm{jose.RS256})
	require.NoError(t, err, "Error parsing JWT.")

	out := make(map[string]interface{})
	if assert.NoError(t, parsed.Claims(&testPrivRSAKey1.PublicKey, &out), "Error unmarshaling claims.") {
		assert.EqualJSON(t, map[string]interface{}{
			"sub":    "42",
			"Scopes": []interface{}{"read:users"},
		}, out)
	}

	_, err = Signed(rsaSigner).Claims("invalid-claims").Claims(&testClaims{"foo"}).Serialize()
	assert.Equal(t, err, ErrInvalidClaims)

	_, err = Signed(rsaSigner).Claims(&invalidMarshalClaims{}).Serialize()
	assert.Equal(t, err.Error(), "json: error calling MarshalJSON for type *jwt.invalidMarshalClaims: failed marshaling invalid claims")
}

func TestBuilderSignedAndEncrypted(t *testing.T) {
	encryptionKey := []byte("itsa16bytesecret" + "itsa16bytesecret")
	recipient := jose.Recipient{
		Algorithm: jose.DIRECT,
		Key:       encryptionKey,
	}
	encrypter, err := jose.NewEncrypter(jose.A128CBC_HS256, recipient, (&jose.EncrypterOptions{}).WithContentType("JWT").WithType("JWT"))
	require.NoError(t, err, "Error creating encrypter.")

	jwt1, err := SignedAndEncrypted(rsaSigner, encrypter).Claims(&testClaims{"foo"}).Token()
	require.NoError(t, err, "Error marshaling signed-then-encrypted token.")
	jwt1Serialized, err := jwt1.enc.CompactSerialize()
	require.NoError(t, err, "Error serializing signed-then-encrypted token.")
	jwt1Deserialized, err := ParseSignedAndEncrypted(jwt1Serialized,
		[]jose.KeyAlgorithm{jose.DIRECT},
		[]jose.ContentEncryption{jose.A128CBC_HS256},
		[]jose.SignatureAlgorithm{jose.RS256})
	require.NoError(t, err, "Error parsing signed-then-encrypted token.")
	if nested, err := jwt1Deserialized.Decrypt(encryptionKey); assert.NoError(t, err, "Error decrypting signed-then-encrypted token.") {
		out := &testClaims{}
		assert.NoError(t, nested.Claims(&testPrivRSAKey1.PublicKey, out))
		assert.Equal(t, testClaims{"foo"}, *out)
	}

	b := SignedAndEncrypted(rsaSigner, encrypter).Claims(&testClaims{"foo"})
	tok1, err := b.Serialize()
	if assert.NoError(t, err) {
		jwt, err := ParseSignedAndEncrypted(
			tok1,
			[]jose.KeyAlgorithm{jose.DIRECT},
			[]jose.ContentEncryption{jose.A128CBC_HS256},
			[]jose.SignatureAlgorithm{jose.RS256})
		if assert.NoError(t, err, "Error parsing signed-then-encrypted compact token.") {
			if nested, err := jwt.Decrypt(encryptionKey); assert.NoError(t, err) {
				out := &testClaims{}
				assert.NoError(t, nested.Claims(&testPrivRSAKey1.PublicKey, out))
				assert.Equal(t, testClaims{"foo"}, *out)
			}
		}
	}

	tok2, err := b.Serialize()
	if assert.NoError(t, err) {
		jwe, err := ParseSignedAndEncrypted(
			tok2,
			[]jose.KeyAlgorithm{jose.DIRECT},
			[]jose.ContentEncryption{jose.A128CBC_HS256},
			[]jose.SignatureAlgorithm{jose.RS256})
		if assert.NoError(t, err, "Error parsing signed-then-encrypted full token.") {
			expected := []jose.Header{{
				Algorithm: string(jose.DIRECT),
				ExtraHeaders: map[jose.HeaderKey]interface{}{
					jose.HeaderType:        "JWT",
					jose.HeaderContentType: "JWT",
					"enc":                  "A128CBC-HS256",
				},
			}}
			if !reflect.DeepEqual(jwe.Headers, expected) {
				t.Errorf("headers from ParseSignedAndEncrypted() = %v, want %v", jwe.Headers, expected)
			}
			if jws, err := jwe.Decrypt(encryptionKey); assert.NoError(t, err) {
				expected := []jose.Header{{
					Algorithm: string(jose.RS256),
					ExtraHeaders: map[jose.HeaderKey]interface{}{
						jose.HeaderType: "JWT",
					},
				}}
				if !reflect.DeepEqual(jws.Headers, expected) {
					t.Errorf("headers from Decrypt() = %v, want %v", jws.Headers, expected)
				}
				out := &testClaims{}
				assert.NoError(t, jws.Claims(&testPrivRSAKey1.PublicKey, out))
				assert.Equal(t, testClaims{"foo"}, *out)
			}
		}
	}

	b2 := SignedAndEncrypted(rsaSigner, encrypter).Claims(&invalidMarshalClaims{})
	_, err = b2.Serialize()
	assert.Equal(t, err.Error(), "json: error calling MarshalJSON for type *jwt.invalidMarshalClaims: failed marshaling invalid claims")
	_, err = b2.Serialize()
	assert.Equal(t, err.Error(), "json: error calling MarshalJSON for type *jwt.invalidMarshalClaims: failed marshaling invalid claims")

	encrypter2, err := jose.NewEncrypter(jose.A128CBC_HS256, recipient, nil)
	require.NoError(t, err, "Error creating encrypter.")
	_, err = SignedAndEncrypted(rsaSigner, encrypter2).Serialize()
	assert.Equal(t, err.Error(), "go-jose/go-jose/jwt: expected content type to be JWT (cty header)")
}

func TestBuilderHeadersSigner(t *testing.T) {
	tests := []struct {
		Keys   []*rsa.PrivateKey
		Claims interface{}
	}{
		{
			Keys:   []*rsa.PrivateKey{testPrivRSAKey1},
			Claims: &Claims{Issuer: "foo"},
		},
		{
			Keys:   []*rsa.PrivateKey{testPrivRSAKey2},
			Claims: &Claims{Issuer: "foo"},
		},
	}

	for i, tc := range tests {
		wantKeyIDs := make([]string, len(tc.Keys))
		signingKeys := make([]jose.SigningKey, len(tc.Keys))

		for j, key := range tc.Keys {
			keyIDBytes := make([]byte, 20)
			if _, err := io.ReadFull(rand.Reader, keyIDBytes); err != nil {
				t.Fatalf("failed to read random bytes: %v", err)
			}
			keyID := hex.EncodeToString(keyIDBytes)

			wantKeyIDs[j] = keyID
			signingKeys[j] = jose.SigningKey{
				Algorithm: jose.RS256,
				Key: &jose.JSONWebKey{
					KeyID:     keyID,
					Algorithm: "RSA",
					Key:       key,
				},
			}
		}

		signer, err := jose.NewMultiSigner(signingKeys, nil)
		if err != nil {
			t.Errorf("case %d: NewMultiSigner(): %v", i, err)
			continue
		}

		var token string
		token, err = Signed(signer).Claims(tc.Claims).Serialize()
		if err != nil {
			t.Errorf("case %d: failed to create token: %v", i, err)
			continue
		}
		jws, err := jose.ParseSignedCompact(token, []jose.SignatureAlgorithm{jose.RS256})
		if err != nil {
			t.Errorf("case %d: parse signed: %v", i, err)
			continue
		}
		gotKeyIDs := make([]string, len(jws.Signatures))
		for i, sig := range jws.Signatures {
			gotKeyIDs[i] = sig.Header.KeyID
		}
		sort.Strings(wantKeyIDs)
		sort.Strings(gotKeyIDs)
		if !reflect.DeepEqual(wantKeyIDs, gotKeyIDs) {
			t.Errorf("case %d: wanted=%q got=%q", i, wantKeyIDs, gotKeyIDs)
		}
	}
}

func TestBuilderHeadersEncrypter(t *testing.T) {
	key := testPrivRSAKey1
	claims := &Claims{Issuer: "foo"}

	keyIDBytes := make([]byte, 20)
	if _, err := io.ReadFull(rand.Reader, keyIDBytes); err != nil {
		t.Fatalf("failed to read random bytes: %v", err)
	}
	keyID := hex.EncodeToString(keyIDBytes)

	wantKeyID := keyID
	recipient := jose.Recipient{
		Algorithm: jose.RSA1_5,
		Key:       key.Public(),
		KeyID:     keyID,
	}

	wantType := jose.ContentType("JWT")
	encrypter, err := jose.NewEncrypter(jose.A128CBC_HS256, recipient, (&jose.EncrypterOptions{}).WithType(wantType))
	require.NoError(t, err, "failed to create encrypter")

	token, err := Encrypted(encrypter).Claims(claims).Serialize()
	require.NoError(t, err, "failed to create token")

	jwe, err := jose.ParseEncrypted(token, []jose.KeyAlgorithm{jose.RSA1_5}, []jose.ContentEncryption{jose.A128CBC_HS256})
	if assert.NoError(t, err, "error parsing encrypted token") {
		expected := jose.Header{
			ExtraHeaders: map[jose.HeaderKey]interface{}{
				jose.HeaderType: string(wantType),
				"enc":           "A128CBC-HS256",
			},
			Algorithm: string(jose.RSA1_5),
			KeyID:     wantKeyID,
		}
		if !reflect.DeepEqual(jwe.Header, expected) {
			t.Errorf("header from ParseEncrypted() = %v, want %v", jwe.Header, expected)
		}
	}
}

func BenchmarkMapClaims(b *testing.B) {
	m := map[string]interface{}{
		"sub": "42",
		"iat": 1451606400,
		"iss": "issuer",
		"aud": []string{"a1", "a2"},
	}

	for i := 0; i < b.N; i++ {
		Signed(rsaSigner).Claims(m)
	}
}

func BenchmarkStructClaims(b *testing.B) {
	for i := 0; i < b.N; i++ {
		Signed(rsaSigner).Claims(sampleClaims)
	}
}

func BenchmarkSignedSerializeRSA(b *testing.B) {
	tb := Signed(rsaSigner).Claims(sampleClaims)

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		if _, err := tb.Serialize(); err != nil {
			b.Fatal(err)
		}
	}
}

func BenchmarkSignedSerializeSHA(b *testing.B) {
	tb := Signed(hmacSigner).Claims(sampleClaims)

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		if _, err := tb.Serialize(); err != nil {
			b.Fatal(err)
		}
	}
}

func mustUnmarshalRSA(data string) *rsa.PrivateKey {
	block, _ := pem.Decode([]byte(data))
	if block == nil {
		panic("failed to decode PEM data")
	}
	key, err := x509.ParsePKCS8PrivateKey(block.Bytes)
	if err != nil {
		panic("failed to parse RSA key: " + err.Error())
	}
	if key, ok := key.(*rsa.PrivateKey); ok {
		return key
	}
	panic("key is not of type *rsa.PrivateKey")
}

func mustMakeSigner(alg jose.SignatureAlgorithm, k interface{}) jose.Signer {
	sig, err := jose.NewSigner(jose.SigningKey{Algorithm: alg, Key: k}, (&jose.SignerOptions{}).WithType("JWT"))
	if err != nil {
		panic("failed to create signer:" + err.Error())
	}

	return sig
}

var (
	sharedKey           = []byte("0102030405060708090A0B0C0D0E0F10")
	sharedEncryptionKey = []byte("itsa16bytesecret")

	testPrivRSAKey1 = mustUnmarshalRSA(`-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDIHBvDHAr7jh8h
xaqBCl11fjI9YZtdC5b3HtXTXZW3c2dIOImNUjffT8POP6p5OpzivmC1om7iOyuZ
3nJjC9LT3zqqs3f2i5d4mImxEuqG6uWdryFfkp0uIv5VkjVO+iQWd6pDAPGP7r1Z
foXCleyCtmyNH4JSkJneNPOk/4BxO8vcvRnCMT/Gv81IT6H+OQ6OovWOuJr8RX9t
1wuCjC9ezZxeI9ONffhiO5FMrVh5H9LJTl3dPOVa4aEcOvgd45hBmvxAyXqf8daE
6Kl2O7vQ4uwgnSTVXYIIjCjbepuersApIMGx/XPSgiU1K3Xtah/TBvep+S3VlwPc
q/QH25S9AgMBAAECggEAe+y8XKYfPw4SxY1uPB+5JSwT3ON3nbWxtjSIYy9Pqp5z
Vcx9kuFZ7JevQSk4X38m7VzM8282kC/ono+d8yy9Uayq3k/qeOqV0X9Vti1qxEbw
ECkG1/MqGApfy4qSLOjINInDDV+mOWa2KJgsKgdCwuhKbVMYGB2ozG2qfYIlfvlY
vLcBEpGWmswJHNmkcjTtGFIyJgPbsI6ndkkOeQbqQKAaadXtG1xUzH+vIvqaUl/l
AkNf+p4qhPkHsoAWXf1qu9cYa2T8T+mEo79Awl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