Skip to content

Commit

Permalink
Merge pull request #233 from phachon/feature/v0.2
Browse files Browse the repository at this point in the history
Feature/v0.2
  • Loading branch information
phachon authored Aug 6, 2020
2 parents 0b7bf57 + 08a021a commit 470c4a7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
41 changes: 24 additions & 17 deletions app/services/auth_login_ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,28 @@ import (
"github.com/go-ldap/ldap/v3"
)

const (
LdapDefaultAccountPattern = "(&(objectClass=User)(userPrincipalName=%s))"
LdapDefaultAttributeGivenName = "displayName"
)

var (
LdapUserSearchNotFoundErr = errors.New("用户不存在或密码错误")
)

type AuthLoginConfig struct {
BaseDn string `json:"basedn"`
BindUsername string `json:"bind_username"`
BindPassword string `json:"bind_password"`
AccountPattern string `json:"account_pattern"`
GivenNameKey string `json:"given_name_key"`
EmailKey string `json:"email_key"`
MobileKey string `json:"mobile_key"`
PhoneKey string `json:"phone_key"`
DepartmentKey string `json:"department_key"`
PositionKey string `json:"position_key"`
LocationKey string `json:"location_key"`
ImKey string `json:"im_key"`
BaseDn string `json:"basedn"`
BindUsername string `json:"bind_username"`
BindPassword string `json:"bind_password"`
AccountPattern string `json:"account_pattern"`
GivenNameKey string `json:"given_name_key"`
EmailKey string `json:"email_key"`
MobileKey string `json:"mobile_key"`
PhoneKey string `json:"phone_key"`
DepartmentKey string `json:"department_key"`
PositionKey string `json:"position_key"`
LocationKey string `json:"location_key"`
ImKey string `json:"im_key"`
}

// AuthLoginLdapService ldap auth login
Expand All @@ -49,6 +54,12 @@ func (al *AuthLoginLdapService) InitConf(url string, conf string) error {
return err
}
al.config = authLoginConfig
if al.config.AccountPattern == "" {
al.config.AccountPattern = LdapDefaultAccountPattern
}
if al.config.GivenNameKey == "" {
al.config.GivenNameKey = LdapDefaultAttributeGivenName
}
return nil
}

Expand Down Expand Up @@ -82,18 +93,14 @@ func (al *AuthLoginLdapService) AuthLogin(username string, password string) (*Au
}

// 搜索下用户信息
accountPattern := "(&(objectClass=User)(userPrincipalName=%s))"
if al.config.AccountPattern != "" {
accountPattern = al.config.AccountPattern
}
searchRequest := ldap.NewSearchRequest(
al.config.BaseDn,
ldap.ScopeWholeSubtree,
ldap.NeverDerefAliases,
0,
0,
false,
fmt.Sprintf(accountPattern, username),
fmt.Sprintf(al.config.AccountPattern, username),
al.GetAttributes(),
nil,
)
Expand Down
16 changes: 8 additions & 8 deletions views/system/auth/doc.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ <h4><i class="fa fa-address-card-o"></i> 登录认证方式配置文档</h4>
"bind_username": "readonly", // ldap bind dn; 用来获取查询权限的 bind 用户;非必填可以为空
"bind_password": "password", // ldap bind dn password; bind 用户密码;非必填可以为空
"account_pattern": "(&(objectClass=User)(userPrincipalName=%s))" // ldap search pattern; 非必填可以为空,默认值为(&(objectClass=User)(userPrincipalName=%s))
"given_name_key": "displayName", // ldap 查询用户名对应的 key,必填
"email_key": "mail", // ldap 查询邮箱对应的 key, 没有可为空
"mobile_key": "mobile", // ldap 查询手机号对应的 key,没有可为空
"phone_key": "telephoneNumber", // ldap 查询电话对应的 key,没有可为空
"department_key": "department", // ldap 查询部门对应的 key,没有可为空
"position_key": "Position", // ldap 查询职位对应的 key,没有可为空
"location_key": "physicalDeliveryOfficeName", // ldap 查询位置对应的 key,没有可为空
"im_key": "im" // ldap 查询 im 信息对应的 key,没有可为空
"given_name_key": "displayName", // ldap 查询用户名对应的 key,非必填可以为空,默认为 displayName
"email_key": "mail", // ldap 查询邮箱对应的 key, 非必填可以为空
"mobile_key": "mobile", // ldap 查询手机号对应的 key,非必填可以为空
"phone_key": "telephoneNumber", // ldap 查询电话对应的 key,非必填可以为空
"department_key": "department", // ldap 查询部门对应的 key,非必填可以为空
"position_key": "Position", // ldap 查询职位对应的 key,非必填可以为空
"location_key": "physicalDeliveryOfficeName", // ldap 查询位置对应的 key,非必填可以为空
"im_key": "im" // ldap 查询 im 信息对应的 key,非必填可以为空
}
</pre>
示例<code>{"basedn":"dc=umich,dc=edu","bind_username":"readonly","bind_password":"password","given_name_key":"displayName","email_key":"mail","mobile_key":"mobile","phone_key":"telephoneNumber","department_key":"department","position_key":"Position","location_key":"physicalDeliveryOfficeName","im_key":"im"}</code>
Expand Down

0 comments on commit 470c4a7

Please sign in to comment.