2018-05-11 09:55:32 +02:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2018-05-11 09:55:32 +02:00
|
|
|
|
2022-09-02 21:18:23 +02:00
|
|
|
package integration
|
2018-05-11 09:55:32 +02:00
|
|
|
|
|
|
|
import (
|
2019-12-15 10:51:28 +01:00
|
|
|
"context"
|
2018-05-11 09:55:32 +02:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2023-02-02 08:45:00 +01:00
|
|
|
auth_model "code.gitea.io/gitea/models/auth"
|
2022-03-29 08:29:02 +02:00
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
"code.gitea.io/gitea/models/organization"
|
2022-02-11 15:24:58 +01:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2024-12-15 12:59:18 +01:00
|
|
|
"code.gitea.io/gitea/modules/optional"
|
|
|
|
"code.gitea.io/gitea/modules/test"
|
2022-06-26 16:19:22 +02:00
|
|
|
"code.gitea.io/gitea/modules/translation"
|
2024-12-15 12:59:18 +01:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-07-24 12:16:34 +02:00
|
|
|
"code.gitea.io/gitea/services/auth"
|
2023-02-02 08:45:00 +01:00
|
|
|
"code.gitea.io/gitea/services/auth/source/ldap"
|
2024-11-27 22:12:26 +01:00
|
|
|
org_service "code.gitea.io/gitea/services/org"
|
2022-09-02 21:18:23 +02:00
|
|
|
"code.gitea.io/gitea/tests"
|
2018-05-11 09:55:32 +02:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-12-15 12:59:18 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-05-11 09:55:32 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type ldapUser struct {
|
2020-03-05 07:30:33 +01:00
|
|
|
UserName string
|
|
|
|
Password string
|
|
|
|
FullName string
|
|
|
|
Email string
|
|
|
|
OtherEmails []string
|
|
|
|
IsAdmin bool
|
|
|
|
IsRestricted bool
|
|
|
|
SSHKeys []string
|
2018-05-11 09:55:32 +02:00
|
|
|
}
|
|
|
|
|
2024-12-15 12:59:18 +01:00
|
|
|
type ldapTestEnv struct {
|
|
|
|
gitLDAPUsers []ldapUser
|
|
|
|
otherLDAPUsers []ldapUser
|
|
|
|
serverHost string
|
|
|
|
serverPort string
|
2018-05-11 09:55:32 +02:00
|
|
|
}
|
|
|
|
|
2024-12-15 12:59:18 +01:00
|
|
|
func prepareLdapTestEnv(t *testing.T) *ldapTestEnv {
|
|
|
|
if os.Getenv("TEST_LDAP") != "1" {
|
|
|
|
t.Skip()
|
|
|
|
return nil
|
|
|
|
}
|
2018-05-11 09:55:32 +02:00
|
|
|
|
2024-12-15 12:59:18 +01:00
|
|
|
gitLDAPUsers := []ldapUser{
|
|
|
|
{
|
|
|
|
UserName: "professor",
|
|
|
|
Password: "professor",
|
|
|
|
FullName: "Hubert Farnsworth",
|
|
|
|
Email: "professor@planetexpress.com",
|
|
|
|
OtherEmails: []string{"hubert@planetexpress.com"},
|
|
|
|
IsAdmin: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
UserName: "hermes",
|
|
|
|
Password: "hermes",
|
|
|
|
FullName: "Conrad Hermes",
|
|
|
|
Email: "hermes@planetexpress.com",
|
|
|
|
SSHKeys: []string{
|
|
|
|
"SHA256:qLY06smKfHoW/92yXySpnxFR10QFrLdRjf/GNPvwcW8",
|
|
|
|
"SHA256:QlVTuM5OssDatqidn2ffY+Lc4YA5Fs78U+0KOHI51jQ",
|
|
|
|
"SHA256:DXdeUKYOJCSSmClZuwrb60hUq7367j4fA+udNC3FdRI",
|
|
|
|
},
|
|
|
|
IsAdmin: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
UserName: "fry",
|
|
|
|
Password: "fry",
|
|
|
|
FullName: "Philip Fry",
|
|
|
|
Email: "fry@planetexpress.com",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
UserName: "leela",
|
|
|
|
Password: "leela",
|
|
|
|
FullName: "Leela Turanga",
|
|
|
|
Email: "leela@planetexpress.com",
|
|
|
|
IsRestricted: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
UserName: "bender",
|
|
|
|
Password: "bender",
|
|
|
|
FullName: "Bender Rodríguez",
|
|
|
|
Email: "bender@planetexpress.com",
|
|
|
|
},
|
|
|
|
}
|
2018-05-11 09:55:32 +02:00
|
|
|
|
2024-12-15 12:59:18 +01:00
|
|
|
otherLDAPUsers := []ldapUser{
|
|
|
|
{
|
|
|
|
UserName: "zoidberg",
|
|
|
|
Password: "zoidberg",
|
|
|
|
FullName: "John Zoidberg",
|
|
|
|
Email: "zoidberg@planetexpress.com",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
UserName: "amy",
|
|
|
|
Password: "amy",
|
|
|
|
FullName: "Amy Kroker",
|
|
|
|
Email: "amy@planetexpress.com",
|
|
|
|
},
|
2018-05-11 09:55:32 +02:00
|
|
|
}
|
|
|
|
|
2024-12-15 12:59:18 +01:00
|
|
|
return &ldapTestEnv{
|
|
|
|
gitLDAPUsers: gitLDAPUsers,
|
|
|
|
otherLDAPUsers: otherLDAPUsers,
|
|
|
|
serverHost: util.IfZero(os.Getenv("TEST_LDAP_HOST"), "ldap"),
|
|
|
|
serverPort: util.IfZero(os.Getenv("TEST_LDAP_PORT"), "389"),
|
2023-02-02 08:45:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-15 12:59:18 +01:00
|
|
|
type ldapAuthOptions struct {
|
|
|
|
attributeUID optional.Option[string] // defaults to "uid"
|
|
|
|
attributeSSHPublicKey string
|
|
|
|
groupFilter string
|
|
|
|
groupTeamMap string
|
|
|
|
groupTeamMapRemoval string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (te *ldapTestEnv) buildAuthSourcePayload(csrf string, opts ...ldapAuthOptions) map[string]string {
|
|
|
|
opt := util.OptionalArg(opts)
|
2023-02-02 08:45:00 +01:00
|
|
|
// Modify user filter to test group filter explicitly
|
|
|
|
userFilter := "(&(objectClass=inetOrgPerson)(memberOf=cn=git,ou=people,dc=planetexpress,dc=com)(uid=%s))"
|
2024-12-15 12:59:18 +01:00
|
|
|
if opt.groupFilter != "" {
|
2023-02-02 08:45:00 +01:00
|
|
|
userFilter = "(&(objectClass=inetOrgPerson)(uid=%s))"
|
|
|
|
}
|
|
|
|
|
2023-02-08 07:44:42 +01:00
|
|
|
return map[string]string{
|
2018-05-24 06:59:02 +02:00
|
|
|
"_csrf": csrf,
|
|
|
|
"type": "2",
|
|
|
|
"name": "ldap",
|
2024-12-15 12:59:18 +01:00
|
|
|
"host": te.serverHost,
|
|
|
|
"port": te.serverPort,
|
2018-05-24 06:59:02 +02:00
|
|
|
"bind_dn": "uid=gitea,ou=service,dc=planetexpress,dc=com",
|
|
|
|
"bind_password": "password",
|
|
|
|
"user_base": "ou=people,dc=planetexpress,dc=com",
|
2023-02-02 08:45:00 +01:00
|
|
|
"filter": userFilter,
|
2018-05-24 06:59:02 +02:00
|
|
|
"admin_filter": "(memberOf=cn=admin_staff,ou=people,dc=planetexpress,dc=com)",
|
2020-03-05 07:30:33 +01:00
|
|
|
"restricted_filter": "(uid=leela)",
|
2024-12-15 12:59:18 +01:00
|
|
|
"attribute_username": util.Iif(opt.attributeUID.Has(), opt.attributeUID.Value(), "uid"),
|
2018-05-24 06:59:02 +02:00
|
|
|
"attribute_name": "givenName",
|
|
|
|
"attribute_surname": "sn",
|
|
|
|
"attribute_mail": "mail",
|
2024-12-15 12:59:18 +01:00
|
|
|
"attribute_ssh_public_key": opt.attributeSSHPublicKey,
|
2018-05-24 06:59:02 +02:00
|
|
|
"is_sync_enabled": "on",
|
|
|
|
"is_active": "on",
|
2022-02-11 15:24:58 +01:00
|
|
|
"groups_enabled": "on",
|
|
|
|
"group_dn": "ou=people,dc=planetexpress,dc=com",
|
|
|
|
"group_member_uid": "member",
|
2024-12-15 12:59:18 +01:00
|
|
|
"group_filter": opt.groupFilter,
|
|
|
|
"group_team_map": opt.groupTeamMap,
|
|
|
|
"group_team_map_removal": opt.groupTeamMapRemoval,
|
2022-02-11 15:24:58 +01:00
|
|
|
"user_uid": "DN",
|
2023-02-08 07:44:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-15 12:59:18 +01:00
|
|
|
func (te *ldapTestEnv) addAuthSource(t *testing.T, opts ...ldapAuthOptions) {
|
2023-02-08 07:44:42 +01:00
|
|
|
session := loginUser(t, "user1")
|
2024-10-10 05:48:21 +02:00
|
|
|
csrf := GetUserCSRFToken(t, session)
|
2024-12-15 12:59:18 +01:00
|
|
|
req := NewRequestWithValues(t, "POST", "/-/admin/auths/new", te.buildAuthSourcePayload(csrf, opts...))
|
2022-03-23 05:54:07 +01:00
|
|
|
session.MakeRequest(t, req, http.StatusSeeOther)
|
2018-05-11 09:55:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLDAPUserSignin(t *testing.T) {
|
2024-12-15 12:59:18 +01:00
|
|
|
te := prepareLdapTestEnv(t)
|
|
|
|
if te == nil {
|
2018-05-11 09:55:32 +02:00
|
|
|
return
|
|
|
|
}
|
2022-09-02 21:18:23 +02:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2024-12-15 12:59:18 +01:00
|
|
|
te.addAuthSource(t)
|
2018-05-11 09:55:32 +02:00
|
|
|
|
2024-12-15 12:59:18 +01:00
|
|
|
u := te.gitLDAPUsers[0]
|
2018-05-11 09:55:32 +02:00
|
|
|
|
|
|
|
session := loginUserWithPassword(t, u.UserName, u.Password)
|
|
|
|
req := NewRequest(t, "GET", "/user/settings")
|
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
|
|
|
|
|
|
|
assert.Equal(t, u.UserName, htmlDoc.GetInputValueByName("name"))
|
|
|
|
assert.Equal(t, u.FullName, htmlDoc.GetInputValueByName("full_name"))
|
2024-02-17 16:01:25 +01:00
|
|
|
assert.Equal(t, u.Email, htmlDoc.Find("#signed-user-email").Text())
|
2018-05-11 09:55:32 +02:00
|
|
|
}
|
|
|
|
|
2021-07-20 13:30:22 +02:00
|
|
|
func TestLDAPAuthChange(t *testing.T) {
|
2024-12-15 12:59:18 +01:00
|
|
|
te := prepareLdapTestEnv(t)
|
|
|
|
if te == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-02 21:18:23 +02:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2024-12-15 12:59:18 +01:00
|
|
|
te.addAuthSource(t)
|
2021-07-20 13:30:22 +02:00
|
|
|
|
|
|
|
session := loginUser(t, "user1")
|
2024-10-10 06:56:49 +02:00
|
|
|
req := NewRequest(t, "GET", "/-/admin/auths")
|
2021-07-20 13:30:22 +02:00
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
doc := NewHTMLParser(t, resp.Body)
|
|
|
|
href, exists := doc.Find("table.table td a").Attr("href")
|
|
|
|
if !exists {
|
|
|
|
assert.True(t, exists, "No authentication source found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req = NewRequest(t, "GET", href)
|
|
|
|
resp = session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
doc = NewHTMLParser(t, resp.Body)
|
|
|
|
csrf := doc.GetCSRF()
|
|
|
|
host, _ := doc.Find(`input[name="host"]`).Attr("value")
|
2024-12-15 12:59:18 +01:00
|
|
|
assert.Equal(t, te.serverHost, host)
|
2021-07-20 13:30:22 +02:00
|
|
|
binddn, _ := doc.Find(`input[name="bind_dn"]`).Attr("value")
|
2023-04-22 23:56:27 +02:00
|
|
|
assert.Equal(t, "uid=gitea,ou=service,dc=planetexpress,dc=com", binddn)
|
2021-07-20 13:30:22 +02:00
|
|
|
|
2024-12-15 12:59:18 +01:00
|
|
|
req = NewRequestWithValues(t, "POST", href, te.buildAuthSourcePayload(csrf, ldapAuthOptions{groupTeamMapRemoval: "off"}))
|
2022-03-23 05:54:07 +01:00
|
|
|
session.MakeRequest(t, req, http.StatusSeeOther)
|
2021-07-20 13:30:22 +02:00
|
|
|
|
|
|
|
req = NewRequest(t, "GET", href)
|
|
|
|
resp = session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
doc = NewHTMLParser(t, resp.Body)
|
|
|
|
host, _ = doc.Find(`input[name="host"]`).Attr("value")
|
2024-12-15 12:59:18 +01:00
|
|
|
assert.Equal(t, te.serverHost, host)
|
2021-07-20 13:30:22 +02:00
|
|
|
binddn, _ = doc.Find(`input[name="bind_dn"]`).Attr("value")
|
2023-04-22 23:56:27 +02:00
|
|
|
assert.Equal(t, "uid=gitea,ou=service,dc=planetexpress,dc=com", binddn)
|
2021-07-20 13:30:22 +02:00
|
|
|
}
|
|
|
|
|
2018-05-11 09:55:32 +02:00
|
|
|
func TestLDAPUserSync(t *testing.T) {
|
2024-12-15 12:59:18 +01:00
|
|
|
te := prepareLdapTestEnv(t)
|
|
|
|
if te == nil {
|
2018-05-11 09:55:32 +02:00
|
|
|
return
|
|
|
|
}
|
2024-12-15 12:59:18 +01:00
|
|
|
|
2022-09-02 21:18:23 +02:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2024-12-15 12:59:18 +01:00
|
|
|
te.addAuthSource(t)
|
allow synchronizing user status from OAuth2 login providers (#31572)
This leverages the existing `sync_external_users` cron job to
synchronize the `IsActive` flag on users who use an OAuth2 provider set
to synchronize. This synchronization is done by checking for expired
access tokens, and using the stored refresh token to request a new
access token. If the response back from the OAuth2 provider is the
`invalid_grant` error code, the user is marked as inactive. However, the
user is able to reactivate their account by logging in the web browser
through their OAuth2 flow.
Also changed to support this is that a linked `ExternalLoginUser` is
always created upon a login or signup via OAuth2.
### Notes on updating permissions
Ideally, we would also refresh permissions from the configured OAuth
provider (e.g., admin, restricted and group mappings) to match the
implementation of LDAP. However, the OAuth library used for this `goth`,
doesn't seem to support issuing a session via refresh tokens. The
interface provides a [`RefreshToken`
method](https://github.com/markbates/goth/blob/master/provider.go#L20),
but the returned `oauth.Token` doesn't implement the `goth.Session` we
would need to call `FetchUser`. Due to specific implementations, we
would need to build a compatibility function for every provider, since
they cast to concrete types (e.g.
[Azure](https://github.com/markbates/goth/blob/master/providers/azureadv2/azureadv2.go#L132))
---------
Co-authored-by: Kyle D <kdumontnu@gmail.com>
2024-07-16 20:33:16 +02:00
|
|
|
err := auth.SyncExternalUsers(context.Background(), true)
|
|
|
|
assert.NoError(t, err)
|
2018-05-11 09:55:32 +02:00
|
|
|
|
|
|
|
// Check if users exists
|
2024-12-15 12:59:18 +01:00
|
|
|
for _, gitLDAPUser := range te.gitLDAPUsers {
|
2023-07-22 12:54:48 +02:00
|
|
|
dbUser, err := user_model.GetUserByName(db.DefaultContext, gitLDAPUser.UserName)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, gitLDAPUser.UserName, dbUser.Name)
|
|
|
|
assert.Equal(t, gitLDAPUser.Email, dbUser.Email)
|
|
|
|
assert.Equal(t, gitLDAPUser.IsAdmin, dbUser.IsAdmin)
|
|
|
|
assert.Equal(t, gitLDAPUser.IsRestricted, dbUser.IsRestricted)
|
2018-05-11 09:55:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if no users exist
|
2024-12-15 12:59:18 +01:00
|
|
|
for _, otherLDAPUser := range te.otherLDAPUsers {
|
2023-07-22 12:54:48 +02:00
|
|
|
_, err := user_model.GetUserByName(db.DefaultContext, otherLDAPUser.UserName)
|
|
|
|
assert.True(t, user_model.IsErrUserNotExist(err))
|
2018-05-11 09:55:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-20 05:04:13 +02:00
|
|
|
func TestLDAPUserSyncWithEmptyUsernameAttribute(t *testing.T) {
|
2024-12-15 12:59:18 +01:00
|
|
|
te := prepareLdapTestEnv(t)
|
|
|
|
if te == nil {
|
2023-06-20 05:04:13 +02:00
|
|
|
return
|
|
|
|
}
|
2024-12-15 12:59:18 +01:00
|
|
|
|
2023-06-20 05:04:13 +02:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
|
|
|
|
session := loginUser(t, "user1")
|
2024-10-10 05:48:21 +02:00
|
|
|
csrf := GetUserCSRFToken(t, session)
|
2024-12-15 12:59:18 +01:00
|
|
|
payload := te.buildAuthSourcePayload(csrf)
|
2023-06-20 05:04:13 +02:00
|
|
|
payload["attribute_username"] = ""
|
2024-10-10 06:56:49 +02:00
|
|
|
req := NewRequestWithValues(t, "POST", "/-/admin/auths/new", payload)
|
2023-06-20 05:04:13 +02:00
|
|
|
session.MakeRequest(t, req, http.StatusSeeOther)
|
|
|
|
|
2024-12-15 12:59:18 +01:00
|
|
|
for _, u := range te.gitLDAPUsers {
|
2024-10-10 06:56:49 +02:00
|
|
|
req := NewRequest(t, "GET", "/-/admin/users?q="+u.UserName)
|
2023-06-20 05:04:13 +02:00
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
|
|
|
|
|
|
|
tr := htmlDoc.doc.Find("table.table tbody tr")
|
2024-12-15 11:41:29 +01:00
|
|
|
assert.Equal(t, 0, tr.Length())
|
2023-06-20 05:04:13 +02:00
|
|
|
}
|
|
|
|
|
2024-12-15 12:59:18 +01:00
|
|
|
for _, u := range te.gitLDAPUsers {
|
2023-06-20 05:04:13 +02:00
|
|
|
req := NewRequestWithValues(t, "POST", "/user/login", map[string]string{
|
|
|
|
"_csrf": csrf,
|
|
|
|
"user_name": u.UserName,
|
|
|
|
"password": u.Password,
|
|
|
|
})
|
|
|
|
MakeRequest(t, req, http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
2024-12-15 12:59:18 +01:00
|
|
|
require.NoError(t, auth.SyncExternalUsers(context.Background(), true))
|
2023-06-20 05:04:13 +02:00
|
|
|
|
|
|
|
authSource := unittest.AssertExistsAndLoadBean(t, &auth_model.Source{
|
|
|
|
Name: payload["name"],
|
|
|
|
})
|
|
|
|
unittest.AssertCount(t, &user_model.User{
|
|
|
|
LoginType: auth_model.LDAP,
|
|
|
|
LoginSource: authSource.ID,
|
2024-12-15 12:59:18 +01:00
|
|
|
}, len(te.gitLDAPUsers))
|
2023-06-20 05:04:13 +02:00
|
|
|
|
2024-12-15 12:59:18 +01:00
|
|
|
for _, u := range te.gitLDAPUsers {
|
2023-06-20 05:04:13 +02:00
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{
|
|
|
|
Name: u.UserName,
|
|
|
|
})
|
|
|
|
assert.True(t, user.IsActive)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-02 08:45:00 +01:00
|
|
|
func TestLDAPUserSyncWithGroupFilter(t *testing.T) {
|
2024-12-15 12:59:18 +01:00
|
|
|
te := prepareLdapTestEnv(t)
|
|
|
|
if te == nil {
|
2023-02-02 08:45:00 +01:00
|
|
|
return
|
|
|
|
}
|
2024-12-15 12:59:18 +01:00
|
|
|
|
2023-02-02 08:45:00 +01:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2024-12-15 12:59:18 +01:00
|
|
|
te.addAuthSource(t, ldapAuthOptions{groupFilter: "(cn=git)"})
|
2023-02-02 08:45:00 +01:00
|
|
|
|
|
|
|
// Assert a user not a member of the LDAP group "cn=git" cannot login
|
|
|
|
// This test may look like TestLDAPUserSigninFailed but it is not.
|
|
|
|
// The later test uses user filter containing group membership filter (memberOf)
|
|
|
|
// This test is for the case when LDAP user records may not be linked with
|
|
|
|
// all groups the user is a member of, the user filter is modified accordingly inside
|
|
|
|
// the addAuthSourceLDAP based on the value of the groupFilter
|
2024-12-15 12:59:18 +01:00
|
|
|
u := te.otherLDAPUsers[0]
|
2024-02-14 22:48:45 +01:00
|
|
|
testLoginFailed(t, u.UserName, u.Password, translation.NewLocale("en-US").TrString("form.username_password_incorrect"))
|
2023-02-02 08:45:00 +01:00
|
|
|
|
2024-12-15 12:59:18 +01:00
|
|
|
require.NoError(t, auth.SyncExternalUsers(context.Background(), true))
|
2023-02-02 08:45:00 +01:00
|
|
|
|
|
|
|
// Assert members of LDAP group "cn=git" are added
|
2024-12-15 12:59:18 +01:00
|
|
|
for _, gitLDAPUser := range te.gitLDAPUsers {
|
2023-02-02 08:45:00 +01:00
|
|
|
unittest.BeanExists(t, &user_model.User{
|
|
|
|
Name: gitLDAPUser.UserName,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert everyone else is not added
|
2024-12-15 12:59:18 +01:00
|
|
|
for _, gitLDAPUser := range te.otherLDAPUsers {
|
2023-02-02 08:45:00 +01:00
|
|
|
unittest.AssertNotExistsBean(t, &user_model.User{
|
|
|
|
Name: gitLDAPUser.UserName,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
ldapSource := unittest.AssertExistsAndLoadBean(t, &auth_model.Source{
|
|
|
|
Name: "ldap",
|
|
|
|
})
|
|
|
|
ldapConfig := ldapSource.Cfg.(*ldap.Source)
|
|
|
|
ldapConfig.GroupFilter = "(cn=ship_crew)"
|
2024-12-15 12:59:18 +01:00
|
|
|
require.NoError(t, auth_model.UpdateSource(db.DefaultContext, ldapSource))
|
2023-02-02 08:45:00 +01:00
|
|
|
|
2024-12-15 12:59:18 +01:00
|
|
|
require.NoError(t, auth.SyncExternalUsers(context.Background(), true))
|
2023-02-02 08:45:00 +01:00
|
|
|
|
2024-12-15 12:59:18 +01:00
|
|
|
for _, gitLDAPUser := range te.gitLDAPUsers {
|
2023-02-02 08:45:00 +01:00
|
|
|
if gitLDAPUser.UserName == "fry" || gitLDAPUser.UserName == "leela" || gitLDAPUser.UserName == "bender" {
|
|
|
|
// Assert members of the LDAP group "cn-ship_crew" are still active
|
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{
|
|
|
|
Name: gitLDAPUser.UserName,
|
|
|
|
})
|
|
|
|
assert.True(t, user.IsActive, "User %s should be active", gitLDAPUser.UserName)
|
|
|
|
} else {
|
|
|
|
// Assert everyone else is inactive
|
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{
|
|
|
|
Name: gitLDAPUser.UserName,
|
|
|
|
})
|
|
|
|
assert.False(t, user.IsActive, "User %s should be inactive", gitLDAPUser.UserName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-11 09:55:32 +02:00
|
|
|
func TestLDAPUserSigninFailed(t *testing.T) {
|
2024-12-15 12:59:18 +01:00
|
|
|
te := prepareLdapTestEnv(t)
|
|
|
|
if te == nil {
|
2018-05-11 09:55:32 +02:00
|
|
|
return
|
|
|
|
}
|
2024-12-15 12:59:18 +01:00
|
|
|
|
2022-09-02 21:18:23 +02:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2024-12-15 12:59:18 +01:00
|
|
|
te.addAuthSource(t)
|
2018-05-11 09:55:32 +02:00
|
|
|
|
2024-12-15 12:59:18 +01:00
|
|
|
u := te.otherLDAPUsers[0]
|
2024-02-14 22:48:45 +01:00
|
|
|
testLoginFailed(t, u.UserName, u.Password, translation.NewLocale("en-US").TrString("form.username_password_incorrect"))
|
2018-05-11 09:55:32 +02:00
|
|
|
}
|
2018-05-24 06:59:02 +02:00
|
|
|
|
|
|
|
func TestLDAPUserSSHKeySync(t *testing.T) {
|
2024-12-15 12:59:18 +01:00
|
|
|
te := prepareLdapTestEnv(t)
|
|
|
|
if te == nil {
|
2018-05-24 06:59:02 +02:00
|
|
|
return
|
|
|
|
}
|
2024-12-15 12:59:18 +01:00
|
|
|
|
2022-09-02 21:18:23 +02:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2024-12-15 12:59:18 +01:00
|
|
|
te.addAuthSource(t, ldapAuthOptions{attributeSSHPublicKey: "sshPublicKey"})
|
2019-12-15 10:51:28 +01:00
|
|
|
|
2024-12-15 12:59:18 +01:00
|
|
|
require.NoError(t, auth.SyncExternalUsers(context.Background(), true))
|
2018-05-24 06:59:02 +02:00
|
|
|
|
|
|
|
// Check if users has SSH keys synced
|
2024-12-15 12:59:18 +01:00
|
|
|
for _, u := range te.gitLDAPUsers {
|
2018-05-24 06:59:02 +02:00
|
|
|
if len(u.SSHKeys) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
session := loginUserWithPassword(t, u.UserName, u.Password)
|
|
|
|
|
|
|
|
req := NewRequest(t, "GET", "/user/settings/keys")
|
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
|
|
|
|
2023-08-01 00:13:42 +02:00
|
|
|
divs := htmlDoc.doc.Find("#keys-ssh .flex-item .flex-item-body:not(:last-child)")
|
2018-05-24 06:59:02 +02:00
|
|
|
|
|
|
|
syncedKeys := make([]string, divs.Length())
|
|
|
|
for i := 0; i < divs.Length(); i++ {
|
|
|
|
syncedKeys[i] = strings.TrimSpace(divs.Eq(i).Text())
|
|
|
|
}
|
|
|
|
|
2020-12-18 18:44:18 +01:00
|
|
|
assert.ElementsMatch(t, u.SSHKeys, syncedKeys, "Unequal number of keys synchronized for user: %s", u.UserName)
|
2018-05-24 06:59:02 +02:00
|
|
|
}
|
|
|
|
}
|
2022-02-11 15:24:58 +01:00
|
|
|
|
|
|
|
func TestLDAPGroupTeamSyncAddMember(t *testing.T) {
|
2024-12-15 12:59:18 +01:00
|
|
|
te := prepareLdapTestEnv(t)
|
|
|
|
if te == nil {
|
2022-02-11 15:24:58 +01:00
|
|
|
return
|
|
|
|
}
|
2024-12-15 12:59:18 +01:00
|
|
|
|
2022-09-02 21:18:23 +02:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2024-12-15 12:59:18 +01:00
|
|
|
te.addAuthSource(t, ldapAuthOptions{
|
|
|
|
groupTeamMap: `{"cn=ship_crew,ou=people,dc=planetexpress,dc=com":{"org26": ["team11"]},"cn=admin_staff,ou=people,dc=planetexpress,dc=com": {"non-existent": ["non-existent"]}}`,
|
|
|
|
groupTeamMapRemoval: "on",
|
|
|
|
})
|
2023-02-08 07:44:42 +01:00
|
|
|
org, err := organization.GetOrgByName(db.DefaultContext, "org26")
|
2022-02-11 15:24:58 +01:00
|
|
|
assert.NoError(t, err)
|
2022-05-20 16:08:52 +02:00
|
|
|
team, err := organization.GetTeam(db.DefaultContext, org.ID, "team11")
|
2022-02-11 15:24:58 +01:00
|
|
|
assert.NoError(t, err)
|
2024-12-15 12:59:18 +01:00
|
|
|
require.NoError(t, auth.SyncExternalUsers(context.Background(), true))
|
|
|
|
for _, gitLDAPUser := range te.gitLDAPUsers {
|
2022-02-11 15:24:58 +01:00
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{
|
|
|
|
Name: gitLDAPUser.UserName,
|
2022-08-16 04:22:25 +02:00
|
|
|
})
|
2023-11-24 04:49:41 +01:00
|
|
|
usersOrgs, err := db.Find[organization.Organization](db.DefaultContext, organization.FindOrgOptions{
|
2022-02-11 15:24:58 +01:00
|
|
|
UserID: user.ID,
|
|
|
|
IncludePrivate: true,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
2022-03-29 08:29:02 +02:00
|
|
|
allOrgTeams, err := organization.GetUserOrgTeams(db.DefaultContext, org.ID, user.ID)
|
2022-02-11 15:24:58 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
if user.Name == "fry" || user.Name == "leela" || user.Name == "bender" {
|
|
|
|
// assert members of LDAP group "cn=ship_crew" are added to mapped teams
|
2023-04-22 23:56:27 +02:00
|
|
|
assert.Len(t, usersOrgs, 1, "User [%s] should be member of one organization", user.Name)
|
|
|
|
assert.Equal(t, "org26", usersOrgs[0].Name, "Membership should be added to the right organization")
|
2022-03-29 08:29:02 +02:00
|
|
|
isMember, err := organization.IsTeamMember(db.DefaultContext, usersOrgs[0].ID, team.ID, user.ID)
|
2022-02-11 15:24:58 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, isMember, "Membership should be added to the right team")
|
2024-11-27 22:12:26 +01:00
|
|
|
err = org_service.RemoveTeamMember(db.DefaultContext, team, user)
|
2022-02-11 15:24:58 +01:00
|
|
|
assert.NoError(t, err)
|
2024-11-27 22:12:26 +01:00
|
|
|
err = org_service.RemoveOrgUser(db.DefaultContext, usersOrgs[0], user)
|
2022-02-11 15:24:58 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
// assert members of LDAP group "cn=admin_staff" keep initial team membership since mapped team does not exist
|
|
|
|
assert.Empty(t, usersOrgs, "User should be member of no organization")
|
2022-03-29 08:29:02 +02:00
|
|
|
isMember, err := organization.IsTeamMember(db.DefaultContext, org.ID, team.ID, user.ID)
|
2022-02-11 15:24:58 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, isMember, "User should no be added to this team")
|
|
|
|
assert.Empty(t, allOrgTeams, "User should not be added to any team")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLDAPGroupTeamSyncRemoveMember(t *testing.T) {
|
2024-12-15 12:59:18 +01:00
|
|
|
te := prepareLdapTestEnv(t)
|
|
|
|
if te == nil {
|
2022-02-11 15:24:58 +01:00
|
|
|
return
|
|
|
|
}
|
2022-09-02 21:18:23 +02:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2024-12-15 12:59:18 +01:00
|
|
|
te.addAuthSource(t, ldapAuthOptions{
|
|
|
|
groupTeamMap: `{"cn=dispatch,ou=people,dc=planetexpress,dc=com": {"org26": ["team11"]}}`,
|
|
|
|
groupTeamMapRemoval: "on",
|
|
|
|
})
|
2023-02-08 07:44:42 +01:00
|
|
|
org, err := organization.GetOrgByName(db.DefaultContext, "org26")
|
2022-02-11 15:24:58 +01:00
|
|
|
assert.NoError(t, err)
|
2022-05-20 16:08:52 +02:00
|
|
|
team, err := organization.GetTeam(db.DefaultContext, org.ID, "team11")
|
2022-02-11 15:24:58 +01:00
|
|
|
assert.NoError(t, err)
|
2024-12-15 12:59:18 +01:00
|
|
|
loginUserWithPassword(t, te.gitLDAPUsers[0].UserName, te.gitLDAPUsers[0].Password)
|
2022-02-11 15:24:58 +01:00
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{
|
2024-12-15 12:59:18 +01:00
|
|
|
Name: te.gitLDAPUsers[0].UserName,
|
2022-08-16 04:22:25 +02:00
|
|
|
})
|
2023-10-03 12:30:41 +02:00
|
|
|
err = organization.AddOrgUser(db.DefaultContext, org.ID, user.ID)
|
2022-02-11 15:24:58 +01:00
|
|
|
assert.NoError(t, err)
|
2024-11-27 22:12:26 +01:00
|
|
|
err = org_service.AddTeamMember(db.DefaultContext, team, user)
|
2022-02-11 15:24:58 +01:00
|
|
|
assert.NoError(t, err)
|
2022-03-29 08:29:02 +02:00
|
|
|
isMember, err := organization.IsOrganizationMember(db.DefaultContext, org.ID, user.ID)
|
2022-02-11 15:24:58 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, isMember, "User should be member of this organization")
|
2022-03-29 08:29:02 +02:00
|
|
|
isMember, err = organization.IsTeamMember(db.DefaultContext, org.ID, team.ID, user.ID)
|
2022-02-11 15:24:58 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, isMember, "User should be member of this team")
|
|
|
|
// assert team member "professor" gets removed from org26 team11
|
2024-12-15 12:59:18 +01:00
|
|
|
loginUserWithPassword(t, te.gitLDAPUsers[0].UserName, te.gitLDAPUsers[0].Password)
|
2022-03-29 08:29:02 +02:00
|
|
|
isMember, err = organization.IsOrganizationMember(db.DefaultContext, org.ID, user.ID)
|
2022-02-11 15:24:58 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, isMember, "User membership should have been removed from organization")
|
2022-03-29 08:29:02 +02:00
|
|
|
isMember, err = organization.IsTeamMember(db.DefaultContext, org.ID, team.ID, user.ID)
|
2022-02-11 15:24:58 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, isMember, "User membership should have been removed from team")
|
|
|
|
}
|
|
|
|
|
2023-02-08 07:44:42 +01:00
|
|
|
func TestLDAPPreventInvalidGroupTeamMap(t *testing.T) {
|
2024-12-15 12:59:18 +01:00
|
|
|
te := prepareLdapTestEnv(t)
|
|
|
|
if te == nil {
|
2022-02-11 15:24:58 +01:00
|
|
|
return
|
|
|
|
}
|
2022-09-02 21:18:23 +02:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2022-02-11 15:24:58 +01:00
|
|
|
|
2023-02-08 07:44:42 +01:00
|
|
|
session := loginUser(t, "user1")
|
2024-10-10 05:48:21 +02:00
|
|
|
csrf := GetUserCSRFToken(t, session)
|
2024-12-15 12:59:18 +01:00
|
|
|
payload := te.buildAuthSourcePayload(csrf, ldapAuthOptions{groupTeamMap: `{"NOT_A_VALID_JSON"["MISSING_DOUBLE_POINT"]}`, groupTeamMapRemoval: "off"})
|
|
|
|
req := NewRequestWithValues(t, "POST", "/-/admin/auths/new", payload)
|
2023-02-08 07:44:42 +01:00
|
|
|
session.MakeRequest(t, req, http.StatusOK) // StatusOK = failed, StatusSeeOther = ok
|
2022-02-11 15:24:58 +01:00
|
|
|
}
|
2024-12-15 12:59:18 +01:00
|
|
|
|
|
|
|
func TestLDAPEmailSignin(t *testing.T) {
|
|
|
|
te := ldapTestEnv{
|
|
|
|
gitLDAPUsers: []ldapUser{
|
|
|
|
{
|
|
|
|
UserName: "u1",
|
|
|
|
Password: "xx",
|
|
|
|
FullName: "user 1",
|
|
|
|
Email: "u1@gitea.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
serverHost: "mock-host",
|
|
|
|
serverPort: "mock-port",
|
|
|
|
}
|
|
|
|
defer test.MockVariableValue(&ldap.MockedSearchEntry, func(source *ldap.Source, name, passwd string, directBind bool) *ldap.SearchResult {
|
|
|
|
var u *ldapUser
|
|
|
|
for _, user := range te.gitLDAPUsers {
|
|
|
|
if user.Email == name && user.Password == passwd {
|
|
|
|
u = &user
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if u == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
result := &ldap.SearchResult{
|
|
|
|
Username: u.UserName,
|
|
|
|
Mail: u.Email,
|
|
|
|
LowerName: strings.ToLower(u.UserName),
|
|
|
|
}
|
|
|
|
nameFields := strings.Split(u.FullName, " ")
|
|
|
|
result.Name = nameFields[0]
|
|
|
|
if len(nameFields) > 1 {
|
|
|
|
result.Surname = nameFields[1]
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
})()
|
|
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
te.addAuthSource(t)
|
|
|
|
|
|
|
|
u := te.gitLDAPUsers[0]
|
|
|
|
|
|
|
|
session := loginUserWithPassword(t, u.Email, u.Password)
|
|
|
|
req := NewRequest(t, "GET", "/user/settings")
|
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
|
|
|
|
|
|
|
assert.Equal(t, u.UserName, htmlDoc.GetInputValueByName("name"))
|
|
|
|
assert.Equal(t, u.FullName, htmlDoc.GetInputValueByName("full_name"))
|
|
|
|
assert.Equal(t, u.Email, htmlDoc.Find("#signed-user-email").Text())
|
|
|
|
}
|