* Login: Fix AuthInfo update process
* Fix GoDoc
* Add regression test for oauth info overwrite
Co-authored-by: jguer <joao.guerreiro@grafana.com>
(cherry picked from commit 73a729bbe8)
Co-authored-by: Joan López de la Franca Beltran <5459617+joanlopez@users.noreply.github.com>
This commit is contained in:
co-authored by
Joan López de la Franca Beltran
parent
49e653dbaf
commit
312b57aa76
@@ -146,8 +146,8 @@ func (s *AuthInfoStore) SetAuthInfo(ctx context.Context, cmd *models.SetAuthInfo
|
||||
})
|
||||
}
|
||||
|
||||
// UpdateAuthInfo updates the auth info for the user with the latest date. Avoids overlapping entries hiding
|
||||
// the last used one (ex: LDAP->SAML->LDAP)
|
||||
// UpdateAuthInfoDate updates the auth info for the user with the latest date.
|
||||
// Avoids overlapping entries hiding the last used one (ex: LDAP->SAML->LDAP).
|
||||
func (s *AuthInfoStore) UpdateAuthInfoDate(ctx context.Context, authInfo *models.UserAuth) error {
|
||||
authInfo.Created = GetTime()
|
||||
|
||||
@@ -157,7 +157,7 @@ func (s *AuthInfoStore) UpdateAuthInfoDate(ctx context.Context, authInfo *models
|
||||
AuthModule: authInfo.AuthModule,
|
||||
}
|
||||
return s.sqlStore.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
_, err := sess.Update(authInfo, cond)
|
||||
_, err := sess.Cols("created").Update(authInfo, cond)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
@@ -278,6 +278,16 @@ func TestUserAuth(t *testing.T) {
|
||||
|
||||
// Now reuse first auth module and make sure it's updated to the most recent
|
||||
database.GetTime = func() time.Time { return fixedTime }
|
||||
|
||||
// add oauth info to auth_info to make sure update date does not overwrite it
|
||||
updateAuthCmd := &models.UpdateAuthInfoCommand{UserId: user.Id, AuthModule: "test1", AuthId: "test1", OAuthToken: &oauth2.Token{
|
||||
AccessToken: "access_token",
|
||||
TokenType: "token_type",
|
||||
RefreshToken: "refresh_token",
|
||||
Expiry: fixedTime,
|
||||
}}
|
||||
err = authInfoStore.UpdateAuthInfo(context.Background(), updateAuthCmd)
|
||||
require.Nil(t, err)
|
||||
user, err = srv.LookupAndUpdate(context.Background(), queryOne)
|
||||
|
||||
require.Nil(t, err)
|
||||
@@ -287,6 +297,8 @@ func TestUserAuth(t *testing.T) {
|
||||
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, "test1", getAuthQuery.Result.AuthModule)
|
||||
// make sure oauth info is not overwritten by update date
|
||||
require.Equal(t, "access_token", getAuthQuery.Result.OAuthAccessToken)
|
||||
|
||||
// Now reuse second auth module and make sure it's updated to the most recent
|
||||
database.GetTime = func() time.Time { return fixedTime.AddDate(0, 0, 1) }
|
||||
|
||||
Reference in New Issue
Block a user