harbor/auth/ldap/ldap.go

129 lines
3.1 KiB
Go
Raw Normal View History

2016-02-01 12:59:10 +01:00
/*
Copyright (c) 2016 VMware, Inc. 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.
*/
2016-02-26 11:54:14 +01:00
2016-02-01 12:59:10 +01:00
package ldap
import (
"errors"
"fmt"
"os"
"strings"
2016-03-25 11:41:10 +01:00
"github.com/vmware/harbor/utils/log"
2016-03-25 11:17:42 +01:00
"github.com/vmware/harbor/auth"
2016-02-01 12:59:10 +01:00
"github.com/vmware/harbor/dao"
"github.com/vmware/harbor/models"
"github.com/mqu/openldap"
)
2016-02-26 11:35:55 +01:00
// Auth implements Authenticator interface to authenticate against LDAP
type Auth struct{}
2016-02-01 12:59:10 +01:00
const metaChars = "&|!=~*<>()"
2016-02-01 12:59:10 +01:00
2016-02-26 11:35:55 +01:00
// Authenticate checks user's credential agains LDAP based on basedn template and LDAP URL,
// if the check is successful a dummy record will be insert into DB, such that this user can
// be associated to other entities in the system.
func (l *Auth) Authenticate(m models.AuthModel) (*models.User, error) {
2016-02-01 12:59:10 +01:00
ldapURL := os.Getenv("LDAP_URL")
if ldapURL == "" {
2016-02-01 12:59:10 +01:00
return nil, errors.New("Can not get any available LDAP_URL.")
}
2016-03-25 11:17:42 +01:00
log.Debug("ldapURL:", ldapURL)
2016-02-01 12:59:10 +01:00
p := m.Principal
for _, c := range metaChars {
2016-02-01 12:59:10 +01:00
if strings.ContainsRune(p, c) {
return nil, fmt.Errorf("the principal contains meta char: %q", c)
2016-02-01 12:59:10 +01:00
}
}
ldap, err := openldap.Initialize(ldapURL)
2016-02-01 12:59:10 +01:00
if err != nil {
return nil, err
}
ldap.SetOption(openldap.LDAP_OPT_PROTOCOL_VERSION, openldap.LDAP_VERSION3)
ldapBaseDn := os.Getenv("LDAP_BASE_DN")
if ldapBaseDn == "" {
return nil, errors.New("Can not get any available LDAP_BASE_DN.")
}
baseDn := fmt.Sprintf(ldapBaseDn, m.Principal)
2016-03-25 11:17:42 +01:00
log.Debug("baseDn:", baseDn)
2016-02-01 12:59:10 +01:00
err = ldap.Bind(baseDn, m.Password)
2016-02-01 12:59:10 +01:00
if err != nil {
return nil, err
}
defer ldap.Close()
2016-02-01 12:59:10 +01:00
scope := openldap.LDAP_SCOPE_SUBTREE // LDAP_SCOPE_BASE, LDAP_SCOPE_ONELEVEL, LDAP_SCOPE_SUBTREE
filter := "objectClass=*"
2016-04-25 13:00:36 +02:00
attributes := []string{"mail"}
2016-02-01 12:59:10 +01:00
result, err := ldap.SearchAll(baseDn, scope, filter, attributes)
if err != nil {
return nil, err
}
u := models.User{}
2016-04-25 13:00:36 +02:00
if len(result.Entries()) == 1 {
en := result.Entries()[0]
for _, attr := range en.Attributes() {
val := attr.Values()[0]
if attr.Name() == "mail" {
u.Email = val
}
2016-02-01 12:59:10 +01:00
}
}
2016-04-25 13:00:36 +02:00
u.Username = m.Principal
log.Debug("username:", u.Username, ",email:", u.Email)
2016-02-01 12:59:10 +01:00
exist, err := dao.UserExists(u, "username")
if err != nil {
return nil, err
}
if exist {
currentUser, err := dao.GetUser(u)
if err != nil {
return nil, err
}
2016-02-26 03:15:01 +01:00
u.UserID = currentUser.UserID
2016-02-01 12:59:10 +01:00
} else {
2016-04-25 13:00:36 +02:00
u.Realname = m.Principal
2016-02-01 12:59:10 +01:00
u.Password = "12345678AbC"
u.Comment = "registered from LDAP."
2016-06-01 11:51:06 +02:00
if u.Email == "" {
u.Email = u.Username + "@vmware.com"
}
userID, err := dao.Register(u)
2016-02-01 12:59:10 +01:00
if err != nil {
return nil, err
}
2016-02-26 03:15:01 +01:00
u.UserID = int(userID)
2016-02-01 12:59:10 +01:00
}
return &u, nil
}
func init() {
auth.Register("ldap_auth", &Auth{})
2016-02-01 12:59:10 +01:00
}