From bf58679390b9045054139fd0f43bef9bb972f3e6 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 11 May 2014 18:10:37 +0800 Subject: [PATCH] add support for smtp authentication --- models/login.go | 23 +++++---- modules/auth/authentication.go | 27 ++++++----- routers/admin/auths.go | 83 ++++++++++++++++++++++++--------- templates/admin/auths/edit.tmpl | 60 +++++++++++++++++++++--- templates/admin/auths/new.tmpl | 57 ++++++++++++---------- 5 files changed, 174 insertions(+), 76 deletions(-) diff --git a/models/login.go b/models/login.go index 7313726562..fae2ac7609 100644 --- a/models/login.go +++ b/models/login.go @@ -38,6 +38,7 @@ var LoginTypes = map[int]string{ } var _ core.Conversion = &LDAPConfig{} +var _ core.Conversion = &SMTPConfig{} type LDAPConfig struct { ldap.Ldapsource @@ -55,7 +56,7 @@ func (cfg *LDAPConfig) ToDB() ([]byte, error) { type SMTPConfig struct { Auth string Host string - Post string + Port int TLS bool } @@ -122,16 +123,12 @@ func GetLoginSourceById(id int64) (*LoginSource, error) { return source, nil } -func AddLDAPSource(name string, cfg *LDAPConfig) error { - _, err := orm.Insert(&LoginSource{Type: LT_LDAP, - Name: name, - IsActived: true, - Cfg: cfg, - }) +func AddSource(source *LoginSource) error { + _, err := orm.Insert(source) return err } -func UpdateLDAPSource(source *LoginSource) error { +func UpdateSource(source *LoginSource) error { _, err := orm.AllCols().Id(source.Id).Update(source) return err } @@ -293,7 +290,9 @@ func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) { } var ( - smtpAuths = []string{"plain", "login", ""} + SMTP_PLAIN = "PLAIN" + SMTP_LOGIN = "LOGIN" + SMTPAuths = []string{SMTP_PLAIN, SMTP_LOGIN} ) func SmtpAuth(addr string, a smtp.Auth) error { @@ -324,13 +323,13 @@ func SmtpAuth(addr string, a smtp.Auth) error { // Return the same LoginUserPlain semantic func LoginUserSMTPSource(user *User, name, passwd string, sourceId int64, cfg *SMTPConfig, autoRegister bool) (*User, error) { var auth smtp.Auth - if cfg.Auth == "plain" { + if cfg.Auth == SMTP_PLAIN { auth = smtp.PlainAuth("", name, passwd, cfg.Host) - } else if cfg.Auth == "login" { + } else if cfg.Auth == SMTP_LOGIN { auth = LoginAuth(name, passwd) } - err := SmtpAuth(fmt.Sprintf("%s:%d", cfg.Host, cfg.Post), auth) + err := SmtpAuth(fmt.Sprintf("%s:%d", cfg.Host, cfg.Port), auth) if err != nil { return nil, err } diff --git a/modules/auth/authentication.go b/modules/auth/authentication.go index 1a5fdcb058..89ccc560f9 100644 --- a/modules/auth/authentication.go +++ b/modules/auth/authentication.go @@ -15,17 +15,22 @@ import ( ) type AuthenticationForm struct { - Id int64 `form:"id"` - Type int `form:"type"` - AuthName string `form:"name" binding:"Required;MaxSize(50)"` - Domain string `form:"domain" binding:"Required"` - Host string `form:"host" binding:"Required"` - Port int `form:"port" binding:"Required"` - BaseDN string `form:"base_dn" binding:"Required"` - Attributes string `form:"attributes" binding:"Required"` - Filter string `form:"filter" binding:"Required"` - MsAdSA string `form:"ms_ad_sa" binding:"Required"` - IsActived bool `form:"is_actived"` + Id int64 `form:"id"` + Type int `form:"type"` + AuthName string `form:"name" binding:"Required;MaxSize(50)"` + Domain string `form:"domain"` + Host string `form:"host"` + Port int `form:"port"` + BaseDN string `form:"base_dn"` + Attributes string `form:"attributes"` + Filter string `form:"filter"` + MsAdSA string `form:"ms_ad_sa"` + IsActived bool `form:"is_actived"` + SmtpAuth string `form:"smtpauth"` + SmtpHost string `form:"smtphost"` + SmtpPort int `form:"smtpport"` + SmtpTls bool `form:"smtptls"` + AllowAutoRegister bool `form:"allowautoregister"` } func (f *AuthenticationForm) Name(field string) string { diff --git a/routers/admin/auths.go b/routers/admin/auths.go index 40318c3d24..c45bbaaba0 100644 --- a/routers/admin/auths.go +++ b/routers/admin/auths.go @@ -5,10 +5,11 @@ package admin import ( + "errors" "strings" "github.com/go-martini/martini" - + "github.com/go-xorm/core" "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/auth" "github.com/gogits/gogs/modules/auth/ldap" @@ -21,32 +22,55 @@ func NewAuthSource(ctx *middleware.Context) { ctx.Data["Title"] = "New Authentication" ctx.Data["PageIsAuths"] = true ctx.Data["LoginTypes"] = models.LoginTypes + ctx.Data["SMTPAuths"] = models.SMTPAuths ctx.HTML(200, "admin/auths/new") } func NewAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) { ctx.Data["Title"] = "New Authentication" ctx.Data["PageIsAuths"] = true + ctx.Data["LoginTypes"] = models.LoginTypes + ctx.Data["SMTPAuths"] = models.SMTPAuths if ctx.HasError() { ctx.HTML(200, "admin/auths/new") return } - u := &models.LDAPConfig{ - Ldapsource: ldap.Ldapsource{ - Host: form.Host, - Port: form.Port, - BaseDN: form.BaseDN, - Attributes: form.Attributes, - Filter: form.Filter, - MsAdSAFormat: form.MsAdSA, - Enabled: true, - Name: form.AuthName, - }, + var u core.Conversion + if form.Type == models.LT_LDAP { + u = &models.LDAPConfig{ + Ldapsource: ldap.Ldapsource{ + Host: form.Host, + Port: form.Port, + BaseDN: form.BaseDN, + Attributes: form.Attributes, + Filter: form.Filter, + MsAdSAFormat: form.MsAdSA, + Enabled: true, + Name: form.AuthName, + }, + } + } else if form.Type == models.LT_SMTP { + u = &models.SMTPConfig{ + Auth: form.SmtpAuth, + Host: form.SmtpHost, + Port: form.SmtpPort, + TLS: form.SmtpTls, + } + } else { + panic(errors.New("not allow type")) } - if err := models.AddLDAPSource(form.AuthName, u); err != nil { + var source = &models.LoginSource{ + Type: form.Type, + Name: form.AuthName, + IsActived: true, + AllowAutoRegisted: form.AllowAutoRegister, + Cfg: u, + } + + if err := models.AddSource(source); err != nil { switch err { default: ctx.Handle(500, "admin.auths.NewAuth", err) @@ -63,6 +87,9 @@ func NewAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) { func EditAuthSource(ctx *middleware.Context, params martini.Params) { ctx.Data["Title"] = "Edit Authentication" ctx.Data["PageIsAuths"] = true + ctx.Data["LoginTypes"] = models.LoginTypes + ctx.Data["SMTPAuths"] = models.SMTPAuths + id, err := base.StrTo(params["authid"]).Int64() if err != nil { ctx.Handle(404, "admin.auths.EditAuthSource", err) @@ -74,24 +101,23 @@ func EditAuthSource(ctx *middleware.Context, params martini.Params) { return } ctx.Data["Source"] = u - ctx.Data["LoginTypes"] = models.LoginTypes ctx.HTML(200, "admin/auths/edit") } func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) { ctx.Data["Title"] = "Edit Authentication" ctx.Data["PageIsAuths"] = true + ctx.Data["LoginTypes"] = models.LoginTypes + ctx.Data["SMTPAuths"] = models.SMTPAuths if ctx.HasError() { ctx.HTML(200, "admin/auths/edit") return } - u := models.LoginSource{ - Name: form.AuthName, - IsActived: form.IsActived, - Type: models.LT_LDAP, - Cfg: &models.LDAPConfig{ + var config core.Conversion + if form.Type == models.LT_LDAP { + config = &models.LDAPConfig{ Ldapsource: ldap.Ldapsource{ Host: form.Host, Port: form.Port, @@ -102,10 +128,25 @@ func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) { Enabled: true, Name: form.AuthName, }, - }, + } + } else if form.Type == models.LT_SMTP { + config = &models.SMTPConfig{ + Auth: form.SmtpAuth, + Host: form.SmtpHost, + Port: form.SmtpPort, + TLS: form.SmtpTls, + } } - if err := models.UpdateLDAPSource(&u); err != nil { + u := models.LoginSource{ + Name: form.AuthName, + IsActived: form.IsActived, + Type: form.Type, + AllowAutoRegisted: form.AllowAutoRegister, + Cfg: config, + } + + if err := models.UpdateSource(&u); err != nil { switch err { default: ctx.Handle(500, "admin.auths.EditAuth", err) diff --git a/templates/admin/auths/edit.tmpl b/templates/admin/auths/edit.tmpl index e040ea8f77..56c4db74aa 100644 --- a/templates/admin/auths/edit.tmpl +++ b/templates/admin/auths/edit.tmpl @@ -14,18 +14,16 @@ {{.CsrfTokenHtml}} {{template "base/alert" .}} + {{$type := .Source.Type}}
+
- + {{range $key, $val := .LoginTypes}} + {{if eq $key $type}}{{$val}}{{end}} + {{end}}
-
@@ -33,6 +31,8 @@
+ {{if eq $type 2}} +
@@ -81,7 +81,53 @@
+ {{else}} + {{if eq $type 3}} + +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ {{end}} + {{end}} +
+ +
+ +
+
+
diff --git a/templates/admin/auths/new.tmpl b/templates/admin/auths/new.tmpl index 5c0ccccae8..287c996c7f 100644 --- a/templates/admin/auths/new.tmpl +++ b/templates/admin/auths/new.tmpl @@ -16,104 +16,111 @@
- {{range $key, $val := .LoginTypes}} {{end}}
-
-
+
- +
- +
- +
- +
- +
- +
- +
- +
- +
- - +
+ +
+ +
+