add jwt parser

This commit is contained in:
daniel.w 2024-08-27 13:46:58 +08:00
parent 0db1affc10
commit 2a933425d7
3 changed files with 239 additions and 0 deletions

71
jwt/claims_test.go Normal file
View File

@ -0,0 +1,71 @@
package jwt
import (
"github.com/stretchr/testify/require"
"testing"
)
func TestDataClaimsSettersAndGetters(t *testing.T) {
tests := []struct {
name string
setterFunc func(c DataClaims, value string)
getterFunc func(c DataClaims) string
value string
expectedVal string
}{
{
name: "Set and Get ID",
setterFunc: func(c DataClaims, value string) { c.SetID(value) },
getterFunc: func(c DataClaims) string { return c.ID() },
value: "12345",
expectedVal: "12345",
},
{
name: "Set and Get Role",
setterFunc: func(c DataClaims, value string) { c.SetRole(value) },
getterFunc: func(c DataClaims) string { return c.Role() },
value: "admin",
expectedVal: "admin",
},
{
name: "Set and Get Device ID",
setterFunc: func(c DataClaims, value string) { c.SetDeviceID(value) },
getterFunc: func(c DataClaims) string { return c.DeviceID() },
value: "device123",
expectedVal: "device123",
},
{
name: "Set and Get Scope",
setterFunc: func(c DataClaims, value string) { c.SetScope(value) },
getterFunc: func(c DataClaims) string { return c.Get(scopeCode) },
value: "read",
expectedVal: "read",
},
{
name: "Set and Get UID",
setterFunc: func(c DataClaims, value string) { c.SetUID(value) },
getterFunc: func(c DataClaims) string { return c.UID() },
value: "user123",
expectedVal: "user123",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
claims := DataClaims{}
// Call the setter function
tt.setterFunc(claims, tt.value)
// Call the getter function and verify the result
require.Equal(t, tt.expectedVal, tt.getterFunc(claims), "Expected value does not match")
})
}
}
func TestDataClaimsGetNonExistentKey(t *testing.T) {
claims := DataClaims{}
// 對於不存在的鍵,應返回空字串
require.Equal(t, "", claims.Get("nonexistent_key"), "Should return empty string for non-existent key")
}

81
jwt/define_test.go Normal file
View File

@ -0,0 +1,81 @@
package jwt
import (
"github.com/stretchr/testify/require"
"testing"
"time"
)
func TestAccessTokenExpires(t *testing.T) {
token := &Token{
ExpiresIn: 3600, // 1小時
}
expectedDuration := time.Hour
actualDuration := token.AccessTokenExpires()
require.Equal(t, expectedDuration, actualDuration, "Access token expiration duration should be 1 hour")
}
func TestRefreshTokenExpires(t *testing.T) {
token := &Token{
RefreshExpiresIn: 7200, // 2小時
}
expectedDuration := 2 * time.Hour
actualDuration := token.RefreshTokenExpires()
require.Equal(t, expectedDuration, actualDuration, "Refresh token expiration duration should be 2 hours")
}
func TestRefreshTokenExpiresUnix(t *testing.T) {
token := &Token{
RefreshExpiresIn: 3600, // 1小時
}
expectedUnix := time.Now().Add(1 * time.Hour).Unix()
actualUnix := token.RefreshTokenExpiresUnix()
// 設定允許範圍確保結果在1秒的範圍內
require.InEpsilon(t, expectedUnix, actualUnix, 1, "Refresh token expires Unix time should match the expected time")
}
func TestIsExpires(t *testing.T) {
// 測試過期情況
tokenExpired := &Token{
ExpiresIn: 3600, // 1小時
AccessCreateAt: time.Now().Add(-2 * time.Hour), // 2小時前生成的 token應該過期
}
require.True(t, tokenExpired.IsExpires(), "Token should be expired")
// 測試未過期情況
tokenNotExpired := &Token{
ExpiresIn: 3600, // 1小時
AccessCreateAt: time.Now().Add(-30 * time.Minute), // 30分鐘前生成的 token應該未過期
}
require.False(t, tokenNotExpired.IsExpires(), "Token should not be expired")
}
func TestRedisExpiredSec(t *testing.T) {
token := &Token{
ExpiresIn: int(time.Now().Add(1 * time.Hour).Unix()), // 1小時後過期
}
expectedSec := int64(3600) // 1小時
actualSec := token.RedisExpiredSec()
// 確保時間在合理範圍內
require.InDelta(t, expectedSec, actualSec, 1, "Redis expired seconds should be close to 3600 seconds")
}
func TestRedisRefreshExpiredSec(t *testing.T) {
token := &Token{
RefreshExpiresIn: int(time.Now().Add(2 * time.Hour).Unix()), // 2小時後過期
}
expectedSec := int64(7200) // 2小時
actualSec := token.RedisRefreshExpiredSec()
// 確保時間在合理範圍內
require.InDelta(t, expectedSec, actualSec, 1, "Redis refresh expired seconds should be close to 7200 seconds")
}

87
jwt/token_test.go Normal file
View File

@ -0,0 +1,87 @@
package jwt
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestGenerateAccessToken(t *testing.T) {
// 定義測試參數
token := Token{
ID: "12345",
ExpiresIn: int(time.Now().Add(1 * time.Hour).Unix()),
}
sign := "secret_sign"
data := map[string]string{
"role": "admin",
"uid": "user123",
}
issuer := "test_issuer"
// 調用生成 access token
accessToken, err := GenerateAccessToken(token, data, sign, issuer)
require.NoError(t, err)
require.NotEmpty(t, accessToken)
// 檢查 access token 是否可以解析
claims, err := ParseToken(accessToken, sign, true)
require.NoError(t, err)
// 驗證 Claims 是否正確
require.Equal(t, token.ID, claims["jti"])
require.Equal(t, issuer, claims["iss"])
require.Equal(t, "admin", claims["data"].(map[string]interface{})["role"])
require.Equal(t, "user123", claims["data"].(map[string]interface{})["uid"])
}
func TestParseToken(t *testing.T) {
// 測試生成並解析 token
token := Token{
ID: "67890",
ExpiresIn: int(time.Now().Add(2 * time.Hour).Unix()),
}
sign := "another_secret_sign"
data := map[string]string{
"role": "user",
"uid": "user456",
}
accessToken, err := GenerateAccessToken(token, data, sign, "example_issuer")
require.NoError(t, err)
require.NotEmpty(t, accessToken)
// 測試有驗證的解析
claims, err := ParseToken(accessToken, sign, true)
require.NoError(t, err)
require.Equal(t, "user", claims["data"].(map[string]interface{})["role"])
require.Equal(t, "user456", claims["data"].(map[string]interface{})["uid"])
// 測試不驗證的解析
claimsNoValidation, err := ParseToken(accessToken, sign, false)
require.NoError(t, err)
require.Equal(t, "user", claimsNoValidation["data"].(map[string]interface{})["role"])
}
func TestParseClaims(t *testing.T) {
// 測試生成並解析 claims
token := Token{
ID: "54321",
ExpiresIn: int(time.Now().Add(3 * time.Hour).Unix()),
}
sign := "test_sign"
data := map[string]string{
"role": "moderator",
"uid": "user789",
}
accessToken, err := GenerateAccessToken(token, data, sign, "sample_issuer")
require.NoError(t, err)
// 測試 claims 解析
parsedClaims, err := ParseClaims(accessToken, sign, true)
require.NoError(t, err)
require.Equal(t, "moderator", parsedClaims["role"])
require.Equal(t, "user789", parsedClaims["uid"])
}