-
-
Notifications
You must be signed in to change notification settings - Fork 936
/
session.go
118 lines (96 loc) · 3.56 KB
/
session.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
package testhelpers
import (
"context"
"net/http"
"testing"
"time"
"github.com/gobuffalo/httptest"
"github.com/stretchr/testify/require"
"github.com/ory/kratos/driver"
"github.com/ory/kratos/identity"
"github.com/ory/kratos/session"
"github.com/ory/kratos/x"
)
type SessionLifespanProvider struct {
e time.Duration
}
func (p *SessionLifespanProvider) SessionLifespan() time.Duration {
return p.e
}
func NewSessionLifespanProvider(expiresIn time.Duration) *SessionLifespanProvider {
return &SessionLifespanProvider{e: expiresIn}
}
func NewSessionClient(t *testing.T, u string) *http.Client {
c := NewClientWithCookies(t)
MockHydrateCookieClient(t, c, u)
return c
}
func maybePersistSession(t *testing.T, reg *driver.RegistryDefault, sess *session.Session) {
id, err := reg.PrivilegedIdentityPool().GetIdentityConfidential(context.Background(), sess.Identity.ID)
if err != nil {
require.NoError(t, reg.PrivilegedIdentityPool().CreateIdentity(context.Background(), sess.Identity))
id, err = reg.PrivilegedIdentityPool().GetIdentityConfidential(context.Background(), sess.Identity.ID)
require.NoError(t, err)
}
sess.Identity = id
sess.IdentityID = id.ID
require.NoError(t, err, reg.SessionPersister().CreateSession(context.Background(), sess))
}
func NewHTTPClientWithSessionCookie(t *testing.T, reg *driver.RegistryDefault, sess *session.Session) *http.Client {
maybePersistSession(t, reg, sess)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.NoError(t, reg.SessionManager().IssueCookie(context.Background(), w, r, sess))
}))
defer ts.Close()
c := NewClientWithCookies(t)
// This should work for other test servers as well because cookies ignore ports.
MockHydrateCookieClient(t, c, ts.URL)
return c
}
func NewTransportWithLogger(parent http.RoundTripper, t *testing.T) *TransportWithLogger {
return &TransportWithLogger{
RoundTripper: parent,
t: t,
}
}
type TransportWithLogger struct {
http.RoundTripper
t *testing.T
}
func (ct *TransportWithLogger) RoundTrip(req *http.Request) (*http.Response, error) {
ct.t.Logf("Made request to: %s", req.URL.String())
if ct.RoundTripper == nil {
return http.DefaultTransport.RoundTrip(req)
}
return ct.RoundTripper.RoundTrip(req)
}
func NewHTTPClientWithSessionToken(t *testing.T, reg *driver.RegistryDefault, sess *session.Session) *http.Client {
maybePersistSession(t, reg, sess)
return &http.Client{
Transport: x.NewTransportWithHeader(http.Header{
"Authorization": {"Bearer " + sess.Token},
}),
}
}
func NewHTTPClientWithArbitrarySessionToken(t *testing.T, reg *driver.RegistryDefault) *http.Client {
return NewHTTPClientWithSessionToken(t, reg, session.NewActiveSession(
&identity.Identity{ID: x.NewUUID()},
NewSessionLifespanProvider(time.Hour),
time.Now(),
))
}
func NewHTTPClientWithArbitrarySessionCookie(t *testing.T, reg *driver.RegistryDefault) *http.Client {
return NewHTTPClientWithSessionCookie(t, reg, session.NewActiveSession(
&identity.Identity{ID: x.NewUUID()},
NewSessionLifespanProvider(time.Hour),
time.Now(),
))
}
func NewHTTPClientWithIdentitySessionCookie(t *testing.T, reg *driver.RegistryDefault, id *identity.Identity) *http.Client {
return NewHTTPClientWithSessionCookie(t, reg,
session.NewActiveSession(id, NewSessionLifespanProvider(time.Hour), time.Now()))
}
func NewHTTPClientWithIdentitySessionToken(t *testing.T, reg *driver.RegistryDefault, id *identity.Identity) *http.Client {
return NewHTTPClientWithSessionToken(t, reg,
session.NewActiveSession(id, NewSessionLifespanProvider(time.Hour), time.Now()))
}