Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

Commit

Permalink
add "iat" automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Jan 1, 2018
1 parent 0513098 commit 4cf65b7
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion 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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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).
2 changes: 1 addition & 1 deletion auth.go
Expand Up @@ -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:
Expand Down
7 changes: 2 additions & 5 deletions example_test.go
@@ -1,8 +1,6 @@
package auth_test

import (
"fmt"
"io/ioutil"
"net/http"

"github.com/SermoDigital/jose/jwt"
Expand Down Expand Up @@ -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))
}
3 changes: 3 additions & 0 deletions jwt/jwt.go
Expand Up @@ -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
Expand Down
7 changes: 2 additions & 5 deletions jwt/jwt_test.go
@@ -1,7 +1,6 @@
package jwt

import (
"fmt"
"testing"
"time"

Expand All @@ -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"))
})

Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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"))
Expand Down

0 comments on commit 4cf65b7

Please sign in to comment.