-
Notifications
You must be signed in to change notification settings - Fork 566
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
12 changed files
with
1,557 additions
and
1,668 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,37 @@ | ||
package generates | ||
|
||
import ( | ||
"bytes" | ||
"encoding/base64" | ||
"strconv" | ||
"strings" | ||
|
||
"gopkg.in/oauth2.v3" | ||
"gopkg.in/oauth2.v3/utils/uuid" | ||
) | ||
|
||
// NewAccessGenerate create to generate the access token instance | ||
func NewAccessGenerate() *AccessGenerate { | ||
return &AccessGenerate{} | ||
} | ||
|
||
// AccessGenerate generate the access token | ||
type AccessGenerate struct { | ||
} | ||
|
||
// Token based on the UUID generated token | ||
func (ag *AccessGenerate) Token(data *oauth2.GenerateBasic, isGenRefresh bool) (access, refresh string, err error) { | ||
buf := bytes.NewBufferString(data.Client.GetID()) | ||
buf.WriteString(data.UserID) | ||
buf.WriteString(strconv.FormatInt(data.CreateAt.UnixNano(), 10)) | ||
|
||
access = base64.URLEncoding.EncodeToString(uuid.NewMD5(uuid.Must(uuid.NewRandom()), buf.Bytes()).Bytes()) | ||
access = strings.ToUpper(strings.TrimRight(access, "=")) | ||
if isGenRefresh { | ||
refresh = base64.URLEncoding.EncodeToString(uuid.NewSHA1(uuid.Must(uuid.NewRandom()), buf.Bytes()).Bytes()) | ||
refresh = strings.ToUpper(strings.TrimRight(refresh, "=")) | ||
} | ||
|
||
return | ||
} | ||
package generates | ||
|
||
import ( | ||
"bytes" | ||
"encoding/base64" | ||
"strconv" | ||
"strings" | ||
|
||
"gopkg.in/oauth2.v3" | ||
"gopkg.in/oauth2.v3/utils/uuid" | ||
) | ||
|
||
// NewAccessGenerate create to generate the access token instance | ||
func NewAccessGenerate() *AccessGenerate { | ||
return &AccessGenerate{} | ||
} | ||
|
||
// AccessGenerate generate the access token | ||
type AccessGenerate struct { | ||
} | ||
|
||
// Token based on the UUID generated token | ||
func (ag *AccessGenerate) Token(data *oauth2.GenerateBasic, isGenRefresh bool) (string, string, error) { | ||
buf := bytes.NewBufferString(data.Client.GetID()) | ||
buf.WriteString(data.UserID) | ||
buf.WriteString(strconv.FormatInt(data.CreateAt.UnixNano(), 10)) | ||
|
||
access := base64.URLEncoding.EncodeToString(uuid.NewMD5(uuid.Must(uuid.NewRandom()), buf.Bytes()).Bytes()) | ||
access = strings.ToUpper(strings.TrimRight(access, "=")) | ||
refresh := "" | ||
if isGenRefresh { | ||
refresh = base64.URLEncoding.EncodeToString(uuid.NewSHA1(uuid.Must(uuid.NewRandom()), buf.Bytes()).Bytes()) | ||
refresh = strings.ToUpper(strings.TrimRight(refresh, "=")) | ||
} | ||
|
||
return access, refresh, nil | ||
} |
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 |
---|---|---|
@@ -1,29 +1,29 @@ | ||
package generates | ||
|
||
import ( | ||
"bytes" | ||
"encoding/base64" | ||
"strings" | ||
|
||
"gopkg.in/oauth2.v3" | ||
"gopkg.in/oauth2.v3/utils/uuid" | ||
) | ||
|
||
// NewAuthorizeGenerate create to generate the authorize code instance | ||
func NewAuthorizeGenerate() *AuthorizeGenerate { | ||
return &AuthorizeGenerate{} | ||
} | ||
|
||
// AuthorizeGenerate generate the authorize code | ||
type AuthorizeGenerate struct{} | ||
|
||
// Token based on the UUID generated token | ||
func (ag *AuthorizeGenerate) Token(data *oauth2.GenerateBasic) (code string, err error) { | ||
buf := bytes.NewBufferString(data.Client.GetID()) | ||
buf.WriteString(data.UserID) | ||
token := uuid.NewMD5(uuid.Must(uuid.NewRandom()), buf.Bytes()) | ||
code = base64.URLEncoding.EncodeToString(token.Bytes()) | ||
code = strings.ToUpper(strings.TrimRight(code, "=")) | ||
|
||
return | ||
} | ||
package generates | ||
|
||
import ( | ||
"bytes" | ||
"encoding/base64" | ||
"strings" | ||
|
||
"gopkg.in/oauth2.v3" | ||
"gopkg.in/oauth2.v3/utils/uuid" | ||
) | ||
|
||
// NewAuthorizeGenerate create to generate the authorize code instance | ||
func NewAuthorizeGenerate() *AuthorizeGenerate { | ||
return &AuthorizeGenerate{} | ||
} | ||
|
||
// AuthorizeGenerate generate the authorize code | ||
type AuthorizeGenerate struct{} | ||
|
||
// Token based on the UUID generated token | ||
func (ag *AuthorizeGenerate) Token(data *oauth2.GenerateBasic) (string, error) { | ||
buf := bytes.NewBufferString(data.Client.GetID()) | ||
buf.WriteString(data.UserID) | ||
token := uuid.NewMD5(uuid.Must(uuid.NewRandom()), buf.Bytes()) | ||
code := base64.URLEncoding.EncodeToString(token.Bytes()) | ||
code = strings.ToUpper(strings.TrimRight(code, "=")) | ||
|
||
return code, nil | ||
} |
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 |
---|---|---|
@@ -1,95 +1,99 @@ | ||
package generates | ||
|
||
import ( | ||
"encoding/base64" | ||
"strings" | ||
"time" | ||
|
||
errs "errors" | ||
|
||
"github.com/dgrijalva/jwt-go" | ||
"gopkg.in/oauth2.v3" | ||
"gopkg.in/oauth2.v3/errors" | ||
"gopkg.in/oauth2.v3/utils/uuid" | ||
) | ||
|
||
// JWTAccessClaims jwt claims | ||
type JWTAccessClaims struct { | ||
jwt.StandardClaims | ||
} | ||
|
||
// Valid claims verification | ||
func (a *JWTAccessClaims) Valid() error { | ||
if time.Unix(a.ExpiresAt, 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{ | ||
StandardClaims: jwt.StandardClaims{ | ||
Audience: data.Client.GetID(), | ||
Subject: data.UserID, | ||
ExpiresAt: data.TokenInfo.GetAccessCreateAt().Add(data.TokenInfo.GetAccessExpiresIn()).Unix(), | ||
}, | ||
} | ||
|
||
token := jwt.NewWithClaims(a.SignedMethod, claims) | ||
var key interface{} | ||
if a.isEs() { | ||
key, err = jwt.ParseECPrivateKeyFromPEM(a.SignedKey) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
} else if a.isRsOrPS() { | ||
key, err = jwt.ParseRSAPrivateKeyFromPEM(a.SignedKey) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
} else if a.isHs() { | ||
key = a.SignedKey | ||
} else { | ||
return "", "", errs.New("unsupported sign method") | ||
} | ||
access, err = token.SignedString(key) | ||
if err != nil { | ||
return | ||
} | ||
|
||
if isGenRefresh { | ||
refresh = base64.URLEncoding.EncodeToString(uuid.NewSHA1(uuid.Must(uuid.NewRandom()), []byte(access)).Bytes()) | ||
refresh = strings.ToUpper(strings.TrimRight(refresh, "=")) | ||
} | ||
|
||
return | ||
} | ||
|
||
func (a *JWTAccessGenerate) isEs() bool { | ||
return strings.HasPrefix(a.SignedMethod.Alg(), "ES") | ||
} | ||
|
||
func (a *JWTAccessGenerate) isRsOrPS() bool { | ||
isRs := strings.HasPrefix(a.SignedMethod.Alg(), "RS") | ||
isPs := strings.HasPrefix(a.SignedMethod.Alg(), "PS") | ||
return isRs || isPs | ||
} | ||
|
||
func (a *JWTAccessGenerate) isHs() bool { | ||
return strings.HasPrefix(a.SignedMethod.Alg(), "HS") | ||
} | ||
package generates | ||
|
||
import ( | ||
"encoding/base64" | ||
"strings" | ||
"time" | ||
|
||
errs "errors" | ||
|
||
"github.com/dgrijalva/jwt-go" | ||
"gopkg.in/oauth2.v3" | ||
"gopkg.in/oauth2.v3/errors" | ||
"gopkg.in/oauth2.v3/utils/uuid" | ||
) | ||
|
||
// JWTAccessClaims jwt claims | ||
type JWTAccessClaims struct { | ||
jwt.StandardClaims | ||
} | ||
|
||
// Valid claims verification | ||
func (a *JWTAccessClaims) Valid() error { | ||
if time.Unix(a.ExpiresAt, 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) (string, string, error) { | ||
claims := &JWTAccessClaims{ | ||
StandardClaims: jwt.StandardClaims{ | ||
Audience: data.Client.GetID(), | ||
Subject: data.UserID, | ||
ExpiresAt: data.TokenInfo.GetAccessCreateAt().Add(data.TokenInfo.GetAccessExpiresIn()).Unix(), | ||
}, | ||
} | ||
|
||
token := jwt.NewWithClaims(a.SignedMethod, claims) | ||
var key interface{} | ||
if a.isEs() { | ||
v, err := jwt.ParseECPrivateKeyFromPEM(a.SignedKey) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
key = v | ||
} else if a.isRsOrPS() { | ||
v, err := jwt.ParseRSAPrivateKeyFromPEM(a.SignedKey) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
key = v | ||
} else if a.isHs() { | ||
key = a.SignedKey | ||
} else { | ||
return "", "", errs.New("unsupported sign method") | ||
} | ||
|
||
access, err := token.SignedString(key) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
refresh := "" | ||
|
||
if isGenRefresh { | ||
refresh = base64.URLEncoding.EncodeToString(uuid.NewSHA1(uuid.Must(uuid.NewRandom()), []byte(access)).Bytes()) | ||
refresh = strings.ToUpper(strings.TrimRight(refresh, "=")) | ||
} | ||
|
||
return access, refresh, nil | ||
} | ||
|
||
func (a *JWTAccessGenerate) isEs() bool { | ||
return strings.HasPrefix(a.SignedMethod.Alg(), "ES") | ||
} | ||
|
||
func (a *JWTAccessGenerate) isRsOrPS() bool { | ||
isRs := strings.HasPrefix(a.SignedMethod.Alg(), "RS") | ||
isPs := strings.HasPrefix(a.SignedMethod.Alg(), "PS") | ||
return isRs || isPs | ||
} | ||
|
||
func (a *JWTAccessGenerate) isHs() bool { | ||
return strings.HasPrefix(a.SignedMethod.Alg(), "HS") | ||
} |
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
Oops, something went wrong.