Skip to content

Commit 8f6cfe1

Browse files
Ajay Kelkarkelkarajay
Ajay Kelkar
authored andcommitted
fix: rename entity to represent Devices
1 parent 1228f04 commit 8f6cfe1

19 files changed

+69
-63
lines changed

persistence/sql/migratest/fixtures/session/7458af86-c1d8-401c-978a-8da89133f78b.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@
4040
"created_at": "2013-10-07T08:23:19Z",
4141
"updated_at": "2013-10-07T08:23:19Z"
4242
},
43-
"metadata": []
43+
"devices": []
4444
}

persistence/sql/migratest/fixtures/session/7458af86-c1d8-401c-978a-8da89133f98b.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"created_at": "2013-10-07T08:23:19Z",
4141
"updated_at": "2013-10-07T08:23:19Z"
4242
},
43-
"metadata": [
43+
"devices": [
4444
{
4545
"id": "884f556e-eb3a-4b9f-bee3-11763642c6c0",
4646
"ip_address": "54.155.246.232",

persistence/sql/migratest/fixtures/session/8571e374-38f2-4f46-8ad3-b9d914e174d3.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@
3535
"created_at": "2013-10-07T08:23:19Z",
3636
"updated_at": "2013-10-07T08:23:19Z"
3737
},
38-
"metadata": []
38+
"devices": []
3939
}

persistence/sql/migratest/fixtures/session/dcde5aaa-f789-4d3d-ae1f-76da8d57e67c.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@
3535
"created_at": "2013-10-07T08:23:19Z",
3636
"updated_at": "2013-10-07T08:23:19Z"
3737
},
38-
"metadata": []
38+
"devices": []
3939
}

persistence/sql/migratest/fixtures/session/f38cdebe-e567-42c9-a562-1bd4dee40998.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@
3535
"created_at": "2013-10-07T08:23:19Z",
3636
"updated_at": "2013-10-07T08:23:19Z"
3737
},
38-
"metadata": []
38+
"devices": []
3939
}

persistence/sql/migratest/testdata/20220907132836_testdata.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ VALUES ('7458af86-c1d8-401c-978a-8da89133f98b', '884f556e-eb3a-4b9f-bee3-1134564
55
'eVwBt7UAAAAVwBt7XAMw', '5ff66179-c240-4703-b0d8-494592cefff5', true, '123eVwBt7UAAAeVwBt7XAMw', 'aal2',
66
'[{"method":"password"},{"method":"totp"}]');
77

8-
INSERT INTO session_metadata (id, nid, session_id, ip_address, user_agent, location, created_at, last_seen)
8+
INSERT INTO session_devices (id, nid, session_id, ip_address, user_agent, location, created_at, last_seen)
99
VALUES ('884f556e-eb3a-4b9f-bee3-11763642c6c0', '884f556e-eb3a-4b9f-bee3-11345642c6c0',
1010
'7458af86-c1d8-401c-978a-8da89133f98b', '54.155.246.232',
1111
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36',
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
DROP TABLE session_metadata;
1+
DROP TABLE session_devices;

persistence/sql/migrations/sql/20220907132836000000_sessions_add_client_fields.cockroach.up.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CREATE TABLE "session_metadata"
1+
CREATE TABLE "session_devices"
22
(
33
"id" UUID NOT NULL,
44
PRIMARY KEY ("id"),
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
DROP TABLE session_metadata;
1+
DROP TABLE session_devices;

persistence/sql/migrations/sql/20220907132836000000_sessions_add_client_fields.mysql.up.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CREATE TABLE `session_metadata`
1+
CREATE TABLE `session_devices`
22
(
33
`id` char(36) NOT NULL,
44
PRIMARY KEY (`id`),
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
DROP TABLE "session_metadata";
1+
DROP TABLE "session_devices";

persistence/sql/migrations/sql/20220907132836000000_sessions_add_client_fields.postgres.up.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CREATE TABLE "session_metadata"
1+
CREATE TABLE "session_devices"
22
(
33
"id" UUID NOT NULL,
44
PRIMARY KEY ("id"),
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
DROP TABLE "session_metadata";
1+
DROP TABLE "session_devices";

persistence/sql/migrations/sql/20220907132836000000_sessions_add_client_fields.sqlite3.up.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CREATE TABLE IF NOT EXISTS "session_metadata"
1+
CREATE TABLE IF NOT EXISTS "session_devices"
22
(
33
"id" TEXT PRIMARY KEY,
44
"ip_address" TEXT DEFAULT '',

persistence/sql/persister_session.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,27 @@ func (p *Persister) GetSession(ctx context.Context, sid uuid.UUID) (*session.Ses
3636
return nil, err
3737
}
3838

39-
metadata, err := p.GetSessionLogs(ctx, sid)
39+
devices, err := p.GetSessionDevices(ctx, sid)
4040
if err != nil {
4141
return nil, err
4242
}
4343

4444
s.Identity = i
45-
s.Metadata = metadata
45+
s.Devices = devices
4646
return &s, nil
4747
}
4848

49-
func (p *Persister) GetSessionLogs(ctx context.Context, sid uuid.UUID) ([]session.Metadata, error) {
50-
ctx, span := p.r.Tracer(ctx).Tracer().Start(ctx, "persistence.sql.GetSessionLogs")
49+
func (p *Persister) GetSessionDevices(ctx context.Context, sid uuid.UUID) ([]session.Device, error) {
50+
ctx, span := p.r.Tracer(ctx).Tracer().Start(ctx, "persistence.sql.GetSessionDevices")
5151
defer span.End()
5252

53-
metadata := make([]session.Metadata, 0)
53+
devices := make([]session.Device, 0)
5454
nid := p.NetworkID(ctx)
55-
if err := p.GetConnection(ctx).Where("session_id = ? AND nid = ?", sid, nid).All(&metadata); err != nil {
55+
if err := p.GetConnection(ctx).Where("session_id = ? AND nid = ?", sid, nid).All(&devices); err != nil {
5656
return nil, sqlcon.HandleError(err)
5757
}
5858

59-
return metadata, nil
59+
return devices, nil
6060
}
6161

6262
// ListSessionsByIdentity retrieves sessions for an identity from the store.
@@ -85,7 +85,13 @@ func (p *Persister) ListSessionsByIdentity(ctx context.Context, iID uuid.UUID, a
8585
return err
8686
}
8787

88+
devices, err := p.GetSessionDevices(ctx, s.ID)
89+
if err != nil {
90+
return err
91+
}
92+
8893
s.Identity = i
94+
s.Devices = devices
8995
}
9096
return nil
9197
}); err != nil {

session/persistence.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ type Persister interface {
2323
// GetSession retrieves a session from the store.
2424
GetSession(ctx context.Context, sid uuid.UUID) (*Session, error)
2525

26-
// GetSessionLogs retrieves all the log history from the store
27-
GetSessionLogs(ctx context.Context, sid uuid.UUID) ([]Metadata, error)
26+
// GetSessionDevices retrieves all the device session history from the store
27+
GetSessionDevices(ctx context.Context, sid uuid.UUID) ([]Device, error)
2828

2929
// ListSessionsByIdentity retrieves sessions for an identity from the store.
3030
ListSessionsByIdentity(ctx context.Context, iID uuid.UUID, active *bool, page, perPage int, except uuid.UUID) ([]*Session, error)

session/session.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ type refreshWindowProvider interface {
3232
SessionRefreshMinTimeLeft(ctx context.Context) time.Duration
3333
}
3434

35-
// Metadata of the Client corresponding to a Session
35+
// Device corresponding to a Session
3636
//
37-
// swagger:model sessionMetadata
38-
type Metadata struct {
39-
// Metadata record ID
37+
// swagger:model sessionDevice
38+
type Device struct {
39+
// Device record ID
4040
//
4141
// required: true
4242
ID uuid.UUID `json:"id" faker:"-" db:"id"`
@@ -62,8 +62,8 @@ type Metadata struct {
6262
NID uuid.UUID `json:"-" faker:"-" db:"nid"`
6363
}
6464

65-
func (m Metadata) TableName(ctx context.Context) string {
66-
return "session_metadata"
65+
func (m Device) TableName(ctx context.Context) string {
66+
return "session_devices"
6767
}
6868

6969
// A Session
@@ -118,8 +118,8 @@ type Session struct {
118118
// required: true
119119
Identity *identity.Identity `json:"identity" faker:"identity" db:"-" belongs_to:"identities" fk_id:"IdentityID"`
120120

121-
// Metadata has history of all clients where the session was used
122-
Metadata []Metadata `json:"metadata" faker:"-" db:"-"`
121+
// Devices has history of all endpoints where the session was used
122+
Devices []Device `json:"devices" faker:"-" db:"-"`
123123

124124
// IdentityID is a helper struct field for gobuffalo.pop.
125125
IdentityID uuid.UUID `json:"-" faker:"-" db:"identity_id"`
@@ -221,33 +221,33 @@ func (s *Session) Activate(r *http.Request, i *identity.Identity, c lifespanProv
221221
s.Identity = i
222222
s.IdentityID = i.ID
223223

224-
s.SaveSessionMeta(r)
224+
s.SaveSessionDeviceInformation(r)
225225
s.SetAuthenticatorAssuranceLevel()
226226
return nil
227227
}
228228

229-
func (s *Session) SaveSessionMeta(r *http.Request) {
230-
var metadata Metadata
229+
func (s *Session) SaveSessionDeviceInformation(r *http.Request) {
230+
var device Device
231231

232-
metadata.ID = x.NewUUID()
233-
metadata.SessionID = s.ID
234-
metadata.CreatedAt = time.Now().UTC()
235-
metadata.LastSeen = time.Now().UTC()
232+
device.ID = x.NewUUID()
233+
device.SessionID = s.ID
234+
device.CreatedAt = time.Now().UTC()
235+
device.LastSeen = time.Now().UTC()
236236

237237
agent := r.Header["User-Agent"]
238238
if len(agent) > 0 {
239-
metadata.UserAgent = stringsx.GetPointer(strings.Join(agent, " "))
239+
device.UserAgent = stringsx.GetPointer(strings.Join(agent, " "))
240240
}
241241

242242
if trueClientIP := r.Header.Get("True-Client-IP"); trueClientIP != "" {
243-
metadata.IPAddress = &trueClientIP
243+
device.IPAddress = &trueClientIP
244244
} else if realClientIP := r.Header.Get("X-Real-IP"); realClientIP != "" {
245-
metadata.IPAddress = &realClientIP
245+
device.IPAddress = &realClientIP
246246
} else if forwardedIP := r.Header["X-Forwarded-For"]; len(forwardedIP) != 0 {
247247
ip, _ := httpx.GetClientIPAddress(forwardedIP, httpx.InternalIPSet)
248-
metadata.IPAddress = &ip
248+
device.IPAddress = &ip
249249
} else {
250-
metadata.IPAddress = &r.RemoteAddr
250+
device.IPAddress = &r.RemoteAddr
251251
}
252252

253253
clientGeoLocation := []string{r.Header.Get("Cf-Ipcity"), r.Header.Get("Cf-Ipcountry")}
@@ -259,9 +259,9 @@ func (s *Session) SaveSessionMeta(r *http.Request) {
259259
}
260260
sb.WriteString(i)
261261
}
262-
metadata.Location = stringsx.GetPointer(sb.String())
262+
device.Location = stringsx.GetPointer(sb.String())
263263

264-
s.Metadata = append(s.Metadata, metadata)
264+
s.Devices = append(s.Devices, device)
265265
}
266266

267267
func (s *Session) Declassify() *Session {

session/session_test.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ func TestSession(t *testing.T) {
7979
assert.True(t, s.Active)
8080
assert.Equal(t, identity.NoAuthenticatorAssuranceLevel, s.AuthenticatorAssuranceLevel)
8181
assert.Equal(t, authAt, s.AuthenticatedAt)
82-
assert.Equal(t, 1, len(s.Metadata))
83-
assert.Equal(t, s.ID.String(), s.Metadata[0].SessionID.String())
84-
assert.Equal(t, "54.155.246.232", *s.Metadata[0].IPAddress)
85-
assert.Equal(t, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36", *s.Metadata[0].UserAgent)
86-
assert.Equal(t, "", *s.Metadata[0].Location)
82+
assert.Equal(t, 1, len(s.Devices))
83+
assert.Equal(t, s.ID.String(), s.Devices[0].SessionID.String())
84+
assert.Equal(t, "54.155.246.232", *s.Devices[0].IPAddress)
85+
assert.Equal(t, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36", *s.Devices[0].UserAgent)
86+
assert.Equal(t, "", *s.Devices[0].Location)
8787
})
8888

8989
t.Run("case=client information reverse proxy real IP set", func(t *testing.T) {
@@ -97,13 +97,13 @@ func TestSession(t *testing.T) {
9797
assert.True(t, s.Active)
9898
assert.Equal(t, identity.NoAuthenticatorAssuranceLevel, s.AuthenticatorAssuranceLevel)
9999
assert.Equal(t, authAt, s.AuthenticatedAt)
100-
assert.Equal(t, 1, len(s.Metadata))
101-
assert.Equal(t, s.ID.String(), s.Metadata[0].SessionID.String())
102-
assert.NotNil(t, s.Metadata[0].LastSeen)
103-
assert.NotNil(t, s.Metadata[0].CreatedAt)
104-
assert.Equal(t, "54.155.246.155", *s.Metadata[0].IPAddress)
105-
assert.Equal(t, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36", *s.Metadata[0].UserAgent)
106-
assert.Equal(t, "", *s.Metadata[0].Location)
100+
assert.Equal(t, 1, len(s.Devices))
101+
assert.Equal(t, s.ID.String(), s.Devices[0].SessionID.String())
102+
assert.NotNil(t, s.Devices[0].LastSeen)
103+
assert.NotNil(t, s.Devices[0].CreatedAt)
104+
assert.Equal(t, "54.155.246.155", *s.Devices[0].IPAddress)
105+
assert.Equal(t, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36", *s.Devices[0].UserAgent)
106+
assert.Equal(t, "", *s.Devices[0].Location)
107107
})
108108

109109
t.Run("case=client information CF", func(t *testing.T) {
@@ -118,11 +118,11 @@ func TestSession(t *testing.T) {
118118
assert.True(t, s.Active)
119119
assert.Equal(t, identity.NoAuthenticatorAssuranceLevel, s.AuthenticatorAssuranceLevel)
120120
assert.Equal(t, authAt, s.AuthenticatedAt)
121-
assert.Equal(t, 1, len(s.Metadata))
122-
assert.Equal(t, s.ID.String(), s.Metadata[0].SessionID.String())
123-
assert.Equal(t, "54.155.246.232", *s.Metadata[0].IPAddress)
124-
assert.Equal(t, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36", *s.Metadata[0].UserAgent)
125-
assert.Equal(t, "Munich, Germany", *s.Metadata[0].Location)
121+
assert.Equal(t, 1, len(s.Devices))
122+
assert.Equal(t, s.ID.String(), s.Devices[0].SessionID.String())
123+
assert.Equal(t, "54.155.246.232", *s.Devices[0].IPAddress)
124+
assert.Equal(t, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36", *s.Devices[0].UserAgent)
125+
assert.Equal(t, "Munich, Germany", *s.Devices[0].Location)
126126
})
127127

128128
for k, tc := range []struct {

x/xsql/sql.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func CleanSQL(t *testing.T, c *pop.Connection) {
2626
new(continuity.Container).TableName(ctx),
2727
new(courier.Message).TableName(ctx),
2828

29-
new(session.Metadata).TableName(ctx),
29+
new(session.Device).TableName(ctx),
3030
new(session.Session).TableName(ctx),
3131
new(login.Flow).TableName(ctx),
3232
new(registration.Flow).TableName(ctx),

0 commit comments

Comments
 (0)