|
| 1 | +package model |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/go-playground/assert/v2" |
| 10 | + "github.com/urnetwork/server" |
| 11 | +) |
| 12 | + |
| 13 | +func TestWalletAuthChallengeCreateAndUse(t *testing.T) { |
| 14 | + server.DefaultTestEnv().Run(t, func(t testing.TB) { |
| 15 | + ctx := context.Background() |
| 16 | + |
| 17 | + result := CreateWalletAuthChallenge(WalletAuthChallengeArgs{}, ctx) |
| 18 | + assert.Equal(t, result.Error, nil) |
| 19 | + assert.Equal(t, strings.Contains(result.MessageTemplate, result.Challenge), true) |
| 20 | + |
| 21 | + useResult, err := UseWalletAuthChallenge(&UseWalletAuthChallengeArgs{ |
| 22 | + Blockchain: "solana", |
| 23 | + PublicKey: "6UJtwDRMv2CCfVCKm6hgMDAGrFzv7z8WKEHut2u8dV8s", |
| 24 | + Message: result.MessageTemplate, |
| 25 | + Signature: "KEpagxVwv1FmPt3KIMdVZz4YsDxgD7J23+f6aafejwdnBy3WJgkE4qteYMwucNoH+9RaPU70YV2Bf+xI+Nd7Cw==", |
| 26 | + }, ctx) |
| 27 | + assert.Equal(t, err, nil) |
| 28 | + assert.Equal(t, useResult.Valid, true) |
| 29 | + |
| 30 | + // replay must fail |
| 31 | + useResult2, err := UseWalletAuthChallenge(&UseWalletAuthChallengeArgs{ |
| 32 | + Blockchain: "solana", |
| 33 | + PublicKey: "6UJtwDRMv2CCfVCKm6hgMDAGrFzv7z8WKEHut2u8dV8s", |
| 34 | + Message: result.MessageTemplate, |
| 35 | + Signature: "KEpagxVwv1FmPt3KIMdVZz4YsDxgD7J23+f6aafejwdnBy3WJgkE4qteYMwucNoH+9RaPU70YV2Bf+xI+Nd7Cw==", |
| 36 | + }, ctx) |
| 37 | + assert.Equal(t, err, nil) |
| 38 | + assert.Equal(t, useResult2.Valid, false) |
| 39 | + assert.Equal(t, useResult2.Error != nil, true) |
| 40 | + }) |
| 41 | +} |
| 42 | + |
| 43 | +func TestWalletAuthChallengeExpired(t *testing.T) { |
| 44 | + server.DefaultTestEnv().Run(t, func(t testing.TB) { |
| 45 | + ctx := context.Background() |
| 46 | + |
| 47 | + result := CreateWalletAuthChallenge(WalletAuthChallengeArgs{}, ctx) |
| 48 | + assert.Equal(t, result.Error, nil) |
| 49 | + |
| 50 | + // mutate the message to an old timestamp |
| 51 | + oldMessage := FormatWalletAuthChallengeMessage(result.Challenge, result.Timestamp-int64((6*time.Minute)/time.Second)) |
| 52 | + useResult, err := UseWalletAuthChallenge(&UseWalletAuthChallengeArgs{ |
| 53 | + Blockchain: "solana", |
| 54 | + PublicKey: "6UJtwDRMv2CCfVCKm6hgMDAGrFzv7z8WKEHut2u8dV8s", |
| 55 | + Message: oldMessage, |
| 56 | + Signature: "KEpagxVwv1FmPt3KIMdVZz4YsDxgD7J23+f6aafejwdnBy3WJgkE4qteYMwucNoH+9RaPU70YV2Bf+xI+Nd7Cw==", |
| 57 | + }, ctx) |
| 58 | + assert.Equal(t, err, nil) |
| 59 | + assert.Equal(t, useResult.Valid, false) |
| 60 | + }) |
| 61 | +} |
| 62 | + |
| 63 | +func TestWalletAuthChallengeFutureTimestamp(t *testing.T) { |
| 64 | + server.DefaultTestEnv().Run(t, func(t testing.TB) { |
| 65 | + ctx := context.Background() |
| 66 | + |
| 67 | + result := CreateWalletAuthChallenge(WalletAuthChallengeArgs{}, ctx) |
| 68 | + assert.Equal(t, result.Error, nil) |
| 69 | + |
| 70 | + futureMessage := FormatWalletAuthChallengeMessage(result.Challenge, result.Timestamp+int64((6*time.Minute)/time.Second)) |
| 71 | + useResult, err := UseWalletAuthChallenge(&UseWalletAuthChallengeArgs{ |
| 72 | + Blockchain: "solana", |
| 73 | + PublicKey: "6UJtwDRMv2CCfVCKm6hgMDAGrFzv7z8WKEHut2u8dV8s", |
| 74 | + Message: futureMessage, |
| 75 | + Signature: "KEpagxVwv1FmPt3KIMdVZz4YsDxgD7J23+f6aafejwdnBy3WJgkE4qteYMwucNoH+9RaPU70YV2Bf+xI+Nd7Cw==", |
| 76 | + }, ctx) |
| 77 | + assert.Equal(t, err, nil) |
| 78 | + assert.Equal(t, useResult.Valid, false) |
| 79 | + }) |
| 80 | +} |
0 commit comments