|
|
@ -1,4 +1,4 @@ |
|
|
|
// Copyright github.com/juju2013. All rights reserved.
|
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
|
@ -20,12 +20,13 @@ import ( |
|
|
|
"github.com/gogits/gogs/modules/log" |
|
|
|
"github.com/gogits/gogs/modules/log" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// Login types.
|
|
|
|
type LoginType int |
|
|
|
|
|
|
|
|
|
|
|
const ( |
|
|
|
const ( |
|
|
|
LT_NOTYPE = iota |
|
|
|
NOTYPE LoginType = iota |
|
|
|
LT_PLAIN |
|
|
|
PLAIN |
|
|
|
LT_LDAP |
|
|
|
LDAP |
|
|
|
LT_SMTP |
|
|
|
SMTP |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
var ( |
|
|
@ -34,9 +35,9 @@ var ( |
|
|
|
ErrAuthenticationUserUsed = errors.New("Authentication has been used by some users") |
|
|
|
ErrAuthenticationUserUsed = errors.New("Authentication has been used by some users") |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
var LoginTypes = map[int]string{ |
|
|
|
var LoginTypes = map[LoginType]string{ |
|
|
|
LT_LDAP: "LDAP", |
|
|
|
LDAP: "LDAP", |
|
|
|
LT_SMTP: "SMTP", |
|
|
|
SMTP: "SMTP", |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Ensure structs implmented interface.
|
|
|
|
// Ensure structs implmented interface.
|
|
|
@ -49,7 +50,6 @@ type LDAPConfig struct { |
|
|
|
ldap.Ldapsource |
|
|
|
ldap.Ldapsource |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// implement
|
|
|
|
|
|
|
|
func (cfg *LDAPConfig) FromDB(bs []byte) error { |
|
|
|
func (cfg *LDAPConfig) FromDB(bs []byte) error { |
|
|
|
return json.Unmarshal(bs, &cfg.Ldapsource) |
|
|
|
return json.Unmarshal(bs, &cfg.Ldapsource) |
|
|
|
} |
|
|
|
} |
|
|
@ -65,7 +65,6 @@ type SMTPConfig struct { |
|
|
|
TLS bool |
|
|
|
TLS bool |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// implement
|
|
|
|
|
|
|
|
func (cfg *SMTPConfig) FromDB(bs []byte) error { |
|
|
|
func (cfg *SMTPConfig) FromDB(bs []byte) error { |
|
|
|
return json.Unmarshal(bs, cfg) |
|
|
|
return json.Unmarshal(bs, cfg) |
|
|
|
} |
|
|
|
} |
|
|
@ -76,13 +75,13 @@ func (cfg *SMTPConfig) ToDB() ([]byte, error) { |
|
|
|
|
|
|
|
|
|
|
|
type LoginSource struct { |
|
|
|
type LoginSource struct { |
|
|
|
Id int64 |
|
|
|
Id int64 |
|
|
|
Type int |
|
|
|
Type LoginType |
|
|
|
Name string `xorm:"unique"` |
|
|
|
Name string `xorm:"UNIQUE"` |
|
|
|
IsActived bool `xorm:"not null default false"` |
|
|
|
IsActived bool `xorm:"NOT NULL DEFAULT false"` |
|
|
|
Cfg core.Conversion `xorm:"TEXT"` |
|
|
|
Cfg core.Conversion `xorm:"TEXT"` |
|
|
|
Created time.Time `xorm:"created"` |
|
|
|
AllowAutoRegister bool `xorm:"NOT NULL DEFAULT false"` |
|
|
|
Updated time.Time `xorm:"updated"` |
|
|
|
Created time.Time `xorm:"CREATED"` |
|
|
|
AllowAutoRegister bool `xorm:"not null default false"` |
|
|
|
Updated time.Time `xorm:"UPDATED"` |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (source *LoginSource) TypeString() string { |
|
|
|
func (source *LoginSource) TypeString() string { |
|
|
@ -97,21 +96,25 @@ func (source *LoginSource) SMTP() *SMTPConfig { |
|
|
|
return source.Cfg.(*SMTPConfig) |
|
|
|
return source.Cfg.(*SMTPConfig) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// for xorm callback
|
|
|
|
|
|
|
|
func (source *LoginSource) BeforeSet(colName string, val xorm.Cell) { |
|
|
|
func (source *LoginSource) BeforeSet(colName string, val xorm.Cell) { |
|
|
|
if colName == "type" { |
|
|
|
if colName == "type" { |
|
|
|
ty := (*val).(int64) |
|
|
|
ty := (*val).(int64) |
|
|
|
switch ty { |
|
|
|
switch LoginType(ty) { |
|
|
|
case LT_LDAP: |
|
|
|
case LDAP: |
|
|
|
source.Cfg = new(LDAPConfig) |
|
|
|
source.Cfg = new(LDAPConfig) |
|
|
|
case LT_SMTP: |
|
|
|
case SMTP: |
|
|
|
source.Cfg = new(SMTPConfig) |
|
|
|
source.Cfg = new(SMTPConfig) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func CreateSource(source *LoginSource) error { |
|
|
|
|
|
|
|
_, err := orm.Insert(source) |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func GetAuths() ([]*LoginSource, error) { |
|
|
|
func GetAuths() ([]*LoginSource, error) { |
|
|
|
var auths = make([]*LoginSource, 0) |
|
|
|
var auths = make([]*LoginSource, 0, 5) |
|
|
|
err := orm.Find(&auths) |
|
|
|
err := orm.Find(&auths) |
|
|
|
return auths, err |
|
|
|
return auths, err |
|
|
|
} |
|
|
|
} |
|
|
@ -121,18 +124,12 @@ func GetLoginSourceById(id int64) (*LoginSource, error) { |
|
|
|
has, err := orm.Id(id).Get(source) |
|
|
|
has, err := orm.Id(id).Get(source) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} else if !has { |
|
|
|
if !has { |
|
|
|
|
|
|
|
return nil, ErrAuthenticationNotExist |
|
|
|
return nil, ErrAuthenticationNotExist |
|
|
|
} |
|
|
|
} |
|
|
|
return source, nil |
|
|
|
return source, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func AddSource(source *LoginSource) error { |
|
|
|
|
|
|
|
_, err := orm.Insert(source) |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func UpdateSource(source *LoginSource) error { |
|
|
|
func UpdateSource(source *LoginSource) error { |
|
|
|
_, err := orm.Id(source.Id).AllCols().Update(source) |
|
|
|
_, err := orm.Id(source.Id).AllCols().Update(source) |
|
|
|
return err |
|
|
|
return err |
|
|
@ -164,14 +161,14 @@ func UserSignIn(uname, passwd string) (*User, error) { |
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if u.LoginType == LT_NOTYPE { |
|
|
|
if u.LoginType == NOTYPE { |
|
|
|
if has { |
|
|
|
if has { |
|
|
|
u.LoginType = LT_PLAIN |
|
|
|
u.LoginType = PLAIN |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// for plain login, user must have existed.
|
|
|
|
// for plain login, user must have existed.
|
|
|
|
if u.LoginType == LT_PLAIN { |
|
|
|
if u.LoginType == PLAIN { |
|
|
|
if !has { |
|
|
|
if !has { |
|
|
|
return nil, ErrUserNotExist |
|
|
|
return nil, ErrUserNotExist |
|
|
|
} |
|
|
|
} |
|
|
@ -191,22 +188,20 @@ func UserSignIn(uname, passwd string) (*User, error) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for _, source := range sources { |
|
|
|
for _, source := range sources { |
|
|
|
if source.Type == LT_LDAP { |
|
|
|
if source.Type == LDAP { |
|
|
|
u, err := LoginUserLdapSource(nil, uname, 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, nil |
|
|
|
return u, nil |
|
|
|
} else { |
|
|
|
|
|
|
|
log.Warn("Fail to login(%s) by LDAP(%s): %v", uname, source.Name, err) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} else if source.Type == LT_SMTP { |
|
|
|
log.Warn("Fail to login(%s) by LDAP(%s): %v", uname, source.Name, err) |
|
|
|
|
|
|
|
} else if source.Type == SMTP { |
|
|
|
u, err := LoginUserSMTPSource(nil, uname, 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 { |
|
|
|
return u, nil |
|
|
|
return u, nil |
|
|
|
} else { |
|
|
|
|
|
|
|
log.Warn("Fail to login(%s) by SMTP(%s): %v", uname, source.Name, err) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
log.Warn("Fail to login(%s) by SMTP(%s): %v", uname, source.Name, err) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -224,10 +219,10 @@ func UserSignIn(uname, passwd string) (*User, error) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
switch u.LoginType { |
|
|
|
switch u.LoginType { |
|
|
|
case LT_LDAP: |
|
|
|
case LDAP: |
|
|
|
return LoginUserLdapSource(u, u.LoginName, passwd, |
|
|
|
return LoginUserLdapSource(u, u.LoginName, passwd, |
|
|
|
source.Id, source.Cfg.(*LDAPConfig), false) |
|
|
|
source.Id, source.Cfg.(*LDAPConfig), false) |
|
|
|
case LT_SMTP: |
|
|
|
case SMTP: |
|
|
|
return LoginUserSMTPSource(u, u.LoginName, passwd, |
|
|
|
return LoginUserSMTPSource(u, u.LoginName, passwd, |
|
|
|
source.Id, source.Cfg.(*SMTPConfig), false) |
|
|
|
source.Id, source.Cfg.(*SMTPConfig), false) |
|
|
|
} |
|
|
|
} |
|
|
@ -252,7 +247,7 @@ func LoginUserLdapSource(user *User, name, passwd string, sourceId int64, cfg *L |
|
|
|
user = &User{ |
|
|
|
user = &User{ |
|
|
|
LowerName: strings.ToLower(name), |
|
|
|
LowerName: strings.ToLower(name), |
|
|
|
Name: strings.ToLower(name), |
|
|
|
Name: strings.ToLower(name), |
|
|
|
LoginType: LT_LDAP, |
|
|
|
LoginType: LDAP, |
|
|
|
LoginSource: sourceId, |
|
|
|
LoginSource: sourceId, |
|
|
|
LoginName: name, |
|
|
|
LoginName: name, |
|
|
|
IsActive: true, |
|
|
|
IsActive: true, |
|
|
@ -320,9 +315,8 @@ func SmtpAuth(host string, port int, a smtp.Auth, useTls bool) error { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} else { |
|
|
|
|
|
|
|
return ErrUnsupportedLoginType |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return ErrUnsupportedLoginType |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Query if name/passwd can login against the LDAP direcotry pool
|
|
|
|
// Query if name/passwd can login against the LDAP direcotry pool
|
|
|
@ -358,13 +352,12 @@ func LoginUserSMTPSource(user *User, name, passwd string, sourceId int64, cfg *S |
|
|
|
user = &User{ |
|
|
|
user = &User{ |
|
|
|
LowerName: strings.ToLower(loginName), |
|
|
|
LowerName: strings.ToLower(loginName), |
|
|
|
Name: strings.ToLower(loginName), |
|
|
|
Name: strings.ToLower(loginName), |
|
|
|
LoginType: LT_SMTP, |
|
|
|
LoginType: SMTP, |
|
|
|
LoginSource: sourceId, |
|
|
|
LoginSource: sourceId, |
|
|
|
LoginName: name, |
|
|
|
LoginName: name, |
|
|
|
IsActive: true, |
|
|
|
IsActive: true, |
|
|
|
Passwd: passwd, |
|
|
|
Passwd: passwd, |
|
|
|
Email: name, |
|
|
|
Email: name, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return RegisterUser(user) |
|
|
|
return RegisterUser(user) |
|
|
|
} |
|
|
|
} |
|
|
|