Skip to content

Commit 7271b90

Browse files
Ajay Kelkarkelkarajay
Ajay Kelkar
authored andcommitted
feat: changes to the session model and session activation
1 parent 8930224 commit 7271b90

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

internal/testhelpers/identity.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ import (
1515

1616
func CreateSession(t *testing.T, reg driver.Registry) *session.Session {
1717
ctx := context.Background()
18+
header := make(map[string][]string, 0)
1819
i := identity.NewIdentity(config.DefaultIdentityTraitsSchemaID)
1920
require.NoError(t, reg.PrivilegedIdentityPool().CreateIdentity(ctx, i))
20-
sess, err := session.NewActiveSession(ctx, i, reg.Config(), time.Now().UTC(), identity.CredentialsTypePassword, identity.AuthenticatorAssuranceLevel1)
21+
sess, err := session.NewActiveSession(ctx, header, i, reg.Config(), time.Now().UTC(), identity.CredentialsTypePassword, identity.AuthenticatorAssuranceLevel1)
2122
require.NoError(t, err)
2223
require.NoError(t, reg.SessionPersister().UpsertSession(ctx, sess))
2324
return sess

internal/testhelpers/session.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ func NewHTTPClientWithSessionToken(t *testing.T, reg *driver.RegistryDefault, se
138138

139139
func NewHTTPClientWithArbitrarySessionToken(t *testing.T, reg *driver.RegistryDefault) *http.Client {
140140
ctx := context.Background()
141+
header := make(map[string][]string, 0)
141142
s, err := session.NewActiveSession(ctx,
143+
header,
142144
&identity.Identity{ID: x.NewUUID(), State: identity.StateActive},
143145
NewSessionLifespanProvider(time.Hour),
144146
time.Now(),
@@ -152,7 +154,9 @@ func NewHTTPClientWithArbitrarySessionToken(t *testing.T, reg *driver.RegistryDe
152154

153155
func NewHTTPClientWithArbitrarySessionCookie(t *testing.T, reg *driver.RegistryDefault) *http.Client {
154156
ctx := context.Background()
157+
header := make(map[string][]string, 0)
155158
s, err := session.NewActiveSession(ctx,
159+
header,
156160
&identity.Identity{ID: x.NewUUID(), State: identity.StateActive},
157161
NewSessionLifespanProvider(time.Hour),
158162
time.Now(),
@@ -166,7 +170,9 @@ func NewHTTPClientWithArbitrarySessionCookie(t *testing.T, reg *driver.RegistryD
166170

167171
func NewNoRedirectHTTPClientWithArbitrarySessionCookie(t *testing.T, reg *driver.RegistryDefault) *http.Client {
168172
ctx := context.Background()
173+
header := make(map[string][]string, 0)
169174
s, err := session.NewActiveSession(ctx,
175+
header,
170176
&identity.Identity{ID: x.NewUUID(), State: identity.StateActive},
171177
NewSessionLifespanProvider(time.Hour),
172178
time.Now(),
@@ -180,7 +186,10 @@ func NewNoRedirectHTTPClientWithArbitrarySessionCookie(t *testing.T, reg *driver
180186

181187
func NewHTTPClientWithIdentitySessionCookie(t *testing.T, reg *driver.RegistryDefault, id *identity.Identity) *http.Client {
182188
ctx := context.Background()
183-
s, err := session.NewActiveSession(ctx, id,
189+
header := make(map[string][]string, 0)
190+
s, err := session.NewActiveSession(ctx,
191+
header,
192+
id,
184193
NewSessionLifespanProvider(time.Hour),
185194
time.Now(),
186195
identity.CredentialsTypePassword,
@@ -193,7 +202,10 @@ func NewHTTPClientWithIdentitySessionCookie(t *testing.T, reg *driver.RegistryDe
193202

194203
func NewHTTPClientWithIdentitySessionToken(t *testing.T, reg *driver.RegistryDefault, id *identity.Identity) *http.Client {
195204
ctx := context.Background()
196-
s, err := session.NewActiveSession(ctx, id,
205+
header := make(map[string][]string, 0)
206+
s, err := session.NewActiveSession(ctx,
207+
header,
208+
id,
197209
NewSessionLifespanProvider(time.Hour),
198210
time.Now(),
199211
identity.CredentialsTypePassword,

session/session.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"database/sql/driver"
66
"encoding/json"
77
"fmt"
8+
"net/http"
9+
"strings"
810
"time"
911

1012
"github.com/pkg/errors"
@@ -79,6 +81,15 @@ type Session struct {
7981
// required: true
8082
Identity *identity.Identity `json:"identity" faker:"identity" db:"-" belongs_to:"identities" fk_id:"IdentityID"`
8183

84+
// IP address of the machine where the session was initiated
85+
ClientIPAddress string `json:"client_ip_address" db:"client_ip_address"`
86+
87+
// User Agent
88+
UserAgent string `json:"user_agent" db:"user_agent"`
89+
90+
// Geo Location where the session was initiated
91+
GeoLocation string `json:"geo_location" db:"geo_location"`
92+
8293
// IdentityID is a helper struct field for gobuffalo.pop.
8394
IdentityID uuid.UUID `json:"-" faker:"-" db:"identity_id"`
8495

@@ -167,7 +178,7 @@ func NewInactiveSession() *Session {
167178
}
168179
}
169180

170-
func (s *Session) Activate(ctx context.Context, requestHeaders map[string][]string, i *identity.Identity, c lifespanProvider, authenticatedAt time.Time) error {
181+
func (s *Session) Activate(ctx context.Context, requestHeaders http.Header, i *identity.Identity, c lifespanProvider, authenticatedAt time.Time) error {
171182
if i != nil && !i.IsActive() {
172183
return ErrIdentityDisabled.WithDetail("identity_id", i.ID)
173184
}
@@ -179,6 +190,22 @@ func (s *Session) Activate(ctx context.Context, requestHeaders map[string][]stri
179190
s.Identity = i
180191
s.IdentityID = i.ID
181192

193+
agent := requestHeaders["User-Agent"]
194+
if len(agent) > 0 {
195+
s.UserAgent = strings.Join(agent, " ")
196+
}
197+
198+
clientIP := requestHeaders.Get("True-Client-IP")
199+
if clientIP != "" {
200+
s.ClientIPAddress = clientIP
201+
} else {
202+
// TODO: Use x lib implementation to parse client IP address from the header string
203+
s.ClientIPAddress = requestHeaders.Get("X-Forwarded-For")
204+
}
205+
206+
clientGeoLocation := []string{requestHeaders.Get("Cf-Ipcity"), requestHeaders.Get("Cf-Ipcountry")}
207+
s.GeoLocation = strings.Join(clientGeoLocation, ", ")
208+
182209
s.SetAuthenticatorAssuranceLevel()
183210
return nil
184211
}

0 commit comments

Comments
 (0)