Browse Source

Run gofmt

pull/3954/head
Skrupellos 8 years ago
parent
commit
c0a38f2e01
  1. 42
      modules/auth/ldap/ldap.go

42
modules/auth/ldap/ldap.go

@ -8,11 +8,11 @@ package ldap
import ( import (
"crypto/tls" "crypto/tls"
"errors"
"fmt" "fmt"
"strings"
"net" "net"
"net/url" "net/url"
"errors" "strings"
"gopkg.in/ldap.v2" "gopkg.in/ldap.v2"
@ -21,7 +21,6 @@ import (
type SecurityProtocol int type SecurityProtocol int
// Basic LDAP authentication service // Basic LDAP authentication service
type Source struct { type Source struct {
Name string // canonical name (ie. corporate.ad) Name string // canonical name (ie. corporate.ad)
@ -109,29 +108,29 @@ func (ls *Source) findUserDN(l *ldap.Conn, name string) (string, bool) {
func dial(ls *Source) (*ldap.Conn, error) { func dial(ls *Source) (*ldap.Conn, error) {
log.Trace("Dialing %s (skip cert verification: %v, start TLS: %v)", ls.URL, ls.SkipVerify, ls.StartTLS) log.Trace("Dialing %s (skip cert verification: %v, start TLS: %v)", ls.URL, ls.SkipVerify, ls.StartTLS)
//// URL Parsing //// URL Parsing
ldapUrl := ls.URL ldapUrl := ls.URL
ldapiHost := "" ldapiHost := ""
// Fix ldapi URLs (1/2): ~ by removing and saving the host part for later. // Fix ldapi URLs (1/2): ~ by removing and saving the host part for later.
if strings.HasPrefix(ldapUrl, "ldapi://") { if strings.HasPrefix(ldapUrl, "ldapi://") {
x := strings.IndexAny(ldapUrl[8:], "/?#") x := strings.IndexAny(ldapUrl[8:], "/?#")
if x >= 0 { if x >= 0 {
ldapiHost = ldapUrl[8:8+x] ldapiHost = ldapUrl[8 : 8+x]
ldapUrl = "ldapi://" + ldapUrl[8+x:] ldapUrl = "ldapi://" + ldapUrl[8+x:]
} else { } else {
ldapiHost = ldapUrl[8:] ldapiHost = ldapUrl[8:]
ldapUrl = "ldapi://" ldapUrl = "ldapi://"
} }
} }
// Parse the URL // Parse the URL
u, err := url.Parse(ldapUrl) u, err := url.Parse(ldapUrl)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// Fix ldapi URLs (2/2): ~ by injecting the saved and decoded host part into the parsed URL struct. // Fix ldapi URLs (2/2): ~ by injecting the saved and decoded host part into the parsed URL struct.
if ldapiHost != "" { if ldapiHost != "" {
u.Host, err = url.QueryUnescape(ldapiHost) u.Host, err = url.QueryUnescape(ldapiHost)
@ -139,12 +138,11 @@ func dial(ls *Source) (*ldap.Conn, error) {
return nil, fmt.Errorf("Unescape hostpart of ldapi URL: %v", err) return nil, fmt.Errorf("Unescape hostpart of ldapi URL: %v", err)
} }
} }
if u.User != nil || u.Path != "" || u.Fragment != "" || u.RawQuery != "" || u.Opaque != "" { if u.User != nil || u.Path != "" || u.Fragment != "" || u.RawQuery != "" || u.Opaque != "" {
return nil, errors.New("LDAP URLs (for now) do not support pathes, fragments, querries or opaque form") return nil, errors.New("LDAP URLs (for now) do not support pathes, fragments, querries or opaque form")
} }
//// Dial //// Dial
// ldapI // ldapI
if u.Scheme == "ldapi" { if u.Scheme == "ldapi" {
@ -152,56 +150,56 @@ func dial(ls *Source) (*ldap.Conn, error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("Dial: %v", err) return nil, fmt.Errorf("Dial: %v", err)
} }
return conn, nil return conn, nil
} }
// Common stuff dor ldap / ldapS // Common stuff dor ldap / ldapS
host, port, err := net.SplitHostPort(u.Host) host, port, err := net.SplitHostPort(u.Host)
if err != nil { if err != nil {
return nil, fmt.Errorf("Failed splitting adress in host and port part: %v", err) return nil, fmt.Errorf("Failed splitting adress in host and port part: %v", err)
} }
tlsCfg := &tls.Config{ tlsCfg := &tls.Config{
ServerName: host, ServerName: host,
InsecureSkipVerify: ls.SkipVerify, InsecureSkipVerify: ls.SkipVerify,
} }
// ldapS // ldapS
if u.Scheme == "ldaps" { if u.Scheme == "ldaps" {
if port == "" { if port == "" {
port = "636" port = "636"
} }
conn, err := ldap.DialTLS("tcp", fmt.Sprintf("%s:%d", host, port), tlsCfg) conn, err := ldap.DialTLS("tcp", fmt.Sprintf("%s:%d", host, port), tlsCfg)
if err != nil { if err != nil {
return nil, fmt.Errorf("DialTLS: %v", err) return nil, fmt.Errorf("DialTLS: %v", err)
} }
return conn, nil return conn, nil
} }
// ldap // ldap
if u.Scheme == "ldap" { if u.Scheme == "ldap" {
if port == "" { if port == "" {
port = "389" port = "389"
} }
conn, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", host, port)) conn, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", host, port))
if err != nil { if err != nil {
return nil, fmt.Errorf("Dial: %v", err) return nil, fmt.Errorf("Dial: %v", err)
} }
if ls.StartTLS == ls.StartTLS { if ls.StartTLS == ls.StartTLS {
if err = conn.StartTLS(tlsCfg); err != nil { if err = conn.StartTLS(tlsCfg); err != nil {
conn.Close() conn.Close()
return nil, fmt.Errorf("StartTLS: %v", err) return nil, fmt.Errorf("StartTLS: %v", err)
} }
} }
return conn, nil return conn, nil
} }
return nil, errors.New("The URL dos not has a valid LDAP scheme ('ldap://', 'ldaps://' or 'ldapi://'") return nil, errors.New("The URL dos not has a valid LDAP scheme ('ldap://', 'ldaps://' or 'ldapi://'")
} }

Loading…
Cancel
Save