2012-04-18 03:49:21 +04:00
|
|
|
package jwt
|
|
|
|
|
|
|
|
import (
|
2012-04-18 23:35:16 +04:00
|
|
|
"fmt"
|
2014-06-03 18:21:48 +04:00
|
|
|
"io/ioutil"
|
2012-04-18 23:35:16 +04:00
|
|
|
"net/http"
|
2012-04-18 23:59:37 +04:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2014-03-08 02:43:11 +04:00
|
|
|
"time"
|
2012-04-18 03:49:21 +04:00
|
|
|
)
|
|
|
|
|
2014-10-12 00:54:16 +04:00
|
|
|
var (
|
|
|
|
jwtTestDefaultKey []byte
|
|
|
|
defaultKeyFunc Keyfunc = func(t *Token) (interface{}, error) { return jwtTestDefaultKey, nil }
|
|
|
|
emptyKeyFunc Keyfunc = func(t *Token) (interface{}, error) { return nil, nil }
|
|
|
|
errorKeyFunc Keyfunc = func(t *Token) (interface{}, error) { return nil, fmt.Errorf("error loading key") }
|
|
|
|
nilKeyFunc Keyfunc = nil
|
|
|
|
)
|
|
|
|
|
2012-04-18 23:59:37 +04:00
|
|
|
var jwtTestData = []struct {
|
2014-03-08 02:43:11 +04:00
|
|
|
name string
|
|
|
|
tokenString string
|
2014-10-12 00:54:16 +04:00
|
|
|
keyfunc Keyfunc
|
2014-03-08 02:43:11 +04:00
|
|
|
claims map[string]interface{}
|
|
|
|
valid bool
|
|
|
|
validationError *ValidationError
|
2012-04-18 23:18:31 +04:00
|
|
|
}{
|
|
|
|
{
|
2012-04-18 23:35:16 +04:00
|
|
|
"basic",
|
2012-04-18 23:18:31 +04:00
|
|
|
"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJmb28iOiJiYXIifQ.FhkiHkoESI_cG3NPigFrxEk9Z60_oXrOT2vGm9Pn6RDgYNovYORQmmA0zs1AoAOf09ly2Nx2YAg6ABqAYga1AcMFkJljwxTT5fYphTuqpWdy4BELeSYJx5Ty2gmr8e7RonuUztrdD5WfPqLKMm1Ozp_T6zALpRmwTIW0QPnaBXaQD90FplAg46Iy1UlDKr-Eupy0i5SLch5Q-p2ZpaL_5fnTIUDlxC3pWhJTyx_71qDI-mAA_5lE_VdroOeflG56sSmDxopPEG3bFlSu1eowyBfxtu0_CuVd-M42RU75Zc4Gsj6uV77MBtbMrf4_7M_NUTSgoIF3fRqxrj0NzihIBg",
|
2014-10-12 00:54:16 +04:00
|
|
|
defaultKeyFunc,
|
2012-04-18 23:18:31 +04:00
|
|
|
map[string]interface{}{"foo": "bar"},
|
|
|
|
true,
|
2014-03-08 02:43:11 +04:00
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"basic expired",
|
|
|
|
"", // autogen
|
2014-10-12 00:54:16 +04:00
|
|
|
defaultKeyFunc,
|
2014-03-08 02:43:11 +04:00
|
|
|
map[string]interface{}{"foo": "bar", "exp": float64(time.Now().Unix() - 100)},
|
|
|
|
false,
|
2014-03-09 23:24:51 +04:00
|
|
|
&ValidationError{Errors: ValidationErrorExpired},
|
2014-03-08 02:43:11 +04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"basic nbf",
|
|
|
|
"", // autogen
|
2014-10-12 00:54:16 +04:00
|
|
|
defaultKeyFunc,
|
2014-03-08 02:43:11 +04:00
|
|
|
map[string]interface{}{"foo": "bar", "nbf": float64(time.Now().Unix() + 100)},
|
|
|
|
false,
|
2014-03-09 23:24:51 +04:00
|
|
|
&ValidationError{Errors: ValidationErrorNotValidYet},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"expired and nbf",
|
|
|
|
"", // autogen
|
2014-10-12 00:54:16 +04:00
|
|
|
defaultKeyFunc,
|
2014-03-09 23:24:51 +04:00
|
|
|
map[string]interface{}{"foo": "bar", "nbf": float64(time.Now().Unix() + 100), "exp": float64(time.Now().Unix() - 100)},
|
|
|
|
false,
|
|
|
|
&ValidationError{Errors: ValidationErrorNotValidYet | ValidationErrorExpired},
|
2012-04-18 23:18:31 +04:00
|
|
|
},
|
|
|
|
{
|
2012-04-18 23:35:16 +04:00
|
|
|
"basic invalid",
|
2012-04-18 23:18:31 +04:00
|
|
|
"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJmb28iOiJiYXIifQ.EhkiHkoESI_cG3NPigFrxEk9Z60_oXrOT2vGm9Pn6RDgYNovYORQmmA0zs1AoAOf09ly2Nx2YAg6ABqAYga1AcMFkJljwxTT5fYphTuqpWdy4BELeSYJx5Ty2gmr8e7RonuUztrdD5WfPqLKMm1Ozp_T6zALpRmwTIW0QPnaBXaQD90FplAg46Iy1UlDKr-Eupy0i5SLch5Q-p2ZpaL_5fnTIUDlxC3pWhJTyx_71qDI-mAA_5lE_VdroOeflG56sSmDxopPEG3bFlSu1eowyBfxtu0_CuVd-M42RU75Zc4Gsj6uV77MBtbMrf4_7M_NUTSgoIF3fRqxrj0NzihIBg",
|
2014-10-12 00:54:16 +04:00
|
|
|
defaultKeyFunc,
|
|
|
|
map[string]interface{}{"foo": "bar"},
|
|
|
|
false,
|
|
|
|
&ValidationError{Errors: ValidationErrorSignatureInvalid},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"basic nokeyfunc",
|
|
|
|
"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJmb28iOiJiYXIifQ.FhkiHkoESI_cG3NPigFrxEk9Z60_oXrOT2vGm9Pn6RDgYNovYORQmmA0zs1AoAOf09ly2Nx2YAg6ABqAYga1AcMFkJljwxTT5fYphTuqpWdy4BELeSYJx5Ty2gmr8e7RonuUztrdD5WfPqLKMm1Ozp_T6zALpRmwTIW0QPnaBXaQD90FplAg46Iy1UlDKr-Eupy0i5SLch5Q-p2ZpaL_5fnTIUDlxC3pWhJTyx_71qDI-mAA_5lE_VdroOeflG56sSmDxopPEG3bFlSu1eowyBfxtu0_CuVd-M42RU75Zc4Gsj6uV77MBtbMrf4_7M_NUTSgoIF3fRqxrj0NzihIBg",
|
|
|
|
nilKeyFunc,
|
|
|
|
map[string]interface{}{"foo": "bar"},
|
|
|
|
false,
|
|
|
|
&ValidationError{Errors: ValidationErrorUnverifiable},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"basic nokey",
|
|
|
|
"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJmb28iOiJiYXIifQ.FhkiHkoESI_cG3NPigFrxEk9Z60_oXrOT2vGm9Pn6RDgYNovYORQmmA0zs1AoAOf09ly2Nx2YAg6ABqAYga1AcMFkJljwxTT5fYphTuqpWdy4BELeSYJx5Ty2gmr8e7RonuUztrdD5WfPqLKMm1Ozp_T6zALpRmwTIW0QPnaBXaQD90FplAg46Iy1UlDKr-Eupy0i5SLch5Q-p2ZpaL_5fnTIUDlxC3pWhJTyx_71qDI-mAA_5lE_VdroOeflG56sSmDxopPEG3bFlSu1eowyBfxtu0_CuVd-M42RU75Zc4Gsj6uV77MBtbMrf4_7M_NUTSgoIF3fRqxrj0NzihIBg",
|
|
|
|
emptyKeyFunc,
|
2012-04-18 23:18:31 +04:00
|
|
|
map[string]interface{}{"foo": "bar"},
|
|
|
|
false,
|
2014-03-09 23:24:51 +04:00
|
|
|
&ValidationError{Errors: ValidationErrorSignatureInvalid},
|
2012-04-18 23:18:31 +04:00
|
|
|
},
|
2014-10-12 00:54:16 +04:00
|
|
|
{
|
|
|
|
"basic errorkey",
|
|
|
|
"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJmb28iOiJiYXIifQ.FhkiHkoESI_cG3NPigFrxEk9Z60_oXrOT2vGm9Pn6RDgYNovYORQmmA0zs1AoAOf09ly2Nx2YAg6ABqAYga1AcMFkJljwxTT5fYphTuqpWdy4BELeSYJx5Ty2gmr8e7RonuUztrdD5WfPqLKMm1Ozp_T6zALpRmwTIW0QPnaBXaQD90FplAg46Iy1UlDKr-Eupy0i5SLch5Q-p2ZpaL_5fnTIUDlxC3pWhJTyx_71qDI-mAA_5lE_VdroOeflG56sSmDxopPEG3bFlSu1eowyBfxtu0_CuVd-M42RU75Zc4Gsj6uV77MBtbMrf4_7M_NUTSgoIF3fRqxrj0NzihIBg",
|
|
|
|
errorKeyFunc,
|
|
|
|
map[string]interface{}{"foo": "bar"},
|
|
|
|
false,
|
|
|
|
&ValidationError{Errors: ValidationErrorUnverifiable},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
var e error
|
|
|
|
if jwtTestDefaultKey, e = ioutil.ReadFile("test/sample_key.pub"); e != nil {
|
|
|
|
panic(e)
|
|
|
|
}
|
2012-04-18 23:18:31 +04:00
|
|
|
}
|
2012-04-18 03:49:21 +04:00
|
|
|
|
2014-03-08 02:43:11 +04:00
|
|
|
func makeSample(c map[string]interface{}) string {
|
2014-06-03 18:21:48 +04:00
|
|
|
key, e := ioutil.ReadFile("test/sample_key")
|
|
|
|
if e != nil {
|
|
|
|
panic(e.Error())
|
|
|
|
}
|
2014-03-08 02:43:11 +04:00
|
|
|
|
2014-12-27 22:14:23 +03:00
|
|
|
token := New(SigningMethodRS256)
|
2014-03-08 02:43:11 +04:00
|
|
|
token.Claims = c
|
|
|
|
s, e := token.SignedString(key)
|
|
|
|
|
|
|
|
if e != nil {
|
|
|
|
panic(e.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2012-04-18 23:18:31 +04:00
|
|
|
func TestJWT(t *testing.T) {
|
|
|
|
for _, data := range jwtTestData {
|
2014-03-08 02:43:11 +04:00
|
|
|
if data.tokenString == "" {
|
|
|
|
data.tokenString = makeSample(data.claims)
|
|
|
|
}
|
2014-10-12 00:54:16 +04:00
|
|
|
token, err := Parse(data.tokenString, data.keyfunc)
|
2012-04-18 23:59:37 +04:00
|
|
|
|
2012-04-18 23:18:31 +04:00
|
|
|
if !reflect.DeepEqual(data.claims, token.Claims) {
|
|
|
|
t.Errorf("[%v] Claims mismatch. Expecting: %v Got: %v", data.name, data.claims, token.Claims)
|
|
|
|
}
|
|
|
|
if data.valid && err != nil {
|
2014-03-08 02:43:11 +04:00
|
|
|
t.Errorf("[%v] Error while verifying token: %T:%v", data.name, err, err)
|
2012-04-18 23:18:31 +04:00
|
|
|
}
|
|
|
|
if !data.valid && err == nil {
|
|
|
|
t.Errorf("[%v] Invalid token passed validation", data.name)
|
|
|
|
}
|
2014-03-08 02:43:11 +04:00
|
|
|
if data.validationError != nil {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("[%v] Expecting error. Didn't get one.", data.name)
|
|
|
|
} else {
|
|
|
|
// perform deep equal without the string bit
|
|
|
|
err.(*ValidationError).err = ""
|
|
|
|
if !reflect.DeepEqual(data.validationError, err) {
|
|
|
|
t.Errorf("[%v] Errors don't match expectation", data.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2012-04-18 23:18:31 +04:00
|
|
|
}
|
2012-04-18 03:49:21 +04:00
|
|
|
}
|
2012-04-18 23:35:16 +04:00
|
|
|
|
|
|
|
func TestParseRequest(t *testing.T) {
|
|
|
|
// Bearer token request
|
|
|
|
for _, data := range jwtTestData {
|
2014-03-08 02:43:11 +04:00
|
|
|
if data.tokenString == "" {
|
|
|
|
data.tokenString = makeSample(data.claims)
|
|
|
|
}
|
|
|
|
|
2012-04-18 23:35:16 +04:00
|
|
|
r, _ := http.NewRequest("GET", "/", nil)
|
|
|
|
r.Header.Set("Authorization", fmt.Sprintf("Bearer %v", data.tokenString))
|
2014-10-12 00:54:16 +04:00
|
|
|
token, err := ParseFromRequest(r, data.keyfunc)
|
2012-04-18 23:59:37 +04:00
|
|
|
|
2012-04-18 23:35:16 +04:00
|
|
|
if !reflect.DeepEqual(data.claims, token.Claims) {
|
|
|
|
t.Errorf("[%v] Claims mismatch. Expecting: %v Got: %v", data.name, data.claims, token.Claims)
|
|
|
|
}
|
|
|
|
if data.valid && err != nil {
|
|
|
|
t.Errorf("[%v] Error while verifying token: %v", data.name, err)
|
|
|
|
}
|
|
|
|
if !data.valid && err == nil {
|
|
|
|
t.Errorf("[%v] Invalid token passed validation", data.name)
|
|
|
|
}
|
|
|
|
}
|
2012-04-18 23:59:37 +04:00
|
|
|
}
|