fix nil pointer

This commit is contained in:
xmdhs 2023-10-23 21:44:57 +08:00
parent 8a5ad05b14
commit 500d2cc223
No known key found for this signature in database
GPG Key ID: E809D6D43DEFCC95
4 changed files with 13 additions and 21 deletions

View File

@ -96,7 +96,7 @@ func (w *WebService) Reg(ctx context.Context, u model.UserReg, ipPrefix, ip stri
if err != nil { if err != nil {
return model.LoginRep{}, fmt.Errorf("Reg: %w", err) return model.LoginRep{}, fmt.Errorf("Reg: %w", err)
} }
jwt, err := utilsService.CreateToken(ctx, du, w.client, w.cache, w.prikey, "web") jwt, err := utilsService.CreateToken(ctx, du, w.client, w.cache, w.prikey, "web", userUuid)
if err != nil { if err != nil {
return model.LoginRep{}, fmt.Errorf("Login: %w", err) return model.LoginRep{}, fmt.Errorf("Login: %w", err)
} }
@ -125,7 +125,7 @@ func (w *WebService) Login(ctx context.Context, l model.Login, ip string) (model
if err != nil { if err != nil {
return model.LoginRep{}, fmt.Errorf("Login: %w", err) return model.LoginRep{}, fmt.Errorf("Login: %w", err)
} }
jwt, err := utilsService.CreateToken(ctx, u, w.client, w.cache, w.prikey, "web") jwt, err := utilsService.CreateToken(ctx, u, w.client, w.cache, w.prikey, "web", u.Edges.Profile.UUID)
if err != nil { if err != nil {
return model.LoginRep{}, fmt.Errorf("Login: %w", err) return model.LoginRep{}, fmt.Errorf("Login: %w", err)
} }

View File

@ -82,7 +82,7 @@ func Auth(ctx context.Context, t yggdrasil.ValidateToken, client *ent.Client, c
return claims, nil return claims, nil
} }
func CreateToken(ctx context.Context, u *ent.User, client *ent.Client, cache cache.Cache, jwtKey *rsa.PrivateKey, clientToken string) (string, error) { func CreateToken(ctx context.Context, u *ent.User, client *ent.Client, cache cache.Cache, jwtKey *rsa.PrivateKey, clientToken string, uuid string) (string, error) {
if IsDisable(u.State) { if IsDisable(u.State) {
return "", fmt.Errorf("CreateToken: %w", ErrUserDisable) return "", fmt.Errorf("CreateToken: %w", ErrUserDisable)
} }
@ -112,7 +112,7 @@ func CreateToken(ctx context.Context, u *ent.User, client *ent.Client, cache cac
if err != nil { if err != nil {
return "", fmt.Errorf("CreateToken: %w", err) return "", fmt.Errorf("CreateToken: %w", err)
} }
t, err := NewJwtToken(jwtKey, strconv.FormatUint(utoken.TokenID, 10), clientToken, u.Edges.Profile.UUID, u.ID) t, err := NewJwtToken(jwtKey, strconv.FormatUint(utoken.TokenID, 10), clientToken, uuid, u.ID)
if err != nil { if err != nil {
return "", fmt.Errorf("CreateToken: %w", err) return "", fmt.Errorf("CreateToken: %w", err)
} }

View File

@ -67,7 +67,7 @@ func (y *Yggdrasil) Authenticate(cxt context.Context, auth yggdrasil.Authenticat
clientToken = strings.ReplaceAll(uuid.New().String(), "-", "") clientToken = strings.ReplaceAll(uuid.New().String(), "-", "")
} }
jwts, err := sutils.CreateToken(cxt, u, y.client, y.cache, y.prikey, clientToken) jwts, err := sutils.CreateToken(cxt, u, y.client, y.cache, y.prikey, clientToken, u.Edges.Profile.UUID)
if err != nil { if err != nil {
return yggdrasil.Token{}, fmt.Errorf("Authenticate: %w", err) return yggdrasil.Token{}, fmt.Errorf("Authenticate: %w", err)
} }

View File

@ -8,7 +8,6 @@ import (
"crypto/x509" "crypto/x509"
"encoding/base64" "encoding/base64"
"encoding/pem" "encoding/pem"
"fmt"
"os" "os"
"testing" "testing"
@ -23,27 +22,20 @@ func TestAuthlibSign(t *testing.T) {
} }
as := NewAuthlibSignWithKey(rsa2048) as := NewAuthlibSignWithKey(rsa2048)
pri, err := as.GetPriKey() pri, err := as.GetPriKey()
if err != nil { require.Nil(t, err)
t.Fatal(err) require.NotEmpty(t, pri)
}
pub, err := as.GetPKIXPubKey() pub, err := as.GetPKIXPubKey()
if err != nil { require.Nil(t, err)
t.Fatal(err) require.NotEmpty(t, pub)
}
sign, err := as.Sign([]byte("xmdhs")) sign, err := as.Sign([]byte("xmdhs"))
if err != nil { require.Nil(t, err)
t.Fatal(err)
}
fmt.Println(pri)
fmt.Println(pub)
fmt.Println(sign)
hashed := sha1.Sum([]byte("xmdhs")) hashed := sha1.Sum([]byte("xmdhs"))
err = rsa.VerifyPKCS1v15(&as.key.PublicKey, crypto.SHA1, hashed[:], lo.Must(base64.StdEncoding.DecodeString(sign))) err = rsa.VerifyPKCS1v15(&as.key.PublicKey, crypto.SHA1, hashed[:], lo.Must(base64.StdEncoding.DecodeString(sign)))
if err != nil { require.Nil(t, err)
t.Fatal(err)
}
} }
func TestLittleskinSign(t *testing.T) { func TestLittleskinSign(t *testing.T) {