-
Notifications
You must be signed in to change notification settings - Fork 18
/
auth_test.go
189 lines (149 loc) · 4.78 KB
/
auth_test.go
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package proton_test
import (
"context"
"runtime"
"testing"
"time"
"github.com/ProtonMail/go-proton-api"
"github.com/ProtonMail/go-proton-api/server"
"github.com/bradenaw/juniper/parallel"
"github.com/stretchr/testify/require"
)
func TestAuth(t *testing.T) {
s := server.New()
defer s.Close()
_, _, err := s.CreateUser("user", []byte("pass"))
require.NoError(t, err)
m := proton.New(
proton.WithHostURL(s.GetHostURL()),
proton.WithTransport(proton.InsecureTransport()),
)
defer m.Close()
// Create one session.
c1, auth1, err := m.NewClientWithLogin(context.Background(), "user", []byte("pass"))
require.NoError(t, err)
// Revoke all other sessions.
require.NoError(t, c1.AuthRevokeAll(context.Background()))
// Create another session.
c2, _, err := m.NewClientWithLogin(context.Background(), "user", []byte("pass"))
require.NoError(t, err)
// There should be two sessions.
sessions, err := c1.AuthSessions(context.Background())
require.NoError(t, err)
require.Len(t, sessions, 2)
// Revoke the first session.
require.NoError(t, c2.AuthRevoke(context.Background(), auth1.UID))
// The first session should no longer work.
require.Error(t, c1.AuthDelete(context.Background()))
// There should be one session remaining.
remaining, err := c2.AuthSessions(context.Background())
require.NoError(t, err)
require.Len(t, remaining, 1)
// Delete the last session.
require.NoError(t, c2.AuthDelete(context.Background()))
}
func TestAuth_Refresh(t *testing.T) {
s := server.New()
defer s.Close()
// Create a user on the server.
userID, _, err := s.CreateUser("user", []byte("pass"))
require.NoError(t, err)
// The auth is valid for 4 seconds.
s.SetAuthLife(4 * time.Second)
m := proton.New(
proton.WithHostURL(s.GetHostURL()),
proton.WithTransport(proton.InsecureTransport()),
)
defer m.Close()
// Create one session for the user.
c, auth, err := m.NewClientWithLogin(context.Background(), "user", []byte("pass"))
require.NoError(t, err)
require.Equal(t, userID, auth.UserID)
// Wait for 2 seconds.
time.Sleep(2 * time.Second)
// The client should still be authenticated.
{
user, err := c.GetUser(context.Background())
require.NoError(t, err)
require.Equal(t, "user", user.Name)
require.Equal(t, userID, user.ID)
}
// Wait for 2 more seconds.
time.Sleep(2 * time.Second)
// The client's auth token should have expired, but will be refreshed on the next request.
{
user, err := c.GetUser(context.Background())
require.NoError(t, err)
require.Equal(t, "user", user.Name)
require.Equal(t, userID, user.ID)
}
}
func TestAuth_Refresh_Multi(t *testing.T) {
s := server.New()
defer s.Close()
// Create a user on the server.
userID, _, err := s.CreateUser("user", []byte("pass"))
require.NoError(t, err)
// The auth is valid for 4 seconds.
s.SetAuthLife(4 * time.Second)
m := proton.New(
proton.WithHostURL(s.GetHostURL()),
proton.WithTransport(proton.InsecureTransport()),
)
defer m.Close()
c, auth, err := m.NewClientWithLogin(context.Background(), "user", []byte("pass"))
require.NoError(t, err)
require.Equal(t, userID, auth.UserID)
time.Sleep(2 * time.Second)
// The client should still be authenticated.
parallel.Do(runtime.NumCPU(), 100, func(idx int) {
user, err := c.GetUser(context.Background())
require.NoError(t, err)
require.Equal(t, "user", user.Name)
require.Equal(t, userID, user.ID)
})
// Wait for the auth to expire.
time.Sleep(2 * time.Second)
// Client auth token should have expired, but will be refreshed on the next request.
parallel.Do(runtime.NumCPU(), 100, func(idx int) {
user, err := c.GetUser(context.Background())
require.NoError(t, err)
require.Equal(t, "user", user.Name)
require.Equal(t, userID, user.ID)
})
}
func TestAuth_Refresh_Deauth(t *testing.T) {
s := server.New()
defer s.Close()
// Create a user on the server.
userID, _, err := s.CreateUser("user", []byte("pass"))
require.NoError(t, err)
m := proton.New(
proton.WithHostURL(s.GetHostURL()),
proton.WithTransport(proton.InsecureTransport()),
)
defer m.Close()
// Create one session for the user.
c, auth, err := m.NewClientWithLogin(context.Background(), "user", []byte("pass"))
require.NoError(t, err)
require.Equal(t, userID, auth.UserID)
deauth := false
c.AddDeauthHandler(func() {
deauth = true
})
// The client should still be authenticated.
{
user, err := c.GetUser(context.Background())
require.NoError(t, err)
require.Equal(t, "user", user.Name)
require.Equal(t, userID, user.ID)
}
require.NoError(t, s.RevokeUser(userID))
// The client's auth token should have expired, and should not be refreshed
{
_, err := c.GetUser(context.Background())
require.Error(t, err)
}
// The client shuold call de-auth handlers.
require.Eventually(t, func() bool { return deauth }, time.Second, 300*time.Millisecond)
}