smtp login bug fixed

This commit is contained in:
Lunny Xiao 2014-05-11 20:04:28 +08:00
parent c5dbc24ca4
commit d2231bb54c

View file

@ -148,10 +148,8 @@ func DelLoginSource(source *LoginSource) error {
// login a user // login a user
func LoginUser(uname, passwd string) (*User, error) { func LoginUser(uname, passwd string) (*User, error) {
var u *User var u *User
var emailLogin bool
if strings.Contains(uname, "@") { if strings.Contains(uname, "@") {
u = &User{Email: uname} u = &User{Email: uname}
emailLogin = true
} else { } else {
u = &User{LowerName: strings.ToLower(uname)} u = &User{LowerName: strings.ToLower(uname)}
} }
@ -161,15 +159,11 @@ func LoginUser(uname, passwd string) (*User, error) {
return nil, err return nil, err
} }
// if email login, then we cannot auto register
if emailLogin {
if !has {
return nil, ErrUserNotExist
}
}
if u.LoginType == LT_NOTYPE { if u.LoginType == LT_NOTYPE {
if has {
u.LoginType = LT_PLAIN u.LoginType = LT_PLAIN
} }
}
// for plain login, user must have existed. // for plain login, user must have existed.
if u.LoginType == LT_PLAIN { if u.LoginType == LT_PLAIN {
@ -194,13 +188,13 @@ func LoginUser(uname, passwd string) (*User, error) {
for _, source := range sources { for _, source := range sources {
if source.Type == LT_LDAP { if source.Type == LT_LDAP {
u, err := LoginUserLdapSource(nil, u.LoginName, passwd, u, err := LoginUserLdapSource(nil, uname, passwd,
source.Id, source.Cfg.(*LDAPConfig), true) source.Id, source.Cfg.(*LDAPConfig), true)
if err == nil { if err == nil {
return u, err return u, err
} }
} else if source.Type == LT_SMTP { } else if source.Type == LT_SMTP {
u, err := LoginUserSMTPSource(nil, u.LoginName, passwd, u, err := LoginUserSMTPSource(nil, uname, passwd,
source.Id, source.Cfg.(*SMTPConfig), true) source.Id, source.Cfg.(*SMTPConfig), true)
if err == nil { if err == nil {
@ -295,17 +289,21 @@ var (
SMTPAuths = []string{SMTP_PLAIN, SMTP_LOGIN} SMTPAuths = []string{SMTP_PLAIN, SMTP_LOGIN}
) )
func SmtpAuth(addr string, a smtp.Auth) error { func SmtpAuth(addr string, a smtp.Auth, tls bool) error {
c, err := smtp.Dial(addr) c, err := smtp.Dial(addr)
if err != nil { if err != nil {
return err return err
} }
defer c.Close() defer c.Close()
if tls {
if ok, _ := c.Extension("STARTTLS"); ok { if ok, _ := c.Extension("STARTTLS"); ok {
if err = c.StartTLS(nil); err != nil { if err = c.StartTLS(nil); err != nil {
return err return err
} }
} else {
return errors.New("smtp server unsupported tls")
}
} }
if ok, _ := c.Extension("AUTH"); ok { if ok, _ := c.Extension("AUTH"); ok {
@ -327,9 +325,11 @@ func LoginUserSMTPSource(user *User, name, passwd string, sourceId int64, cfg *S
auth = smtp.PlainAuth("", name, passwd, cfg.Host) auth = smtp.PlainAuth("", name, passwd, cfg.Host)
} else if cfg.Auth == SMTP_LOGIN { } else if cfg.Auth == SMTP_LOGIN {
auth = LoginAuth(name, passwd) auth = LoginAuth(name, passwd)
} else {
return nil, errors.New("Unsupported smtp auth type")
} }
err := SmtpAuth(fmt.Sprintf("%s:%d", cfg.Host, cfg.Port), auth) err := SmtpAuth(fmt.Sprintf("%s:%d", cfg.Host, cfg.Port), auth, cfg.TLS)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -338,10 +338,15 @@ func LoginUserSMTPSource(user *User, name, passwd string, sourceId int64, cfg *S
return user, nil return user, nil
} }
var loginName = name
idx := strings.Index(name, "@")
if idx > -1 {
loginName = name[:idx]
}
// fake a local user creation // fake a local user creation
user = &User{ user = &User{
LowerName: strings.ToLower(name), LowerName: strings.ToLower(loginName),
Name: strings.ToLower(name), Name: strings.ToLower(loginName),
LoginType: LT_SMTP, LoginType: LT_SMTP,
LoginSource: sourceId, LoginSource: sourceId,
LoginName: name, LoginName: name,