-
Notifications
You must be signed in to change notification settings - Fork 575
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
122 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package generates | ||
|
||
import ( | ||
"encoding/base64" | ||
"strings" | ||
"time" | ||
|
||
"github.com/dgrijalva/jwt-go" | ||
uuid "github.com/satori/go.uuid" | ||
"gopkg.in/oauth2.v3" | ||
"gopkg.in/oauth2.v3/errors" | ||
) | ||
|
||
// JWTAccessClaims jwt claims | ||
type JWTAccessClaims struct { | ||
ClientID string `json:"client_id,omitempty"` | ||
UserID string `json:"user_id,omitempty"` | ||
ExpiredAt int64 `json:"expired_at,omitempty"` | ||
} | ||
|
||
// Valid claims verification | ||
func (a *JWTAccessClaims) Valid() error { | ||
if time.Unix(a.ExpiredAt, 0).Before(time.Now()) { | ||
return errors.ErrInvalidAccessToken | ||
} | ||
return nil | ||
} | ||
|
||
// NewJWTAccessGenerate create to generate the jwt access token instance | ||
func NewJWTAccessGenerate(key []byte, method jwt.SigningMethod) *JWTAccessGenerate { | ||
return &JWTAccessGenerate{ | ||
SignedKey: key, | ||
SignedMethod: method, | ||
} | ||
} | ||
|
||
// JWTAccessGenerate generate the jwt access token | ||
type JWTAccessGenerate struct { | ||
SignedKey []byte | ||
SignedMethod jwt.SigningMethod | ||
} | ||
|
||
// Token based on the UUID generated token | ||
func (a *JWTAccessGenerate) Token(data *oauth2.GenerateBasic, isGenRefresh bool) (access, refresh string, err error) { | ||
claims := &JWTAccessClaims{ | ||
ClientID: data.Client.GetID(), | ||
UserID: data.UserID, | ||
ExpiredAt: data.TokenInfo.GetAccessCreateAt().Add(data.TokenInfo.GetAccessExpiresIn()).Unix(), | ||
} | ||
|
||
token := jwt.NewWithClaims(a.SignedMethod, claims) | ||
access, err = token.SignedString(a.SignedKey) | ||
if err != nil { | ||
return | ||
} | ||
|
||
if isGenRefresh { | ||
refresh = base64.URLEncoding.EncodeToString(uuid.NewV5(uuid.Must(uuid.NewV4()), access).Bytes()) | ||
refresh = strings.ToUpper(strings.TrimRight(refresh, "=")) | ||
} | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package generates_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/dgrijalva/jwt-go" | ||
|
||
"gopkg.in/oauth2.v3" | ||
"gopkg.in/oauth2.v3/generates" | ||
"gopkg.in/oauth2.v3/models" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestJWTAccess(t *testing.T) { | ||
Convey("Test JWT Access Generate", t, func() { | ||
data := &oauth2.GenerateBasic{ | ||
Client: &models.Client{ | ||
ID: "123456", | ||
Secret: "123456", | ||
}, | ||
UserID: "000000", | ||
TokenInfo: &models.Token{ | ||
AccessCreateAt: time.Now(), | ||
AccessExpiresIn: time.Second * 120, | ||
}, | ||
} | ||
|
||
gen := generates.NewJWTAccessGenerate([]byte("00000000"), jwt.SigningMethodHS512) | ||
access, refresh, err := gen.Token(data, true) | ||
So(err, ShouldBeNil) | ||
So(access, ShouldNotBeEmpty) | ||
So(refresh, ShouldNotBeEmpty) | ||
|
||
token, err := jwt.ParseWithClaims(access, &generates.JWTAccessClaims{}, func(t *jwt.Token) (interface{}, error) { | ||
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok { | ||
return nil, fmt.Errorf("parse error") | ||
} | ||
return []byte("00000000"), nil | ||
}) | ||
So(err, ShouldBeNil) | ||
|
||
claims, ok := token.Claims.(*generates.JWTAccessClaims) | ||
So(ok, ShouldBeTrue) | ||
So(token.Valid, ShouldBeTrue) | ||
So(claims.ClientID, ShouldEqual, "123456") | ||
So(claims.UserID, ShouldEqual, "000000") | ||
}) | ||
} |