forked from casdoor/casdoor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support simple LDAP server (casdoor#1155)
* feat:Support simple ldap server * fix:fix review problems * fix:fix review problems
- Loading branch information
Showing
6 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,4 @@ origin = | |
staticBaseUrl = "https://cdn.casbin.org" | ||
isDemoMode = false | ||
batchSize = 100 | ||
ldapServerPort = 389 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// Copyright 2022 The Casdoor Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package controllers | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
|
||
"github.com/casdoor/casdoor/conf" | ||
"github.com/casdoor/casdoor/object" | ||
"github.com/forestmgy/ldapserver" | ||
"github.com/lor00x/goldap/message" | ||
) | ||
|
||
func StartLdapServer() { | ||
server := ldapserver.NewServer() | ||
routes := ldapserver.NewRouteMux() | ||
|
||
routes.Bind(handleBind) | ||
routes.Search(handleSearch).Label(" SEARCH****") | ||
|
||
server.Handle(routes) | ||
|
||
go server.ListenAndServe("0.0.0.0:" + conf.GetConfigString("ldapServerPort")) | ||
|
||
// When CTRL+C, SIGINT and SIGTERM signal occurs | ||
// Then stop server gracefully | ||
ch := make(chan os.Signal) | ||
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM) | ||
<-ch | ||
close(ch) | ||
|
||
server.Stop() | ||
} | ||
|
||
func handleBind(w ldapserver.ResponseWriter, m *ldapserver.Message) { | ||
r := m.GetBindRequest() | ||
res := ldapserver.NewBindResponse(ldapserver.LDAPResultSuccess) | ||
|
||
if r.AuthenticationChoice() == "simple" { | ||
bindusername, bindorg, err := object.GetNameAndOrgFromDN(string(r.Name())) | ||
if err != "" { | ||
log.Printf("Bind failed ,ErrMsg=%s", err) | ||
res.SetResultCode(ldapserver.LDAPResultInvalidDNSyntax) | ||
res.SetDiagnosticMessage("bind failed ErrMsg: " + err) | ||
w.Write(res) | ||
return | ||
} | ||
bindpassword := string(r.AuthenticationSimple()) | ||
binduser, err := object.CheckUserPassword(bindorg, bindusername, bindpassword) | ||
if err != "" { | ||
log.Printf("Bind failed User=%s, Pass=%#v, ErrMsg=%s", string(r.Name()), r.Authentication(), err) | ||
res.SetResultCode(ldapserver.LDAPResultInvalidCredentials) | ||
res.SetDiagnosticMessage("invalid credentials ErrMsg: " + err) | ||
w.Write(res) | ||
return | ||
} | ||
if bindorg == "built-in" { | ||
m.Client.IsGlobalAdmin, m.Client.IsOrgAdmin = true, true | ||
} else if binduser.IsAdmin { | ||
m.Client.IsOrgAdmin = true | ||
} | ||
m.Client.IsAuthenticated = true | ||
m.Client.UserName = bindusername | ||
m.Client.OrgName = bindorg | ||
} else { | ||
res.SetResultCode(ldapserver.LDAPResultAuthMethodNotSupported) | ||
res.SetDiagnosticMessage("Authentication method not supported,Please use Simple Authentication") | ||
} | ||
w.Write(res) | ||
} | ||
|
||
func handleSearch(w ldapserver.ResponseWriter, m *ldapserver.Message) { | ||
res := ldapserver.NewSearchResultDoneResponse(ldapserver.LDAPResultSuccess) | ||
if !m.Client.IsAuthenticated { | ||
res.SetResultCode(ldapserver.LDAPResultUnwillingToPerform) | ||
w.Write(res) | ||
return | ||
} | ||
r := m.GetSearchRequest() | ||
if r.FilterString() == "(objectClass=*)" { | ||
w.Write(res) | ||
return | ||
} | ||
name, org, errCode := object.GetUserNameAndOrgFromBaseDnAndFilter(string(r.BaseObject()), r.FilterString()) | ||
if errCode != ldapserver.LDAPResultSuccess { | ||
res.SetResultCode(errCode) | ||
w.Write(res) | ||
return | ||
} | ||
// Handle Stop Signal (server stop / client disconnected / Abandoned request....) | ||
select { | ||
case <-m.Done: | ||
log.Print("Leaving handleSearch...") | ||
return | ||
default: | ||
} | ||
users, errCode := object.GetFilteredUsers(m, name, org) | ||
if errCode != ldapserver.LDAPResultSuccess { | ||
res.SetResultCode(errCode) | ||
w.Write(res) | ||
return | ||
} | ||
for i := 0; i < len(users); i++ { | ||
user := users[i] | ||
dn := fmt.Sprintf("cn=%s,%s", user.DisplayName, string(r.BaseObject())) | ||
e := ldapserver.NewSearchResultEntry(dn) | ||
e.AddAttribute("cn", message.AttributeValue(user.Name)) | ||
e.AddAttribute("uid", message.AttributeValue(user.Name)) | ||
e.AddAttribute("email", message.AttributeValue(user.Email)) | ||
e.AddAttribute("mobile", message.AttributeValue(user.Phone)) | ||
// e.AddAttribute("postalAddress", message.AttributeValue(user.Address[0])) | ||
w.Write(e) | ||
} | ||
w.Write(res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2022 The Casdoor Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package object | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/forestmgy/ldapserver" | ||
) | ||
|
||
func GetNameAndOrgFromDN(DN string) (string, string, string) { | ||
DNValue := strings.Split(DN, ",") | ||
if len(DNValue) == 1 || strings.ToLower(DNValue[0])[0] != 'c' || strings.ToLower(DNValue[1])[0] != 'o' { | ||
return "", "", "please use correct Admin Name format like cn=xxx,ou=xxx,dc=example,dc=com" | ||
} | ||
return DNValue[0][3:], DNValue[1][3:], "" | ||
} | ||
|
||
func GetUserNameAndOrgFromBaseDnAndFilter(baseDN, filter string) (string, string, int) { | ||
if !strings.Contains(baseDN, "ou=") || !strings.Contains(filter, "cn=") { | ||
return "", "", ldapserver.LDAPResultInvalidDNSyntax | ||
} | ||
name := getUserNameFromFilter(filter) | ||
_, org, _ := GetNameAndOrgFromDN(fmt.Sprintf("cn=%s,", name) + baseDN) | ||
errCode := ldapserver.LDAPResultSuccess | ||
return name, org, errCode | ||
} | ||
|
||
func getUserNameFromFilter(filter string) string { | ||
nameIndex := strings.Index(filter, "cn=") | ||
var name string | ||
for i := nameIndex + 3; filter[i] != ')'; i++ { | ||
name = name + string(filter[i]) | ||
} | ||
return name | ||
} | ||
|
||
func GetFilteredUsers(m *ldapserver.Message, name, org string) ([]*User, int) { | ||
var filteredUsers []*User | ||
if name == "*" && m.Client.IsOrgAdmin { // get all users from organization 'org' | ||
if m.Client.OrgName == "built-in" && org == "*" { | ||
filteredUsers = GetGlobalUsers() | ||
return filteredUsers, ldapserver.LDAPResultSuccess | ||
} else if m.Client.OrgName == "built-in" || org == m.Client.OrgName { | ||
filteredUsers = GetUsers(org) | ||
return filteredUsers, ldapserver.LDAPResultSuccess | ||
} else { | ||
return nil, ldapserver.LDAPResultInsufficientAccessRights | ||
} | ||
} else { | ||
hasPermission, err := CheckUserPermission(fmt.Sprintf("%s/%s", m.Client.OrgName, m.Client.UserName), fmt.Sprintf("%s/%s", org, name), org, true) | ||
if !hasPermission { | ||
log.Printf("ErrMsg = %v", err.Error()) | ||
return nil, ldapserver.LDAPResultInsufficientAccessRights | ||
} | ||
user := getUser(org, name) | ||
filteredUsers = append(filteredUsers, user) | ||
return filteredUsers, ldapserver.LDAPResultSuccess | ||
} | ||
} |