1
0
mirror of https://github.com/go-gitea/gitea synced 2024-12-24 03:35:55 +01:00

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

@ -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
}

@ -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 {

@ -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)

@ -14,18 +14,16 @@
{{.CsrfTokenHtml}}
{{template "base/alert" .}}
<input type="hidden" value="{{.Source.Id}}" name="id"/>
{{$type := .Source.Type}}
<div class="form-group">
<label class="col-md-3 control-label">Auth Type: </label>
<input type="hidden" name="type" value="{{.Source.Type}}"/>
<div class="col-md-7">
<select class="form-control">
{{$type := .Source.Type}}
{{range $key, $val := .LoginTypes}}
<option value="{{$key}}" {{if eq $key $type}}selected{{end}}>{{$val}}</option>
{{end}}
</select>
{{range $key, $val := .LoginTypes}}
{{if eq $key $type}}{{$val}}{{end}}
{{end}}
</div>
</div>
<div class="form-group {{if .Err_AuthName}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Name: </label>
<div class="col-md-7">
@ -33,6 +31,8 @@
</div>
</div>
{{if eq $type 2}}
<div class="form-group {{if .Err_Domain}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Domain: </label>
<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">
</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="col-md-7 col-md-offset-3">
<div class="checkbox">

@ -16,104 +16,111 @@
<div class="form-group">
<label class="col-md-3 control-label">Auth Type: </label>
<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}}
<option value="{{$key}}">{{$val}}</option>
{{end}}
</select>
</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>
<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 class="ldap">
<div class="form-group {{if .Err_Domain}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Domain: </label>
<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 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="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 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="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 class="form-group {{if .Err_BaseDN}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Base DN: </label>
<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 class="form-group {{if .Err_Attributes}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Search Attributes: </label>
<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 class="form-group {{if .Err_Filter}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Search Filter: </label>
<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 class="form-group {{if .Err_MsAdSA}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Ms Ad SA: </label>
<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 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}}">
<label class="col-md-3 control-label">Host: </label>
<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 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="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 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="port" type="checkbox" class="form-control" value="" required="required">
</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>
<input name="smtptls" type="checkbox" class="form-control" value="">
</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/>
<div class="form-group">
<div class="col-md-offset-3 col-md-7">