]> source.dussan.org Git - gitea.git/commitdiff
Make gmail auth work
authorUnknown <joe2010xtmf@163.com>
Thu, 15 May 2014 18:46:04 +0000 (14:46 -0400)
committerUnknown <joe2010xtmf@163.com>
Thu, 15 May 2014 18:46:04 +0000 (14:46 -0400)
models/login.go
models/user.go
modules/mailer/mailer.go
routers/admin/auths.go
templates/admin/auths.tmpl
templates/admin/auths/edit.tmpl
templates/admin/auths/new.tmpl
templates/admin/users/edit.tmpl

index aa82eb0036c17bf2b2a656b9859b504f979501d0..005a1f5b766eaf59960d99deb9fdcde7641bcc49 100644 (file)
@@ -5,6 +5,7 @@
 package models
 
 import (
+       "crypto/tls"
        "encoding/json"
        "errors"
        "fmt"
@@ -133,7 +134,7 @@ func AddSource(source *LoginSource) error {
 }
 
 func UpdateSource(source *LoginSource) error {
-       _, err := orm.AllCols().Id(source.Id).Update(source)
+       _, err := orm.Id(source.Id).AllCols().Update(source)
        return err
 }
 
@@ -197,7 +198,7 @@ func LoginUser(uname, passwd string) (*User, error) {
                                        if err == nil {
                                                return u, nil
                                        } else {
-                                               log.Warn("try ldap login", source.Name, "by", uname, "error:", err)
+                                               log.Warn("Fail to login(%s) by LDAP(%s): %v", uname, source.Name, err)
                                        }
                                } else if source.Type == LT_SMTP {
                                        u, err := LoginUserSMTPSource(nil, uname, passwd,
@@ -205,7 +206,7 @@ func LoginUser(uname, passwd string) (*User, error) {
                                        if err == nil {
                                                return u, nil
                                        } else {
-                                               log.Warn("try smtp login", source.Name, "by", uname, "error:", err)
+                                               log.Warn("Fail to login(%s) by SMTP(%s): %v", uname, source.Name, err)
                                        }
                                }
                        }
@@ -217,12 +218,9 @@ func LoginUser(uname, passwd string) (*User, error) {
                hasSource, err := orm.Id(u.LoginSource).Get(&source)
                if err != nil {
                        return nil, err
-               }
-               if !hasSource {
+               } else if !hasSource {
                        return nil, ErrLoginSourceNotExist
-               }
-
-               if !source.IsActived {
+               } else if !source.IsActived {
                        return nil, ErrLoginSourceNotActived
                }
 
@@ -296,20 +294,25 @@ var (
        SMTPAuths  = []string{SMTP_PLAIN, SMTP_LOGIN}
 )
 
-func SmtpAuth(addr string, a smtp.Auth, tls bool) error {
-       c, err := smtp.Dial(addr)
+func SmtpAuth(host string, port int, a smtp.Auth, useTls bool) error {
+       c, err := smtp.Dial(fmt.Sprintf("%s:%d", host, port))
        if err != nil {
                return err
        }
        defer c.Close()
 
-       if tls {
+       if err = c.Hello("gogs"); err != nil {
+               return err
+       }
+
+       if useTls {
                if ok, _ := c.Extension("STARTTLS"); ok {
-                       if err = c.StartTLS(nil); err != nil {
+                       config := &tls.Config{ServerName: host}
+                       if err = c.StartTLS(config); err != nil {
                                return err
                        }
                } else {
-                       return errors.New("smtp server unsupported tls")
+                       return errors.New("SMTP server unsupported TLS")
                }
        }
 
@@ -333,11 +336,13 @@ func LoginUserSMTPSource(user *User, name, passwd string, sourceId int64, cfg *S
        } else if cfg.Auth == SMTP_LOGIN {
                auth = LoginAuth(name, passwd)
        } else {
-               return nil, errors.New("Unsupported smtp auth type")
+               return nil, errors.New("Unsupported SMTP auth type")
        }
 
-       err := SmtpAuth(fmt.Sprintf("%s:%d", cfg.Host, cfg.Port), auth, cfg.TLS)
-       if err != nil {
+       if err := SmtpAuth(cfg.Host, cfg.Port, auth, cfg.TLS); err != nil {
+               if strings.Contains(err.Error(), "Username and Password not accepted") {
+                       return nil, ErrUserNotExist
+               }
                return nil, err
        }
 
index c5c474ee6f2ebee8f3e3390eaf489416a780cc8c..dd0498501f924d38127a4fc68b72c98b74698990 100644 (file)
@@ -34,7 +34,7 @@ var (
        ErrUserNameIllegal       = errors.New("User name contains illegal characters")
        ErrLoginSourceNotExist   = errors.New("Login source does not exist")
        ErrLoginSourceNotActived = errors.New("Login source is not actived")
-       ErrUnsupportedLoginType  = errors.New("Login source is unknow")
+       ErrUnsupportedLoginType  = errors.New("Login source is unknown")
 )
 
 // User represents the object of individual and member of organization.
index 63861d870ec901997c23815c460b7932e931ef87..a293beb15bb2be7dccb9830e69598f3a3378fa81 100644 (file)
@@ -72,16 +72,14 @@ func Send(msg *Message) (int, error) {
        // get message body
        content := msg.Content()
 
-       auth := smtp.PlainAuth("", base.MailService.User, base.MailService.Passwd, host[0])
-
        if len(msg.To) == 0 {
                return 0, fmt.Errorf("empty receive emails")
-       }
-
-       if len(msg.Body) == 0 {
+       } else if len(msg.Body) == 0 {
                return 0, fmt.Errorf("empty email body")
        }
 
+       auth := smtp.PlainAuth("", base.MailService.User, base.MailService.Passwd, host[0])
+
        if msg.Massive {
                // send mail to multiple emails one by one
                num := 0
index bf4bade17bb0c7e5fb12900b2b4981406d61a267..c4702afc81e3d1c734a0957a23e0e5c44cb43ada 100644 (file)
@@ -158,7 +158,7 @@ func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
        }
 
        log.Trace("%s Authentication changed by admin(%s): %s", ctx.Req.RequestURI,
-               ctx.User.LowerName, strings.ToLower(form.AuthName))
+               ctx.User.LowerName, form.AuthName)
 
        ctx.Redirect("/admin/auths")
 }
index 87baeafd6262641b526428e6d7cf8eb0e8d5fab4..a0f7ba979081dd5398531c1839b768ed5ffa3b23 100644 (file)
@@ -19,7 +19,7 @@
                             <th>Actived</th>
                             <th>Updated</th>
                             <th>Created</th>
-                            <th>Operation</th>
+                            <th>Edit</th>
                         </tr>
                     </thead>
                     <tbody>
index f2ba68fd277f971540c5004b377f970500277f1a..5747c1ab516bdb899b5466cadb194ddcdf2c3d8f 100644 (file)
                             <input name="smtpport" class="form-control" placeholder="Type port number" value="{{.Source.SMTP.Port}}">
                         </div>
                     </div>
-
-                    <!-- <div class="form-group {{if .Err_TLS}}has-error has-feedback{{end}}">
-                        <label class="col-md-3 control-label">TLS: </label>
-                        <div class="col-md-7">
-                            <input name="tls" type="checkbox" class="form-control" {{if .Source.SMTP.TLS}}checked{{end}}>
-                        </div>
-                    </div> -->
                     {{end}}
 
                     <div class="form-group">
+                        {{if eq $type 3}}
+                        <div class="col-md-offset-3 col-md-7">
+                            <div class="checkbox">
+                                <label>
+                                <input name="tls" type="checkbox" class="form-control" {{if .Source.SMTP.TLS}}checked{{end}}>
+                                    <strong>Enable TLS Encryption</strong>
+                                </label>
+                            </div>
+                        </div>
+                        {{end}}
+
                         <div class="col-md-offset-3 col-md-7">
                             <div class="checkbox">
                                 <label>
index f9d9892f618ba17d86574b598af342af94b02f9d..6045f1c532e40149f3ede851de55f8646ccfaff6 100644 (file)
                             </div>
                         </div>
 
-                        <!-- <div class="form-group">
+                        <div class="form-group">
                             <div class="col-md-offset-3 col-md-7">
                                 <div class="checkbox">
                                     <label>
                                         <input name="tls" type="checkbox" {{if .tls}}checked{{end}}>
-                                        <strong>Enable Register Confirmation</strong>
+                                        <strong>Enable TLS Encryption</strong>
                                     </label>
                                 </div>
                             </div>
-                        </div> -->
+                        </div>
                     </div>
 
                     <div class="form-group">
             </div>
         </div>
 
+        <div class="panel panel-info">
+            <div class="panel-heading">
+                Tips
+            </div>
+
+            <div class="panel-body">
+                <h5>GMail Setting:</h5>
+                <p>Host: smtp.gmail.com, Post: 587, Enable TLS Encryption: true</p>
+            </div>
+        </div>
        </div>
 </div>
 <script>
index 4995856768337a6346c1bf1cb8dc3a11e4c70d39..b1fffb69c329b3470b36caaed819b3568e76e6ed 100644 (file)
@@ -29,7 +29,7 @@
                                        <div class="form-group">
                                            <label class="col-md-3 control-label">Auth Login Name: </label>
                                            <div class="col-md-7">
-                                                       <input name="loginname" class="form-control" placeholder="Type auth login's username" value="{{.loginname}}">
+                                                       <input name="loginname" class="form-control" placeholder="Type auth login's username" value="{{.User.LoginName}}">
                                                </div>
                                        </div>