|
| 1 | +package auth |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/tls" |
| 5 | + "fmt" |
| 6 | + "net" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +type LDAPConfig struct { |
| 12 | + Server string `json:"server"` |
| 13 | + Port int `json:"port"` |
| 14 | + BaseDN string `json:"base_dn"` |
| 15 | + BindDN string `json:"bind_dn"` |
| 16 | + BindPass string `json:"bind_pass"` |
| 17 | + Filter string `json:"filter"` |
| 18 | + UseTLS bool `json:"use_tls"` |
| 19 | + Timeout int `json:"timeout"` |
| 20 | +} |
| 21 | + |
| 22 | +type LDAPAuthenticator struct { |
| 23 | + config LDAPConfig |
| 24 | +} |
| 25 | + |
| 26 | +func NewLDAPAuthenticator(config LDAPConfig) *LDAPAuthenticator { |
| 27 | + if config.Timeout == 0 { |
| 28 | + config.Timeout = 10 |
| 29 | + } |
| 30 | + if config.Port == 0 { |
| 31 | + config.Port = 389 |
| 32 | + } |
| 33 | + if config.Filter == "" { |
| 34 | + config.Filter = "(uid=%s)" |
| 35 | + } |
| 36 | + return &LDAPAuthenticator{config: config} |
| 37 | +} |
| 38 | + |
| 39 | +func (a *LDAPAuthenticator) Authenticate(username, password string) (map[string]string, error) { |
| 40 | + addr := fmt.Sprintf("%s:%d", a.config.Server, a.config.Port) |
| 41 | + conn, err := net.DialTimeout("tcp", addr, time.Duration(a.config.Timeout)*time.Second) |
| 42 | + if err != nil { |
| 43 | + return nil, fmt.Errorf("ldap connect failed: %w", err) |
| 44 | + } |
| 45 | + defer conn.Close() |
| 46 | + |
| 47 | + if a.config.UseTLS { |
| 48 | + tlsConn := tls.Client(conn, &tls.Config{InsecureSkipVerify: true}) |
| 49 | + if err := tlsConn.Handshake(); err != nil { |
| 50 | + return nil, fmt.Errorf("ldap tls handshake failed: %w", err) |
| 51 | + } |
| 52 | + conn = tlsConn |
| 53 | + } |
| 54 | + |
| 55 | + ldapMsg := buildBindRequest(a.config.BindDN, a.config.BindPass) |
| 56 | + if _, err := conn.Write(ldapMsg); err != nil { |
| 57 | + return nil, fmt.Errorf("ldap bind write failed: %w", err) |
| 58 | + } |
| 59 | + |
| 60 | + resp := make([]byte, 4096) |
| 61 | + n, err := conn.Read(resp) |
| 62 | + if err != nil { |
| 63 | + return nil, fmt.Errorf("ldap bind read failed: %w", err) |
| 64 | + } |
| 65 | + if n < 2 || resp[1] != 0x00 { |
| 66 | + return nil, fmt.Errorf("ldap bind failed: server returned error code") |
| 67 | + } |
| 68 | + |
| 69 | + userDN := fmt.Sprintf(a.config.Filter, username) |
| 70 | + if !strings.Contains(a.config.Filter, "%s") { |
| 71 | + userDN = a.config.Filter |
| 72 | + } |
| 73 | + |
| 74 | + ldapMsg = buildBindRequest(userDN, password) |
| 75 | + if _, err := conn.Write(ldapMsg); err != nil { |
| 76 | + return nil, fmt.Errorf("ldap user bind failed: %w", err) |
| 77 | + } |
| 78 | + |
| 79 | + n, err = conn.Read(resp) |
| 80 | + if err != nil { |
| 81 | + return nil, fmt.Errorf("ldap user bind read failed: %w", err) |
| 82 | + } |
| 83 | + if n < 2 || resp[1] != 0x00 { |
| 84 | + return nil, fmt.Errorf("invalid ldap credentials") |
| 85 | + } |
| 86 | + |
| 87 | + return map[string]string{ |
| 88 | + "username": username, |
| 89 | + "dn": userDN, |
| 90 | + }, nil |
| 91 | +} |
| 92 | + |
| 93 | +func buildBindRequest(dn, password string) []byte { |
| 94 | + // Minimal LDAP BIND request (simple auth) |
| 95 | + var req []byte |
| 96 | + req = append(req, 0x30) |
| 97 | + body := append([]byte{0x02, 0x01, 0x01}, // version 3 |
| 98 | + append([]byte{0x60}, encodeLength(len(dn)+2)...)...) // bind request tag |
| 99 | + body = append(body, append([]byte{0x04}, encodeLength(len(dn))...)...) |
| 100 | + body = append(body, []byte(dn)...) |
| 101 | + body = append(body, 0x80) |
| 102 | + body = append(body, encodeLength(len(password))...) |
| 103 | + body = append(body, []byte(password)...) |
| 104 | + req = append(req, encodeLength(len(body))...) |
| 105 | + req = append(req, body...) |
| 106 | + return req |
| 107 | +} |
| 108 | + |
| 109 | +func encodeLength(n int) []byte { |
| 110 | + if n < 128 { |
| 111 | + return []byte{byte(n)} |
| 112 | + } |
| 113 | + return []byte{0x81, byte(n)} |
| 114 | +} |
0 commit comments