add support for smtp authentication

This commit is contained in:
Lunny Xiao 2014-05-11 18:10:37 +08:00
parent cdc87623dc
commit bf58679390
5 changed files with 174 additions and 76 deletions

View File

@ -38,6 +38,7 @@ var LoginTypes = map[int]string{
} }
var _ core.Conversion = &LDAPConfig{} var _ core.Conversion = &LDAPConfig{}
var _ core.Conversion = &SMTPConfig{}
type LDAPConfig struct { type LDAPConfig struct {
ldap.Ldapsource ldap.Ldapsource
@ -55,7 +56,7 @@ func (cfg *LDAPConfig) ToDB() ([]byte, error) {
type SMTPConfig struct { type SMTPConfig struct {
Auth string Auth string
Host string Host string
Post string Port int
TLS bool TLS bool
} }
@ -122,16 +123,12 @@ func GetLoginSourceById(id int64) (*LoginSource, error) {
return source, nil return source, nil
} }
func AddLDAPSource(name string, cfg *LDAPConfig) error { func AddSource(source *LoginSource) error {
_, err := orm.Insert(&LoginSource{Type: LT_LDAP, _, err := orm.Insert(source)
Name: name,
IsActived: true,
Cfg: cfg,
})
return err return err
} }
func UpdateLDAPSource(source *LoginSource) error { func UpdateSource(source *LoginSource) error {
_, err := orm.AllCols().Id(source.Id).Update(source) _, err := orm.AllCols().Id(source.Id).Update(source)
return err return err
} }
@ -293,7 +290,9 @@ func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
} }
var ( 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 { 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 // Return the same LoginUserPlain semantic
func LoginUserSMTPSource(user *User, name, passwd string, sourceId int64, cfg *SMTPConfig, autoRegister bool) (*User, error) { func LoginUserSMTPSource(user *User, name, passwd string, sourceId int64, cfg *SMTPConfig, autoRegister bool) (*User, error) {
var auth smtp.Auth var auth smtp.Auth
if cfg.Auth == "plain" { if cfg.Auth == SMTP_PLAIN {
auth = smtp.PlainAuth("", name, passwd, cfg.Host) auth = smtp.PlainAuth("", name, passwd, cfg.Host)
} else if cfg.Auth == "login" { } else if cfg.Auth == SMTP_LOGIN {
auth = LoginAuth(name, passwd) 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 { if err != nil {
return nil, err return nil, err
} }

View File

@ -15,17 +15,22 @@ import (
) )
type AuthenticationForm struct { type AuthenticationForm struct {
Id int64 `form:"id"` Id int64 `form:"id"`
Type int `form:"type"` Type int `form:"type"`
AuthName string `form:"name" binding:"Required;MaxSize(50)"` AuthName string `form:"name" binding:"Required;MaxSize(50)"`
Domain string `form:"domain" binding:"Required"` Domain string `form:"domain"`
Host string `form:"host" binding:"Required"` Host string `form:"host"`
Port int `form:"port" binding:"Required"` Port int `form:"port"`
BaseDN string `form:"base_dn" binding:"Required"` BaseDN string `form:"base_dn"`
Attributes string `form:"attributes" binding:"Required"` Attributes string `form:"attributes"`
Filter string `form:"filter" binding:"Required"` Filter string `form:"filter"`
MsAdSA string `form:"ms_ad_sa" binding:"Required"` MsAdSA string `form:"ms_ad_sa"`
IsActived bool `form:"is_actived"` 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 { func (f *AuthenticationForm) Name(field string) string {

View File

@ -5,10 +5,11 @@
package admin package admin
import ( import (
"errors"
"strings" "strings"
"github.com/go-martini/martini" "github.com/go-martini/martini"
"github.com/go-xorm/core"
"github.com/gogits/gogs/models" "github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/auth" "github.com/gogits/gogs/modules/auth"
"github.com/gogits/gogs/modules/auth/ldap" "github.com/gogits/gogs/modules/auth/ldap"
@ -21,32 +22,55 @@ func NewAuthSource(ctx *middleware.Context) {
ctx.Data["Title"] = "New Authentication" ctx.Data["Title"] = "New Authentication"
ctx.Data["PageIsAuths"] = true ctx.Data["PageIsAuths"] = true
ctx.Data["LoginTypes"] = models.LoginTypes ctx.Data["LoginTypes"] = models.LoginTypes
ctx.Data["SMTPAuths"] = models.SMTPAuths
ctx.HTML(200, "admin/auths/new") ctx.HTML(200, "admin/auths/new")
} }
func NewAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) { func NewAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
ctx.Data["Title"] = "New Authentication" ctx.Data["Title"] = "New Authentication"
ctx.Data["PageIsAuths"] = true ctx.Data["PageIsAuths"] = true
ctx.Data["LoginTypes"] = models.LoginTypes
ctx.Data["SMTPAuths"] = models.SMTPAuths
if ctx.HasError() { if ctx.HasError() {
ctx.HTML(200, "admin/auths/new") ctx.HTML(200, "admin/auths/new")
return return
} }
u := &models.LDAPConfig{ var u core.Conversion
Ldapsource: ldap.Ldapsource{ if form.Type == models.LT_LDAP {
Host: form.Host, u = &models.LDAPConfig{
Port: form.Port, Ldapsource: ldap.Ldapsource{
BaseDN: form.BaseDN, Host: form.Host,
Attributes: form.Attributes, Port: form.Port,
Filter: form.Filter, BaseDN: form.BaseDN,
MsAdSAFormat: form.MsAdSA, Attributes: form.Attributes,
Enabled: true, Filter: form.Filter,
Name: form.AuthName, 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 { switch err {
default: default:
ctx.Handle(500, "admin.auths.NewAuth", err) 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) { func EditAuthSource(ctx *middleware.Context, params martini.Params) {
ctx.Data["Title"] = "Edit Authentication" ctx.Data["Title"] = "Edit Authentication"
ctx.Data["PageIsAuths"] = true ctx.Data["PageIsAuths"] = true
ctx.Data["LoginTypes"] = models.LoginTypes
ctx.Data["SMTPAuths"] = models.SMTPAuths
id, err := base.StrTo(params["authid"]).Int64() id, err := base.StrTo(params["authid"]).Int64()
if err != nil { if err != nil {
ctx.Handle(404, "admin.auths.EditAuthSource", err) ctx.Handle(404, "admin.auths.EditAuthSource", err)
@ -74,24 +101,23 @@ func EditAuthSource(ctx *middleware.Context, params martini.Params) {
return return
} }
ctx.Data["Source"] = u ctx.Data["Source"] = u
ctx.Data["LoginTypes"] = models.LoginTypes
ctx.HTML(200, "admin/auths/edit") ctx.HTML(200, "admin/auths/edit")
} }
func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) { func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
ctx.Data["Title"] = "Edit Authentication" ctx.Data["Title"] = "Edit Authentication"
ctx.Data["PageIsAuths"] = true ctx.Data["PageIsAuths"] = true
ctx.Data["LoginTypes"] = models.LoginTypes
ctx.Data["SMTPAuths"] = models.SMTPAuths
if ctx.HasError() { if ctx.HasError() {
ctx.HTML(200, "admin/auths/edit") ctx.HTML(200, "admin/auths/edit")
return return
} }
u := models.LoginSource{ var config core.Conversion
Name: form.AuthName, if form.Type == models.LT_LDAP {
IsActived: form.IsActived, config = &models.LDAPConfig{
Type: models.LT_LDAP,
Cfg: &models.LDAPConfig{
Ldapsource: ldap.Ldapsource{ Ldapsource: ldap.Ldapsource{
Host: form.Host, Host: form.Host,
Port: form.Port, Port: form.Port,
@ -102,10 +128,25 @@ func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
Enabled: true, Enabled: true,
Name: form.AuthName, 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 { switch err {
default: default:
ctx.Handle(500, "admin.auths.EditAuth", err) ctx.Handle(500, "admin.auths.EditAuth", err)

View File

@ -14,18 +14,16 @@
{{.CsrfTokenHtml}} {{.CsrfTokenHtml}}
{{template "base/alert" .}} {{template "base/alert" .}}
<input type="hidden" value="{{.Source.Id}}" name="id"/> <input type="hidden" value="{{.Source.Id}}" name="id"/>
{{$type := .Source.Type}}
<div class="form-group"> <div class="form-group">
<label class="col-md-3 control-label">Auth Type: </label> <label class="col-md-3 control-label">Auth Type: </label>
<input type="hidden" name="type" value="{{.Source.Type}}"/>
<div class="col-md-7"> <div class="col-md-7">
<select class="form-control"> {{range $key, $val := .LoginTypes}}
{{$type := .Source.Type}} {{if eq $key $type}}{{$val}}{{end}}
{{range $key, $val := .LoginTypes}} {{end}}
<option value="{{$key}}" {{if eq $key $type}}selected{{end}}>{{$val}}</option>
{{end}}
</select>
</div> </div>
</div> </div>
<div class="form-group {{if .Err_AuthName}}has-error has-feedback{{end}}"> <div class="form-group {{if .Err_AuthName}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Name: </label> <label class="col-md-3 control-label">Name: </label>
<div class="col-md-7"> <div class="col-md-7">
@ -33,6 +31,8 @@
</div> </div>
</div> </div>
{{if eq $type 2}}
<div class="form-group {{if .Err_Domain}}has-error has-feedback{{end}}"> <div class="form-group {{if .Err_Domain}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Domain: </label> <label class="col-md-3 control-label">Domain: </label>
<div class="col-md-7"> <div class="col-md-7">
@ -81,7 +81,53 @@
<input name="ms_ad_sa" class="form-control" placeholder="Type Ms Ad SA" value="{{.Source.LDAP.MsAdSAFormat}}" required="required"> <input name="ms_ad_sa" class="form-control" placeholder="Type Ms Ad SA" value="{{.Source.LDAP.MsAdSAFormat}}" required="required">
</div> </div>
</div> </div>
{{else}}
{{if eq $type 3}}
<div class="form-group {{if .Err_TLS}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">SMTP Auth: </label>
<div class="col-md-7">
<select name="smtpauth" class="form-control">
{{$auth := .Source.SMTP.Auth}}
{{range .SMTPAuths}}
<option value="{{.}}"
{{if eq . $auth}} selected{{end}}>{{.}}</option>
{{end}}
}
</select>
</div>
</div>
<div class="form-group {{if .Err_Host}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Host: </label>
<div class="col-md-7">
<input name="smtphost" class="form-control" placeholder="Type host address" value="{{.Source.SMTP.Host}}">
</div>
</div>
<div class="form-group {{if .Err_Port}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Port: </label>
<div class="col-md-7">
<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="smtptls" type="checkbox" class="form-control" {{if .Source.SMTP.TLS}}checked{{end}}>
</div>
</div>
{{end}}
{{end}}
<div class="form-group {{if .Err_TLS}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Auto Register: </label>
<div class="col-md-7">
<input name="allowautoregister" type="checkbox" class="form-control" {{if .Source.AllowAutoRegisted}}checked{{end}}>
</div>
</div>
<div class="form-group"> <div class="form-group">
<div class="col-md-7 col-md-offset-3"> <div class="col-md-7 col-md-offset-3">
<div class="checkbox"> <div class="checkbox">

View File

@ -16,104 +16,111 @@
<div class="form-group"> <div class="form-group">
<label class="col-md-3 control-label">Auth Type: </label> <label class="col-md-3 control-label">Auth Type: </label>
<div class="col-md-7"> <div class="col-md-7">
<select class="form-control" id="auth-type"> <select name="type" class="form-control" id="auth-type">
{{range $key, $val := .LoginTypes}} {{range $key, $val := .LoginTypes}}
<option value="{{$key}}">{{$val}}</option> <option value="{{$key}}">{{$val}}</option>
{{end}} {{end}}
</select> </select>
</div> </div>
</div> </div>
<div class="ldap"> <div class="form-group {{if .Err_AuthName}}has-error has-feedback{{end}}">
<div class="form-group {{if .Err_AuthName}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Name: </label> <label class="col-md-3 control-label">Name: </label>
<div class="col-md-7"> <div class="col-md-7">
<input name="name" class="form-control" placeholder="Type authentication's name" value="{{.name}}" required="required"> <input name="name" class="form-control" placeholder="Type authentication's name" value="{{.name}}">
</div> </div>
</div> </div>
<div class="ldap">
<div class="form-group {{if .Err_Domain}}has-error has-feedback{{end}}"> <div class="form-group {{if .Err_Domain}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Domain: </label> <label class="col-md-3 control-label">Domain: </label>
<div class="col-md-7"> <div class="col-md-7">
<input name="domain" class="form-control" placeholder="Type domain name" value="{{.domain}}" required="required"> <input name="domain" class="form-control" placeholder="Type domain name" value="{{.domain}}">
</div> </div>
</div> </div>
<div class="form-group {{if .Err_Host}}has-error has-feedback{{end}}"> <div class="form-group {{if .Err_Host}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Host: </label> <label class="col-md-3 control-label">Host: </label>
<div class="col-md-7"> <div class="col-md-7">
<input name="host" class="form-control" placeholder="Type host address" value="{{.host}}" required="required"> <input name="host" class="form-control" placeholder="Type host address" value="{{.host}}">
</div> </div>
</div> </div>
<div class="form-group {{if .Err_Port}}has-error has-feedback{{end}}"> <div class="form-group {{if .Err_Port}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Port: </label> <label class="col-md-3 control-label">Port: </label>
<div class="col-md-7"> <div class="col-md-7">
<input name="port" class="form-control" placeholder="Type port number" value="{{.port}}" required="required"> <input name="port" class="form-control" placeholder="Type port number" value="{{.port}}">
</div> </div>
</div> </div>
<div class="form-group {{if .Err_BaseDN}}has-error has-feedback{{end}}"> <div class="form-group {{if .Err_BaseDN}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Base DN: </label> <label class="col-md-3 control-label">Base DN: </label>
<div class="col-md-7"> <div class="col-md-7">
<input name="base_dn" class="form-control" placeholder="Type base DN" value="{{.base_dn}}" required="required"> <input name="base_dn" class="form-control" placeholder="Type base DN" value="{{.base_dn}}">
</div> </div>
</div> </div>
<div class="form-group {{if .Err_Attributes}}has-error has-feedback{{end}}"> <div class="form-group {{if .Err_Attributes}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Search Attributes: </label> <label class="col-md-3 control-label">Search Attributes: </label>
<div class="col-md-7"> <div class="col-md-7">
<input name="attributes" class="form-control" placeholder="Type search attributes" value="{{.attributes}}" required="required"> <input name="attributes" class="form-control" placeholder="Type search attributes" value="{{.attributes}}">
</div> </div>
</div> </div>
<div class="form-group {{if .Err_Filter}}has-error has-feedback{{end}}"> <div class="form-group {{if .Err_Filter}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Search Filter: </label> <label class="col-md-3 control-label">Search Filter: </label>
<div class="col-md-7"> <div class="col-md-7">
<input name="filter" class="form-control" placeholder="Type search filter" value="{{.filter}}" required="required"> <input name="filter" class="form-control" placeholder="Type search filter" value="{{.filter}}">
</div> </div>
</div> </div>
<div class="form-group {{if .Err_MsAdSA}}has-error has-feedback{{end}}"> <div class="form-group {{if .Err_MsAdSA}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Ms Ad SA: </label> <label class="col-md-3 control-label">Ms Ad SA: </label>
<div class="col-md-7"> <div class="col-md-7">
<input name="ms_ad_sa" class="form-control" placeholder="Type Ms Ad SA" value="{{.ms_ad_sa}}" required="required"> <input name="ms_ad_sa" class="form-control" placeholder="Type Ms Ad SA" value="{{.ms_ad_sa}}">
</div> </div>
</div> </div>
</div> </div>
<div class="smtp hidden"> <div class="smtp hidden">
<div class="form-group {{if .Err_TLS}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">SMTP Auth: </label>
<div class="col-md-7">
<select name="smtpauth" class="form-control">
{{range .SMTPAuths}}
<option value="{{.}}">{{.}}</option>
{{end}}
}
</select>
</div>
</div>
<div class="form-group {{if .Err_Host}}has-error has-feedback{{end}}"> <div class="form-group {{if .Err_Host}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Host: </label> <label class="col-md-3 control-label">Host: </label>
<div class="col-md-7"> <div class="col-md-7">
<input name="host" class="form-control" placeholder="Type host address" value="{{.host}}" required="required"> <input name="smtphost" class="form-control" placeholder="Type host address" value="{{.host}}">
</div> </div>
</div> </div>
<div class="form-group {{if .Err_Port}}has-error has-feedback{{end}}"> <div class="form-group {{if .Err_Port}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Port: </label> <label class="col-md-3 control-label">Port: </label>
<div class="col-md-7"> <div class="col-md-7">
<input name="port" class="form-control" placeholder="Type port number" value="{{.port}}" required="required"> <input name="smtpport" class="form-control" placeholder="Type port number" value="{{.port}}">
</div> </div>
</div> </div>
<div class="form-group {{if .Err_TLS}}has-error has-feedback{{end}}"> <div class="form-group {{if .Err_TLS}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">TLS: </label> <label class="col-md-3 control-label">TLS: </label>
<div class="col-md-7"> <div class="col-md-7">
<input name="port" type="checkbox" class="form-control" value="" required="required"> <input name="smtptls" type="checkbox" class="form-control" value="">
</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">
<select class="form-control">
<option value="">options</option>
</select>
</div> </div>
</div> </div>
</div> </div>
<div class="form-group {{if .Err_TLS}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Auto Register: </label>
<div class="col-md-7">
<input name="allowautoregister" type="checkbox" class="form-control" value="">
</div>
</div>
<hr/> <hr/>
<div class="form-group"> <div class="form-group">
<div class="col-md-offset-3 col-md-7"> <div class="col-md-offset-3 col-md-7">