feat: add EIP712 verification, auth service implemention and jwt gen
This commit is contained in:
@@ -14,26 +14,23 @@ type SessionRepo interface {
|
||||
GetUserSessions(ctx context.Context, userID uuid.UUID) ([]UserSession, error)
|
||||
}
|
||||
|
||||
// TODO: add IP and UserAgent
|
||||
type UserSession struct {
|
||||
ID uuid.UUID
|
||||
UserID uuid.UUID
|
||||
User User
|
||||
WalletID uuid.UUID
|
||||
IPaddress string
|
||||
Agent string
|
||||
WalletID string
|
||||
ExpiresAt time.Time
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
DeletedAt *time.Time
|
||||
}
|
||||
|
||||
func NewSession(userID, walletID uuid.UUID, ipAddress, agent string, expiresAt time.Time) *UserSession {
|
||||
func NewSession(userID uuid.UUID, walletID string, expiresAt time.Time) *UserSession {
|
||||
return &UserSession{
|
||||
ID: uuid.New(),
|
||||
UserID: userID,
|
||||
WalletID: walletID,
|
||||
IPaddress: ipAddress,
|
||||
Agent: agent,
|
||||
ExpiresAt: expiresAt,
|
||||
CreatedAt: time.Now().UTC(),
|
||||
UpdatedAt: time.Now().UTC(),
|
||||
|
||||
+57
-14
@@ -1,30 +1,33 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"backend/pkg/validate/national"
|
||||
"backend/pkg/validate/phone"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"net/mail"
|
||||
|
||||
validate "backend/pkg/validate/common"
|
||||
|
||||
"github.com/ethereum/go-ethereum/accounts"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrPhoneInvalid = errors.New("phone number is invalid")
|
||||
ErrUserNotFound = errors.New("user not found")
|
||||
ErrNationalIDInvalid = errors.New("national ID is invalid")
|
||||
ErrWalletInvalid = errors.New("wallet address is invalid")
|
||||
ErrEmailInvalid = errors.New("email is invalid")
|
||||
ErrUserNotFound = errors.New("user not found")
|
||||
ErrEmailInvalid = errors.New("email is invalid")
|
||||
)
|
||||
|
||||
type UserRepo interface {
|
||||
Create(ctx context.Context, user *User) error
|
||||
GetByID(ctx context.Context, id uuid.UUID) (*User, error)
|
||||
GetByPhone(ctx context.Context, phone string) (*User, error)
|
||||
GetByPubKey(ctx context.Context, pubKey string) (*User, error)
|
||||
Update(ctx context.Context, user *User) error
|
||||
Delete(ctx context.Context, id uuid.UUID) error
|
||||
}
|
||||
@@ -53,25 +56,65 @@ type Challenge struct {
|
||||
ExpiresAt time.Time
|
||||
}
|
||||
|
||||
// TODO: check EIP712 in here for challenge validation
|
||||
func (c *Challenge) IsExpired() bool {
|
||||
return time.Now().After(c.ExpiresAt)
|
||||
}
|
||||
|
||||
func (c *Challenge) Verify(address string, signedMsg string) (bool, error) {
|
||||
if c.IsExpired() {
|
||||
return false, errors.New("challenge has expired")
|
||||
}
|
||||
|
||||
signature, err := hexutil.Decode(signedMsg)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("decode signature: %w", err)
|
||||
}
|
||||
|
||||
challengeBytes := []byte(c.Message.String())
|
||||
|
||||
return c.VerifySignedBytes(address, challengeBytes, signature)
|
||||
}
|
||||
|
||||
func (c *Challenge) VerifySignedBytes(expectedAddress string, msg []byte, sig []byte) (bool, error) {
|
||||
msgHash := accounts.TextHash(msg)
|
||||
|
||||
if len(sig) != 65 {
|
||||
return false, fmt.Errorf("invalid signature length: expected 65 bytes, got %d", len(sig))
|
||||
}
|
||||
|
||||
if sig[crypto.RecoveryIDOffset] == 27 || sig[crypto.RecoveryIDOffset] == 28 {
|
||||
sig[crypto.RecoveryIDOffset] -= 27
|
||||
}
|
||||
|
||||
recovered, err := crypto.SigToPub(msgHash, sig)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("failed to recover public key: %w", err)
|
||||
}
|
||||
recoveredAddr := crypto.PubkeyToAddress(*recovered)
|
||||
|
||||
if !common.IsHexAddress(expectedAddress) {
|
||||
return false, fmt.Errorf("invalid Ethereum address format: %s", expectedAddress)
|
||||
}
|
||||
expectedAddr := common.HexToAddress(expectedAddress)
|
||||
|
||||
return strings.EqualFold(expectedAddr.Hex(), recoveredAddr.Hex()), nil
|
||||
}
|
||||
|
||||
func NewUser(pubKey, name, lastName, phoneNumber, email, nationalID string) (*User, error) {
|
||||
|
||||
_, err := phone.IsValid(phoneNumber)
|
||||
_, err := validate.IsValidPhone(phoneNumber)
|
||||
if err != nil {
|
||||
return nil, ErrPhoneInvalid
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = national.IsValid(nationalID)
|
||||
_, err = validate.IsValidNationalID(nationalID)
|
||||
if err != nil {
|
||||
return nil, ErrNationalIDInvalid
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !common.IsHexAddress(pubKey) {
|
||||
return nil, ErrWalletInvalid
|
||||
_, err = validate.IsValidPublicKey(pubKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &User{
|
||||
|
||||
Reference in New Issue
Block a user