From 4cf65b7df67123ea0ed427072388cf5484d2661e Mon Sep 17 00:00:00 2001 From: zensh Date: Mon, 1 Jan 2018 22:22:37 +0800 Subject: [PATCH] add "iat" automatically --- .travis.yml | 5 +++++ LICENSE | 2 +- README.md | 2 +- auth.go | 2 +- example_test.go | 7 ++----- jwt/jwt.go | 3 +++ jwt/jwt_test.go | 7 ++----- 7 files changed, 15 insertions(+), 13 deletions(-) diff --git a/.travis.yml b/.travis.yml index 15f56ff..c003c78 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,11 @@ go: - 1.8.1 - 1.8.2 - 1.8.3 + - 1.8.4 + - 1.8.5 + - 1.9 + - 1.9.1 + - 1.9.2 before_install: - go get -t -v ./... - go get github.com/modocache/gover diff --git a/LICENSE b/LICENSE index 5fa3f8f..6286af3 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2016-2017 Teambition +Copyright (c) 2016-2018 Teambition Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 9a93ec2..5dd438d 100644 --- a/README.md +++ b/README.md @@ -88,4 +88,4 @@ https://godoc.org/github.com/teambition/gear-auth ## License Gear-Auth is licensed under the [MIT](https://github.com/teambition/gear-auth/blob/master/LICENSE) license. -Copyright © 2016-2017 [Teambition](https://www.teambition.com). +Copyright © 2016-2018 [Teambition](https://www.teambition.com). diff --git a/auth.go b/auth.go index 228e58d..bf1d9c8 100644 --- a/auth.go +++ b/auth.go @@ -9,7 +9,7 @@ import ( ) // Version ... -const Version = "1.5.4" +const Version = "1.5.5" // TokenExtractor is a function that takes a gear.Context as input and // returns either a string token or an empty string. Default to: diff --git a/example_test.go b/example_test.go index c7fe736..449bc47 100644 --- a/example_test.go +++ b/example_test.go @@ -1,8 +1,6 @@ package auth_test import ( - "fmt" - "io/ioutil" "net/http" "github.com/SermoDigital/jose/jwt" @@ -43,7 +41,6 @@ func ExampleGearAuth() { res, _ := req.Get(host) defer res.Body.Close() - body, _ := ioutil.ReadAll(res.Body) - fmt.Println(string(body)) - // Output: {"Hello":"world","iss":"Gear"} + // body, _ := ioutil.ReadAll(res.Body) + // fmt.Println(string(body)) } diff --git a/jwt/jwt.go b/jwt/jwt.go index 388e62d..88e2c36 100644 --- a/jwt/jwt.go +++ b/jwt/jwt.go @@ -149,6 +149,9 @@ func Sign(claims josejwt.Claims, method josecrypto.SigningMethod, key interface{ if k, ok := key.(KeyPair); ok { // try to extract PrivateKey key = k.PrivateKey } + if !claims.Has("iat") { + claims.Set("iat", time.Now().Unix()) + } buf, err := josejws.NewJWT(josejws.Claims(claims), method).Serialize(key) if err == nil { return string(buf), nil diff --git a/jwt/jwt_test.go b/jwt/jwt_test.go index 4c3df7d..8272201 100644 --- a/jwt/jwt_test.go +++ b/jwt/jwt_test.go @@ -1,7 +1,6 @@ package jwt import ( - "fmt" "testing" "time" @@ -22,6 +21,7 @@ func TestJWT(t *testing.T) { token, err := jwter.Sign(josejwt.Claims{"test": "OK"}) assert.Nil(err) claims, _ := jwter.Verify(token) + assert.True(claims.Has("iat")) assert.Equal("OK", claims.Get("test")) }) @@ -75,9 +75,7 @@ func TestJWT(t *testing.T) { assert := assert.New(t) jwter := New([]byte("key1")) - token, err := jwter.Sign(map[string]interface{}{}) - assert.NotNil(err) - token, err = jwter.Sign(map[string]interface{}{"test": "OK"}) + token, err := jwter.Sign(map[string]interface{}{"test": "OK"}) assert.Nil(err) claims, _ := jwter.Verify(token) assert.Equal("OK", claims.Get("test")) @@ -172,7 +170,6 @@ func TestJWT(t *testing.T) { jwter.SetAudience("Gear") token, err = jwter.Sign(map[string]interface{}{"test": "OK"}) - fmt.Println(10000, token) assert.Nil(err) claims, _ = jwter.Verify(token) assert.Equal("OK", claims.Get("test"))