mirror of
https://github.com/pomerium/pomerium.git
synced 2025-04-29 18:36:30 +02:00
identity: fix nil reference error when there is no authenticator (#3932)
identity: fix nil reference error when there is no authenticator (#3930) Co-authored-by: Caleb Doxsey <cdoxsey@pomerium.com>
This commit is contained in:
parent
7c69e612b6
commit
32985aabe6
2 changed files with 45 additions and 3 deletions
|
@ -187,6 +187,16 @@ func (mgr *Manager) refreshSession(ctx context.Context, userID, sessionID string
|
||||||
Str("session_id", sessionID).
|
Str("session_id", sessionID).
|
||||||
Msg("refreshing session")
|
Msg("refreshing session")
|
||||||
|
|
||||||
|
authenticator := mgr.cfg.Load().authenticator
|
||||||
|
if authenticator == nil {
|
||||||
|
log.Info(ctx).
|
||||||
|
Str("user_id", userID).
|
||||||
|
Str("session_id", sessionID).
|
||||||
|
Msg("no authenticator defined, deleting session")
|
||||||
|
mgr.deleteSession(ctx, userID, sessionID)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
s, ok := mgr.sessions.Get(userID, sessionID)
|
s, ok := mgr.sessions.Get(userID, sessionID)
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Warn(ctx).
|
log.Warn(ctx).
|
||||||
|
@ -214,7 +224,7 @@ func (mgr *Manager) refreshSession(ctx context.Context, userID, sessionID string
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
newToken, err := mgr.cfg.Load().authenticator.Refresh(ctx, FromOAuthToken(s.OauthToken), &s)
|
newToken, err := authenticator.Refresh(ctx, FromOAuthToken(s.OauthToken), &s)
|
||||||
metrics.RecordIdentityManagerSessionRefresh(ctx, err)
|
metrics.RecordIdentityManagerSessionRefresh(ctx, err)
|
||||||
mgr.recordLastError(metrics_ids.IdentityManagerLastSessionRefreshError, err)
|
mgr.recordLastError(metrics_ids.IdentityManagerLastSessionRefreshError, err)
|
||||||
if isTemporaryError(err) {
|
if isTemporaryError(err) {
|
||||||
|
@ -233,7 +243,7 @@ func (mgr *Manager) refreshSession(ctx context.Context, userID, sessionID string
|
||||||
}
|
}
|
||||||
s.OauthToken = ToOAuthToken(newToken)
|
s.OauthToken = ToOAuthToken(newToken)
|
||||||
|
|
||||||
err = mgr.cfg.Load().authenticator.UpdateUserInfo(ctx, FromOAuthToken(s.OauthToken), &s)
|
err = authenticator.UpdateUserInfo(ctx, FromOAuthToken(s.OauthToken), &s)
|
||||||
metrics.RecordIdentityManagerUserRefresh(ctx, err)
|
metrics.RecordIdentityManagerUserRefresh(ctx, err)
|
||||||
mgr.recordLastError(metrics_ids.IdentityManagerLastUserRefreshError, err)
|
mgr.recordLastError(metrics_ids.IdentityManagerLastUserRefreshError, err)
|
||||||
if isTemporaryError(err) {
|
if isTemporaryError(err) {
|
||||||
|
@ -268,6 +278,11 @@ func (mgr *Manager) refreshUser(ctx context.Context, userID string) {
|
||||||
Str("user_id", userID).
|
Str("user_id", userID).
|
||||||
Msg("refreshing user")
|
Msg("refreshing user")
|
||||||
|
|
||||||
|
authenticator := mgr.cfg.Load().authenticator
|
||||||
|
if authenticator == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
u, ok := mgr.users.Get(userID)
|
u, ok := mgr.users.Get(userID)
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Warn(ctx).
|
log.Warn(ctx).
|
||||||
|
@ -286,7 +301,7 @@ func (mgr *Manager) refreshUser(ctx context.Context, userID string) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
err := mgr.cfg.Load().authenticator.UpdateUserInfo(ctx, FromOAuthToken(s.OauthToken), &u)
|
err := authenticator.UpdateUserInfo(ctx, FromOAuthToken(s.OauthToken), &u)
|
||||||
metrics.RecordIdentityManagerUserRefresh(ctx, err)
|
metrics.RecordIdentityManagerUserRefresh(ctx, err)
|
||||||
mgr.recordLastError(metrics_ids.IdentityManagerLastUserRefreshError, err)
|
mgr.recordLastError(metrics_ids.IdentityManagerLastUserRefreshError, err)
|
||||||
if isTemporaryError(err) {
|
if isTemporaryError(err) {
|
||||||
|
|
|
@ -9,6 +9,8 @@ import (
|
||||||
"github.com/golang/mock/gomock"
|
"github.com/golang/mock/gomock"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
|
"google.golang.org/grpc/codes"
|
||||||
|
"google.golang.org/grpc/status"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
"google.golang.org/protobuf/types/known/timestamppb"
|
"google.golang.org/protobuf/types/known/timestamppb"
|
||||||
|
|
||||||
|
@ -36,6 +38,31 @@ func (mock mockAuthenticator) UpdateUserInfo(_ context.Context, _ *oauth2.Token,
|
||||||
return errors.New("update user info")
|
return errors.New("update user info")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestManager_refresh(t *testing.T) {
|
||||||
|
ctrl := gomock.NewController(t)
|
||||||
|
ctx, clearTimeout := context.WithTimeout(context.Background(), time.Second*10)
|
||||||
|
t.Cleanup(clearTimeout)
|
||||||
|
|
||||||
|
client := mock_databroker.NewMockDataBrokerServiceClient(ctrl)
|
||||||
|
mgr := New(WithDataBrokerClient(client))
|
||||||
|
mgr.onUpdateRecords(ctx, updateRecordsMessage{
|
||||||
|
records: []*databroker.Record{
|
||||||
|
databroker.NewRecord(&session.Session{
|
||||||
|
Id: "s1",
|
||||||
|
UserId: "u1",
|
||||||
|
OauthToken: &session.OAuthToken{},
|
||||||
|
ExpiresAt: timestamppb.New(time.Now().Add(time.Second * 10)),
|
||||||
|
}),
|
||||||
|
databroker.NewRecord(&user.User{
|
||||||
|
Id: "u1",
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
client.EXPECT().Get(gomock.Any(), gomock.Any()).Return(nil, status.Error(codes.NotFound, "not found"))
|
||||||
|
mgr.refreshSession(ctx, "u1", "s1")
|
||||||
|
mgr.refreshUser(ctx, "u1")
|
||||||
|
}
|
||||||
|
|
||||||
func TestManager_onUpdateRecords(t *testing.T) {
|
func TestManager_onUpdateRecords(t *testing.T) {
|
||||||
ctrl := gomock.NewController(t)
|
ctrl := gomock.NewController(t)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue