-
-
Notifications
You must be signed in to change notification settings - Fork 321
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
13 changed files
with
349 additions
and
45 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
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,98 @@ | ||
package common | ||
|
||
import ( | ||
"bytes" | ||
"crypto/hmac" | ||
"crypto/sha256" | ||
"encoding/base64" | ||
"errors" | ||
"fmt" | ||
"hash" | ||
"sync" | ||
|
||
"github.com/spf13/viper" | ||
"github.com/sqids/sqids-go" | ||
) | ||
|
||
var ( | ||
hashidsMinLength = 15 | ||
hashids *sqids.Sqids | ||
|
||
jwtSecretBytes = []byte{} | ||
hmacPool = sync.Pool{ | ||
New: func() interface{} { | ||
return hmac.New(sha256.New, jwtSecretBytes) | ||
}, | ||
} | ||
) | ||
|
||
func InitUserToken() error { | ||
tokenSecret := viper.GetString("user_token_secret") | ||
hashidsSalt := viper.GetString("hashids_salt") | ||
|
||
if tokenSecret == "" || hashidsSalt == "" { | ||
return errors.New("token_secret or hashids_salt is not set") | ||
} | ||
|
||
var err error | ||
hashids, err = sqids.New(sqids.Options{ | ||
MinLength: uint8(hashidsMinLength), | ||
Alphabet: hashidsSalt, | ||
}) | ||
|
||
jwtSecretBytes = []byte(tokenSecret) | ||
|
||
return err | ||
} | ||
|
||
func GenerateToken(tokenID, userID int) (string, error) { | ||
payload, err := hashids.Encode([]uint64{uint64(tokenID), uint64(userID)}) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
h := hmacPool.Get().(hash.Hash) | ||
defer func() { | ||
h.Reset() | ||
hmacPool.Put(h) | ||
}() | ||
|
||
h.Write([]byte(payload)) | ||
signature := base64.RawURLEncoding.EncodeToString(h.Sum(nil)) | ||
|
||
return payload + "_" + signature, nil | ||
} | ||
|
||
func ValidateToken(token string) (tokenID, userID int, err error) { | ||
parts := bytes.SplitN([]byte(token), []byte("_"), 2) | ||
if len(parts) != 2 { | ||
return 0, 0, fmt.Errorf("无效的令牌") | ||
} | ||
|
||
payloadEncoded, receivedSignature := parts[0], parts[1] | ||
|
||
h := hmacPool.Get().(hash.Hash) | ||
defer func() { | ||
h.Reset() | ||
hmacPool.Put(h) | ||
}() | ||
|
||
h.Write(payloadEncoded) | ||
expectedSignature := h.Sum(nil) | ||
|
||
decodedSignature, err := base64.RawURLEncoding.DecodeString(string(receivedSignature)) | ||
if err != nil { | ||
return 0, 0, fmt.Errorf("签名解码失败") | ||
} | ||
|
||
if !bytes.Equal(decodedSignature, expectedSignature) { | ||
return 0, 0, fmt.Errorf("签名验证失败") | ||
} | ||
|
||
numbers := hashids.Decode(string(payloadEncoded)) | ||
if len(numbers) != 2 { | ||
return 0, 0, fmt.Errorf("无效的令牌") | ||
} | ||
|
||
return int(numbers[0]), int(numbers[1]), 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
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
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
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.