新增 auth 包,提供 CLSAuthService 结构体及 JWT 令牌的本地和远程校验功能;新增单元测试文件 auth_test.go,验证 JwtAuth 和 TokenAuth 方法的正确性。

This commit is contained in:
2025-07-22 21:25:04 +08:00
parent 29071aa83f
commit 622e5e4094
2 changed files with 8 additions and 8 deletions

View File

@ -1,5 +1,5 @@
// Package cls 提供 CloudLiteSync JWT 令牌校验与集成支持。
package token
package auth
import (
"crypto/rsa"
@ -24,9 +24,9 @@ type CLSAuthService struct {
RemoteServer string
}
// NewCLSTokenService 创建一个新的 CLSService 实例。
// NewCLSAuthService 创建一个新的 CLSService 实例。
// publicKeyPEM 为 PEM 格式的 RSA 公钥matchPurpose 为用途标识remoteServer 为远程服务地址。
func NewCLSTokenService(publicKeyPEM, matchPurpose, remoteServer string) *CLSAuthService {
func NewCLSAuthService(publicKeyPEM, matchPurpose, remoteServer string) *CLSAuthService {
block, _ := pem.Decode([]byte(publicKeyPEM))
if block == nil {
panic("failed to parse PEM block")

View File

@ -1,4 +1,4 @@
package token
package auth
import (
"testing"
@ -16,7 +16,7 @@ JQIDAQAB
-----END PUBLIC KEY-----`
func TestTokenAuth_Mock(t *testing.T) {
cls := NewCLSTokenService(testPublicKey, "MiniCatch", "https://cls.mazhangjing.com")
cls := NewCLSAuthService(testPublicKey, "MiniCatch", "https://cls.mazhangjing.com")
claims, err := cls.JwtAuth("eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImNvcmtpbmUiLCJyb2xlIjoiYWRtaW4iLCJwdXJwb3NlIjoiTWluaUNhdGNoIiwiZXhwIjoxNzUzOTczNzYwLCJuYmYiOjE3NTMxNjczNjcsImlhdCI6MTc1MzE2NzM2N30.n_sWBUPkTjBa8Uuu_gFd5rw7mOvo3ccINuymFamserBos9WPxiOT9FEf9sPvT-bA7GAQ9K5RFdvWhAmDXRHryraxtk1nzf-WQeV6REjVdwbqHZxBQT0e8DnvPLpLkAbFtK-KP_CAlX3bEj-l7Mq8MzsREKJ456qJIIeybIxCwpmyJ--s2iRwcRS-5Y5kTrXMZqxkiGsLjjlLwKglgG7sqBcZNvPiNCkhnr3Jlj6H74_xF3jAdbBflrns_D4SCm-3IhaPvqpKEEnq6Un_oD1ZbvSsHxZozQKYIR4lqmmBvFo0BKyWOVL5yL8nrooF8Cj1SKMihisL_DgqFoyU65lCIQ")
if err != nil {
t.Fatalf("JwtAuth failed: %v", err)
@ -25,7 +25,7 @@ func TestTokenAuth_Mock(t *testing.T) {
}
func TestTokenAuthFailed(t *testing.T) {
cls := NewCLSTokenService(testPublicKey, "MiniCatch2", "https://cls.mazhangjing.com")
cls := NewCLSAuthService(testPublicKey, "MiniCatch2", "https://cls.mazhangjing.com")
claims, err := cls.JwtAuth("eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImNvcmtpbmUiLCJyb2xlIjoiYWRtaW4iLCJwdXJwb3NlIjoiTWluaUNhdGNoIiwiZXhwIjoxNzUzOTczNzYwLCJuYmYiOjE3NTMxNjczNjcsImlhdCI6MTc1MzE2NzM2N30.n_sWBUPkTjBa8Uuu_gFd5rw7mOvo3ccINuymFamserBos9WPxiOT9FEf9sPvT-bA7GAQ9K5RFdvWhAmDXRHryraxtk1nzf-WQeV6REjVdwbqHZxBQT0e8DnvPLpLkAbFtK-KP_CAlX3bEj-l7Mq8MzsREKJ456qJIIeybIxCwpmyJ--s2iRwcRS-5Y5kTrXMZqxkiGsLjjlLwKglgG7sqBcZNvPiNCkhnr3Jlj6H74_xF3jAdbBflrns_D4SCm-3IhaPvqpKEEnq6Un_oD1ZbvSsHxZozQKYIR4lqmmBvFo0BKyWOVL5yL8nrooF8Cj1SKMihisL_DgqFoyU65lCIQ")
if err != nil {
t.Logf("JwtAuth failed: %v", err)
@ -35,7 +35,7 @@ func TestTokenAuthFailed(t *testing.T) {
}
func TestTokenTotpAuth1(t *testing.T) {
cls := NewCLSTokenService(testPublicKey, "MiniCatch", "https://cls.mazhangjing.com")
cls := NewCLSAuthService(testPublicKey, "MiniCatch", "https://cls.mazhangjing.com")
claims, err := cls.TokenAuth("638963")
if err != nil {
t.Logf("TokenAuth failed: %v", err)
@ -45,7 +45,7 @@ func TestTokenTotpAuth1(t *testing.T) {
}
func TestTokenTotpAuth2(t *testing.T) {
cls := NewCLSTokenService(testPublicKey, "MiniCatch2", "https://cls.mazhangjing.com")
cls := NewCLSAuthService(testPublicKey, "MiniCatch2", "https://cls.mazhangjing.com")
claims, err := cls.TokenAuth("638963")
if err != nil {
t.Logf("TokenAuth failed: %v", err)