forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth_flow_test.go
More file actions
109 lines (92 loc) · 3.46 KB
/
Copy pathauth_flow_test.go
File metadata and controls
109 lines (92 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package model
import (
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gorm.io/gorm"
)
func TestAuthFlowIsBoundAndConsumedOnce(t *testing.T) {
truncateTables(t)
token, created, err := CreateAuthFlow(AuthFlowCreate{
Purpose: AuthFlowPurposeOAuth,
Provider: "github",
Intent: AuthFlowIntentBind,
UserId: 42,
SessionId: "session-a",
Payload: `{"affiliate_code":"invite"}`,
ExpiresAt: time.Now().Add(time.Minute),
})
require.NoError(t, err)
require.NotEmpty(t, token)
assert.NotEqual(t, token, created.TokenHash)
_, err = ConsumeAuthFlow(token, AuthFlowMatch{
Purpose: AuthFlowPurposeOAuth,
Provider: "github",
Intent: AuthFlowIntentBind,
UserId: 99,
SessionId: "session-a",
})
assert.ErrorIs(t, err, ErrAuthFlowInvalid)
peeked, err := GetAuthFlow(token, AuthFlowMatch{Purpose: AuthFlowPurposeOAuth, Provider: "github"})
require.NoError(t, err)
assert.Nil(t, peeked.ConsumedAt)
consumed, err := ConsumeAuthFlow(token, AuthFlowMatch{
Purpose: AuthFlowPurposeOAuth,
Provider: "github",
Intent: AuthFlowIntentBind,
UserId: 42,
SessionId: "session-a",
})
require.NoError(t, err)
require.NotNil(t, consumed.ConsumedAt)
_, err = ConsumeAuthFlow(token, AuthFlowMatch{Purpose: AuthFlowPurposeOAuth})
assert.ErrorIs(t, err, ErrAuthFlowConsumed)
}
func TestAuthFlowExpiryIsEnforced(t *testing.T) {
truncateTables(t)
token, flow, err := CreateAuthFlow(AuthFlowCreate{
Purpose: AuthFlowPurposeTwoFALogin,
UserId: 7,
ExpiresAt: time.Now().Add(time.Minute),
})
require.NoError(t, err)
require.NoError(t, DB.Model(&AuthFlow{}).Where("id = ?", flow.Id).Update("expires_at", time.Now().Add(-time.Second)).Error)
_, err = GetAuthFlow(token, AuthFlowMatch{Purpose: AuthFlowPurposeTwoFALogin})
assert.True(t, errors.Is(err, ErrAuthFlowExpired))
_, err = ConsumeAuthFlow(token, AuthFlowMatch{Purpose: AuthFlowPurposeTwoFALogin})
assert.True(t, errors.Is(err, ErrAuthFlowExpired))
}
func TestExternalAuthAssertionCanOnlyBeClaimedOnce(t *testing.T) {
truncateTables(t)
expiresAt := time.Now().Add(time.Minute)
require.NoError(t, ClaimExternalAuthAssertion(AuthFlowPurposeTelegramAssertion, "signed-assertion", expiresAt))
err := ClaimExternalAuthAssertion(AuthFlowPurposeTelegramAssertion, "signed-assertion", expiresAt)
assert.ErrorIs(t, err, ErrAuthFlowConsumed)
require.NoError(t, ClaimExternalAuthAssertion(AuthFlowPurposeTelegramAssertion, "different-assertion", expiresAt))
}
func TestConsumeAuthFlowWithActionRollsBackTogether(t *testing.T) {
truncateTables(t)
token, _, err := CreateAuthFlow(AuthFlowCreate{
Purpose: AuthFlowPurposeTelegramBind,
UserId: 42,
SessionId: "session-a",
ExpiresAt: time.Now().Add(time.Minute),
})
require.NoError(t, err)
actionErr := errors.New("binding failed")
_, err = ConsumeAuthFlowWithAction(token, AuthFlowMatch{
Purpose: AuthFlowPurposeTelegramBind, UserId: 42, SessionId: "session-a",
}, func(tx *gorm.DB, _ *AuthFlow) error {
if err := ClaimExternalAuthAssertionWithTx(tx, AuthFlowPurposeTelegramAssertion, "assertion-a", time.Now().Add(time.Minute)); err != nil {
return err
}
return actionErr
})
assert.ErrorIs(t, err, actionErr)
flow, err := GetAuthFlow(token, AuthFlowMatch{Purpose: AuthFlowPurposeTelegramBind})
require.NoError(t, err)
assert.Nil(t, flow.ConsumedAt)
require.NoError(t, ClaimExternalAuthAssertion(AuthFlowPurposeTelegramAssertion, "assertion-a", time.Now().Add(time.Minute)))
}