Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/api/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,9 @@ func (a *API) createAccountFromExternalIdentity(tx *storage.Connection, r *http.
user = decision.User
identity = decision.Identities[0]

now := time.Now()
identity.IdentityData = identityData
identity.LastSignInAt = &now
if terr = tx.UpdateOnly(identity, "identity_data", "last_sign_in_at"); terr != nil {
return 0, nil, terr
}
Expand Down
40 changes: 40 additions & 0 deletions internal/api/external_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/url"
"sync"
"testing"
"time"

"github.com/gofrs/uuid"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -636,3 +637,42 @@ func (ts *ExternalTestSuite) TestAfterUserCreatedHookNotFiredOnIdentityLinking()

ts.Require().Equal(0, hookCalls(), "hook must not fire when an identity is linked to an existing user")
}

// TestRepeatedOAuthSignInUpdatesIdentityLastSignInAt verifies that signing in
// again with an existing external identity refreshes identity.LastSignInAt.
func (ts *ExternalTestSuite) TestRepeatedOAuthSignInUpdatesIdentityLastSignInAt() {
userData := &provider.UserProvidedData{
Metadata: &provider.Claims{
Subject: "repeat-oauth-subject",
Email: "repeat-oauth@example.com",
EmailVerified: true,
},
Emails: []provider.Email{{
Email: "repeat-oauth@example.com",
Primary: true,
Verified: true,
}},
}
r := httptest.NewRequest(http.MethodGet, "/callback", nil)

decision, _, err := ts.API.createAccountFromExternalIdentity(ts.API.db, r, userData, "github", false)
ts.Require().NoError(err)
ts.Require().Equal(models.CreateAccount, decision)

firstIdentity, err := models.FindIdentityByIdAndProvider(ts.API.db, userData.Metadata.Subject, "github")
ts.Require().NoError(err)
ts.Require().NotNil(firstIdentity.LastSignInAt)

past := time.Now().Add(-time.Hour).UTC()
firstIdentity.LastSignInAt = &past
ts.Require().NoError(ts.API.db.UpdateOnly(firstIdentity, "last_sign_in_at"))

decision, _, err = ts.API.createAccountFromExternalIdentity(ts.API.db, r, userData, "github", false)
ts.Require().NoError(err)
ts.Require().Equal(models.AccountExists, decision)

updatedIdentity, err := models.FindIdentityByIdAndProvider(ts.API.db, userData.Metadata.Subject, "github")
ts.Require().NoError(err)
ts.Require().NotNil(updatedIdentity.LastSignInAt)
ts.Require().True(updatedIdentity.LastSignInAt.After(past), "identity.LastSignInAt should advance on subsequent OAuth sign-ins")
}