Update used credentials on external authentication
This commit is contained in:
@@ -100,7 +100,7 @@ func (r *credentials) Create(c *types.Credentials) (*types.Credentials, error) {
|
|||||||
func (r *credentials) Update(c *types.Credentials) (*types.Credentials, error) {
|
func (r *credentials) Update(c *types.Credentials) (*types.Credentials, error) {
|
||||||
updatedAt := time.Now()
|
updatedAt := time.Now()
|
||||||
c.UpdatedAt = &updatedAt
|
c.UpdatedAt = &updatedAt
|
||||||
return c, r.db().Update(r.tblname, c)
|
return c, r.db().Replace(r.tblname, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *credentials) DeleteByID(id uint64) error {
|
func (r *credentials) DeleteByID(id uint64) error {
|
||||||
|
|||||||
@@ -112,6 +112,19 @@ func (svc *auth) External(profile goth.User) (u *types.User, err error) {
|
|||||||
c.ID)
|
c.ID)
|
||||||
} else if u.Valid() {
|
} else if u.Valid() {
|
||||||
// Valid user, matching emails. Bingo!
|
// Valid user, matching emails. Bingo!
|
||||||
|
c.LastUsedAt = svc.now()
|
||||||
|
if c, err = svc.credentials.Update(c); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf(
|
||||||
|
"updating credential entry (%v, %v) for exisintg user (%v, %v)",
|
||||||
|
c.ID,
|
||||||
|
profile.Provider,
|
||||||
|
u.ID,
|
||||||
|
u.Email,
|
||||||
|
)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
} else {
|
} else {
|
||||||
// Scenario: linked to an invalid user
|
// Scenario: linked to an invalid user
|
||||||
@@ -161,50 +174,30 @@ func (svc *auth) External(profile goth.User) (u *types.User, err error) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
if c == nil {
|
c = &types.Credentials{
|
||||||
c = &types.Credentials{
|
Kind: profile.Provider,
|
||||||
Kind: profile.Provider,
|
OwnerID: u.ID,
|
||||||
OwnerID: u.ID,
|
Credentials: profile.UserID,
|
||||||
Credentials: profile.UserID,
|
LastUsedAt: svc.now(),
|
||||||
LastUsedAt: svc.now(),
|
|
||||||
}
|
|
||||||
|
|
||||||
if !profile.ExpiresAt.IsZero() {
|
|
||||||
// Copy expiration date when provided
|
|
||||||
c.ExpiresAt = &profile.ExpiresAt
|
|
||||||
}
|
|
||||||
|
|
||||||
if c, err = svc.credentials.Create(c); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf(
|
|
||||||
"creating new credential entry (%v, %v) for exisintg user (%v, %v)",
|
|
||||||
c.ID,
|
|
||||||
profile.Provider,
|
|
||||||
u.ID,
|
|
||||||
u.Email,
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
if !profile.ExpiresAt.IsZero() {
|
|
||||||
// Copy expiration date when provided
|
|
||||||
c.ExpiresAt = &profile.ExpiresAt
|
|
||||||
}
|
|
||||||
|
|
||||||
c.LastUsedAt = svc.now()
|
|
||||||
if c, err = svc.credentials.Update(c); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf(
|
|
||||||
"updating credential entry (%v, %v) for exisintg user (%v, %v)",
|
|
||||||
c.ID,
|
|
||||||
profile.Provider,
|
|
||||||
u.ID,
|
|
||||||
u.Email,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !profile.ExpiresAt.IsZero() {
|
||||||
|
// Copy expiration date when provided
|
||||||
|
c.ExpiresAt = &profile.ExpiresAt
|
||||||
|
}
|
||||||
|
|
||||||
|
if c, err = svc.credentials.Create(c); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf(
|
||||||
|
"creating new credential entry (%v, %v) for exisintg user (%v, %v)",
|
||||||
|
c.ID,
|
||||||
|
profile.Provider,
|
||||||
|
u.ID,
|
||||||
|
u.Email,
|
||||||
|
)
|
||||||
|
|
||||||
// Owner loaded, carry on.
|
// Owner loaded, carry on.
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -56,6 +56,11 @@ func TestAuth_External_Existing(t *testing.T) {
|
|||||||
Times(1).
|
Times(1).
|
||||||
Return(types.CredentialsSet{c}, nil)
|
Return(types.CredentialsSet{c}, nil)
|
||||||
|
|
||||||
|
crdRpoMock.EXPECT().
|
||||||
|
Update(gomock.Any()).
|
||||||
|
Times(1).
|
||||||
|
Return(c, nil)
|
||||||
|
|
||||||
usrRpoMock := repomock.NewMockUserRepository(mockCtrl)
|
usrRpoMock := repomock.NewMockUserRepository(mockCtrl)
|
||||||
usrRpoMock.EXPECT().FindByID(u.ID).Times(1).Return(u, nil)
|
usrRpoMock.EXPECT().FindByID(u.ID).Times(1).Return(u, nil)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user