TODO: 反射

This commit is contained in:
eson
2023-06-28 19:32:41 +08:00
parent 0851c922ef
commit d4e75b1efc
27 changed files with 527 additions and 186 deletions

View File

@@ -1,6 +1,8 @@
package auth
import (
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"
@@ -181,6 +183,10 @@ func getJwtClaims(AuthKey string, AccessSecret *string) (jwt.MapClaims, error) {
return nil, errors.New(fmt.Sprint("Invalid token", err))
}
func PasswordHash(pwd string) string {
return base64.URLEncoding.EncodeToString(sha256.New().Sum([]byte(pwd)))
}
func CheckValueRange[T comparable](v T, rangevalues ...T) bool {
for _, rv := range rangevalues {
if v == rv {

View File

@@ -1,6 +1,9 @@
package auth
import (
"crypto/sha256"
"encoding/base64"
"log"
"testing"
"time"
)
@@ -29,6 +32,33 @@ func TestGenJwt(t *testing.T) {
// log.Println(claims)
}
func TestGenBackendJwt(t *testing.T) {
now := time.Now().Unix()
secret := "fusen_backend_2023"
a, err := GenerateBackendJwtToken(&secret, 3600*24*7, now, 1, 1)
if err != nil {
t.Error(err)
}
log.Println(a)
claims, err := getJwtClaims(a, &secret)
if err != nil {
t.Error(err)
}
userinfo, err := GetBackendUserInfoFormMapClaims(claims)
if err != nil {
t.Error(err)
}
if userinfo.UserId != 1 || userinfo.DepartmentId != 1 {
t.Error(userinfo)
}
// log.Println(claims)
}
func TestCase1(t *testing.T) {
log.Println(base64.URLEncoding.EncodeToString(sha256.New().Sum([]byte("fusen_backend_2023"))))
}
func TestCheckValueRange(t *testing.T) {
v := 1
rv1 := []int{1, 3, 4}