mirror of
https://github.com/grafana/grafana.git
synced 2024-11-23 09:26:43 -06:00
Login: Fix AuthInfo update process (#49556)
* Login: Fix AuthInfo update process * Fix GoDoc * Add regression test for oauth info overwrite Co-authored-by: jguer <joao.guerreiro@grafana.com>
This commit is contained in:
parent
eab806620c
commit
73a729bbe8
@ -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) }
|
||||
|
Loading…
Reference in New Issue
Block a user