|
|
|
@ -18,6 +18,7 @@ import ( |
|
|
|
|
"github.com/grafana/grafana/pkg/login/social/socialtest" |
|
|
|
|
"github.com/grafana/grafana/pkg/services/auth/identity" |
|
|
|
|
"github.com/grafana/grafana/pkg/services/authn" |
|
|
|
|
"github.com/grafana/grafana/pkg/services/featuremgmt" |
|
|
|
|
"github.com/grafana/grafana/pkg/services/login" |
|
|
|
|
"github.com/grafana/grafana/pkg/services/oauthtoken/oauthtokentest" |
|
|
|
|
"github.com/grafana/grafana/pkg/services/org" |
|
|
|
@ -37,6 +38,8 @@ func TestOAuth_Authenticate(t *testing.T) { |
|
|
|
|
addPKCECookie bool |
|
|
|
|
pkceCookieValue string |
|
|
|
|
|
|
|
|
|
features []any |
|
|
|
|
|
|
|
|
|
isEmailAllowed bool |
|
|
|
|
userInfo *social.BasicUserInfo |
|
|
|
|
|
|
|
|
@ -120,6 +123,24 @@ func TestOAuth_Authenticate(t *testing.T) { |
|
|
|
|
isEmailAllowed: false, |
|
|
|
|
expectedErr: errOAuthEmailNotAllowed, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
desc: "should return error when no auth id is set and feature toggle is enabled", |
|
|
|
|
req: &authn.Request{ |
|
|
|
|
HTTPRequest: &http.Request{ |
|
|
|
|
Header: map[string][]string{}, |
|
|
|
|
URL: mustParseURL("http://grafana.com/?state=some-state"), |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
features: []any{featuremgmt.FlagOauthRequireSubClaim}, |
|
|
|
|
oauthCfg: &social.OAuthInfo{UsePKCE: true, Enabled: true}, |
|
|
|
|
addStateCookie: true, |
|
|
|
|
stateCookieValue: "some-state", |
|
|
|
|
addPKCECookie: true, |
|
|
|
|
pkceCookieValue: "some-pkce-value", |
|
|
|
|
userInfo: &social.BasicUserInfo{Email: "some@email.com"}, |
|
|
|
|
isEmailAllowed: false, |
|
|
|
|
expectedErr: errOAuthUserInfo, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
desc: "should return identity for valid request", |
|
|
|
|
req: &authn.Request{HTTPRequest: &http.Request{ |
|
|
|
@ -197,6 +218,42 @@ func TestOAuth_Authenticate(t *testing.T) { |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
desc: "should return identity when feature toggle is enabled and auth id is set", |
|
|
|
|
req: &authn.Request{ |
|
|
|
|
HTTPRequest: &http.Request{ |
|
|
|
|
Header: map[string][]string{}, |
|
|
|
|
URL: mustParseURL("http://grafana.com/?state=some-state"), |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
oauthCfg: &social.OAuthInfo{Enabled: true}, |
|
|
|
|
addStateCookie: true, |
|
|
|
|
stateCookieValue: "some-state", |
|
|
|
|
isEmailAllowed: true, |
|
|
|
|
features: []any{featuremgmt.FlagOauthRequireSubClaim}, |
|
|
|
|
userInfo: &social.BasicUserInfo{ |
|
|
|
|
Id: "123", |
|
|
|
|
Name: "name", |
|
|
|
|
Email: "some@email.com", |
|
|
|
|
Role: "Admin", |
|
|
|
|
}, |
|
|
|
|
expectedIdentity: &authn.Identity{ |
|
|
|
|
Email: "some@email.com", |
|
|
|
|
AuthenticatedBy: login.AzureADAuthModule, |
|
|
|
|
AuthID: "123", |
|
|
|
|
Name: "name", |
|
|
|
|
OAuthToken: &oauth2.Token{}, |
|
|
|
|
OrgRoles: map[int64]org.RoleType{1: org.RoleAdmin}, |
|
|
|
|
ClientParams: authn.ClientParams{ |
|
|
|
|
SyncUser: true, |
|
|
|
|
SyncTeams: true, |
|
|
|
|
AllowSignUp: true, |
|
|
|
|
FetchSyncedUser: true, |
|
|
|
|
SyncOrgRoles: true, |
|
|
|
|
LookUpParams: login.UserLookupParams{}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for _, tt := range tests { |
|
|
|
@ -231,7 +288,7 @@ func TestOAuth_Authenticate(t *testing.T) { |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
c := ProvideOAuth(authn.ClientWithPrefix("azuread"), cfg, nil, fakeSocialSvc, settingsProvider) |
|
|
|
|
c := ProvideOAuth(authn.ClientWithPrefix("azuread"), cfg, nil, fakeSocialSvc, settingsProvider, featuremgmt.WithFeatures(tt.features...)) |
|
|
|
|
|
|
|
|
|
identity, err := c.Authenticate(context.Background(), tt.req) |
|
|
|
|
assert.ErrorIs(t, err, tt.expectedErr) |
|
|
|
@ -314,7 +371,7 @@ func TestOAuth_RedirectURL(t *testing.T) { |
|
|
|
|
|
|
|
|
|
cfg := setting.NewCfg() |
|
|
|
|
|
|
|
|
|
c := ProvideOAuth(authn.ClientWithPrefix("azuread"), cfg, nil, fakeSocialSvc, &setting.OSSImpl{Cfg: cfg}) |
|
|
|
|
c := ProvideOAuth(authn.ClientWithPrefix("azuread"), cfg, nil, fakeSocialSvc, &setting.OSSImpl{Cfg: cfg}, featuremgmt.WithFeatures()) |
|
|
|
|
|
|
|
|
|
redirect, err := c.RedirectURL(context.Background(), nil) |
|
|
|
|
assert.ErrorIs(t, err, tt.expectedErr) |
|
|
|
@ -427,7 +484,7 @@ func TestOAuth_Logout(t *testing.T) { |
|
|
|
|
fakeSocialSvc := &socialtest.FakeSocialService{ |
|
|
|
|
ExpectedAuthInfoProvider: tt.oauthCfg, |
|
|
|
|
} |
|
|
|
|
c := ProvideOAuth(authn.ClientWithPrefix("azuread"), tt.cfg, mockService, fakeSocialSvc, &setting.OSSImpl{Cfg: tt.cfg}) |
|
|
|
|
c := ProvideOAuth(authn.ClientWithPrefix("azuread"), tt.cfg, mockService, fakeSocialSvc, &setting.OSSImpl{Cfg: tt.cfg}, featuremgmt.WithFeatures()) |
|
|
|
|
|
|
|
|
|
redirect, ok := c.Logout(context.Background(), &authn.Identity{}, &login.UserAuth{}) |
|
|
|
|
|
|
|
|
|