Skip to content

Commit bcbf68e

Browse files
author
Ajay Kelkar
authored
fix: active attribute based off IsActive checks (#2901)
1 parent 2d46209 commit bcbf68e

7 files changed

+62
-5
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "7458af86-c1d8-401c-978a-8da89133f78b",
33
"active": true,
4-
"expires_at": "2013-10-07T08:23:19Z",
4+
"expires_at": "2080-10-07T08:23:19Z",
55
"authenticated_at": "2013-10-07T08:23:19Z",
66
"authenticator_assurance_level": "aal2",
77
"authentication_methods": [

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"id": "7458af86-c1d8-401c-978a-8da89133f98b",
3-
"active": true,
3+
"active": false,
44
"expires_at": "2013-10-07T08:23:19Z",
55
"authenticated_at": "2013-10-07T08:23:19Z",
66
"authenticator_assurance_level": "aal2",

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"id": "dcde5aaa-f789-4d3d-ae1f-76da8d57e67c",
3-
"active": true,
3+
"active": false,
44
"expires_at": "2013-10-07T08:23:19Z",
55
"authenticated_at": "2013-10-07T08:23:19Z",
66
"authenticator_assurance_level": "aal1",

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"id": "f38cdebe-e567-42c9-a562-1bd4dee40998",
3-
"active": true,
3+
"active": false,
44
"expires_at": "2013-10-07T08:23:19Z",
55
"authenticated_at": "2013-10-07T08:23:19Z",
66
"authenticator_assurance_level": "aal1",

persistence/sql/migratest/testdata/20210810153530_testdata.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
INSERT INTO sessions (id, nid, issued_at, expires_at, authenticated_at, created_at, updated_at, token, identity_id,
22
active, logout_token, aal, authentication_methods)
33
VALUES ('7458af86-c1d8-401c-978a-8da89133f78b', '884f556e-eb3a-4b9f-bee3-11345642c6c0', '2013-10-07 08:23:19',
4-
'2013-10-07 08:23:19', '2013-10-07 08:23:19', '2013-10-07 08:23:19', '2013-10-07 08:23:19',
4+
'2080-10-07 08:23:19', '2013-10-07 08:23:19', '2013-10-07 08:23:19', '2013-10-07 08:23:19',
55
'eVwBt7UAAAAVwBt7UWPw', '5ff66179-c240-4703-b0d8-494592cefff5', true, '123eVwBt7UAAAeVwBt7UWPw', 'aal2',
66
'[{"method":"password"},{"method":"totp"}]');
77

session/handler_test.go

+46
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,52 @@ func TestHandlerAdminSessionManagement(t *testing.T) {
649649
})
650650
})
651651

652+
t.Run("case=session status should be false for inactive identity", func(t *testing.T) {
653+
client := testhelpers.NewClientWithCookies(t)
654+
var s *Session
655+
require.NoError(t, faker.FakeData(&s))
656+
s.Active = true
657+
s.Identity.State = identity.StateInactive
658+
require.NoError(t, reg.Persister().CreateIdentity(ctx, s.Identity))
659+
660+
assert.Equal(t, uuid.Nil, s.ID)
661+
require.NoError(t, reg.SessionPersister().UpsertSession(ctx, s))
662+
assert.NotEqual(t, uuid.Nil, s.ID)
663+
assert.NotEqual(t, uuid.Nil, s.Identity.ID)
664+
665+
req, _ := http.NewRequest("GET", ts.URL+"/admin/sessions/"+s.ID.String()+"?expand=Identity", nil)
666+
res, err := client.Do(req)
667+
require.NoError(t, err)
668+
assert.Equal(t, http.StatusOK, res.StatusCode)
669+
670+
body, err := io.ReadAll(res.Body)
671+
require.NoError(t, err)
672+
assert.Equal(t, "false", gjson.GetBytes(body, "active").String(), "%s", body)
673+
})
674+
675+
t.Run("case=session status should be false when session expiry is past", func(t *testing.T) {
676+
client := testhelpers.NewClientWithCookies(t)
677+
var s *Session
678+
require.NoError(t, faker.FakeData(&s))
679+
s.Active = true
680+
s.ExpiresAt = time.Now().Add(-time.Hour * 1)
681+
require.NoError(t, reg.Persister().CreateIdentity(ctx, s.Identity))
682+
683+
assert.Equal(t, uuid.Nil, s.ID)
684+
require.NoError(t, reg.SessionPersister().UpsertSession(ctx, s))
685+
assert.NotEqual(t, uuid.Nil, s.ID)
686+
assert.NotEqual(t, uuid.Nil, s.Identity.ID)
687+
688+
req, _ := http.NewRequest("GET", ts.URL+"/admin/sessions/"+s.ID.String(), nil)
689+
res, err := client.Do(req)
690+
require.NoError(t, err)
691+
assert.Equal(t, http.StatusOK, res.StatusCode)
692+
693+
body, err := io.ReadAll(res.Body)
694+
require.NoError(t, err)
695+
assert.Equal(t, "false", gjson.GetBytes(body, "active").String(), "%s", body)
696+
})
697+
652698
t.Run("case=should return 400 when bad UUID is sent", func(t *testing.T) {
653699
client := testhelpers.NewClientWithCookies(t)
654700

session/session.go

+11
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,17 @@ func (s Session) TableName(ctx context.Context) string {
148148
return "sessions"
149149
}
150150

151+
func (s Session) MarshalJSON() ([]byte, error) {
152+
type sl Session
153+
s.Active = s.IsActive()
154+
155+
result, err := json.Marshal(sl(s))
156+
if err != nil {
157+
return nil, err
158+
}
159+
return result, nil
160+
}
161+
151162
func (s *Session) CompletedLoginFor(method identity.CredentialsType, aal identity.AuthenticatorAssuranceLevel) {
152163
s.AMR = append(s.AMR, AuthenticationMethod{Method: method, AAL: aal, CompletedAt: time.Now().UTC()})
153164
}

0 commit comments

Comments
 (0)