github.com/infraboard/keyauth@v0.8.1/apps/provider/auth/ldap/config.go (about)

     1  package ldap
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  // NewDefaultConfig represents the default LDAP config.
     9  func NewDefaultConfig() *Config {
    10  	return &Config{
    11  		MailAttribute:        "mail",
    12  		DisplayNameAttribute: "displayName",
    13  		GroupNameAttribute:   "cn",
    14  		UsernameAttribute:    "uid",
    15  		UsersFilter:          "(uid={input})",
    16  		GroupsFilter:         "(|(member={dn})(uid={username})(uid={input}))",
    17  	}
    18  }
    19  
    20  // Config represents the configuration related to LDAP server.
    21  type Config struct {
    22  	URL                  string `bson:"url" json:"url"`
    23  	SkipVerify           bool   `bson:"skip_verify" json:"skip_verify"`
    24  	BaseDN               string `bson:"base_dn" json:"base_dn"`
    25  	AdditionalUsersDN    string `bson:"additional_users_dn" json:"additional_users_dn"`
    26  	UsersFilter          string `bson:"users_filter" json:"users_filter"`
    27  	AdditionalGroupsDN   string `bson:"additional_groups_dn" json:"additional_groups_dn"`
    28  	GroupsFilter         string `bson:"groups_filter" json:"groups_filter"`
    29  	GroupNameAttribute   string `bson:"group_name_attribute" json:"group_name_attribute"`
    30  	UsernameAttribute    string `bson:"username_attribute" json:"username_attribute"`
    31  	MailAttribute        string `bson:"mail_attribute" json:"mail_attribute"`
    32  	DisplayNameAttribute string `bson:"display_name_attribute" json:"display_name_attribute"`
    33  	User                 string `bson:"user" json:"user"`
    34  	Password             string `bson:"password" json:"password"`
    35  }
    36  
    37  // GetBaseDNFromUser 从用户中获取BaseDN
    38  func (c *Config) GetBaseDNFromUser() string {
    39  	baseDN := []string{}
    40  	for _, item := range strings.Split(c.User, ",") {
    41  		if !strings.HasPrefix(item, "cn=") {
    42  			baseDN = append(baseDN, item)
    43  		}
    44  	}
    45  
    46  	return strings.Join(baseDN, ",")
    47  }
    48  
    49  // Validate todo
    50  func (c *Config) Validate() error {
    51  	if c.URL == "" {
    52  		return fmt.Errorf("url required")
    53  	}
    54  
    55  	if c.User == "" || c.Password == "" {
    56  		return fmt.Errorf("user and password required")
    57  	}
    58  
    59  	return nil
    60  }
    61  
    62  // Desensitize todo
    63  func (c *Config) Desensitize() {
    64  	c.Password = ""
    65  }