summaryrefslogtreecommitdiffstats
path: root/models/login.go
diff options
context:
space:
mode:
authorUnknwon <u@gogs.io>2015-09-11 13:32:33 -0400
committerUnknwon <u@gogs.io>2015-09-11 13:32:33 -0400
commit362d64df0483700ac435c4b05709265378887a4e (patch)
treefb367bbd236ea7707df0d9d5bbfe5d95a19dca0a /models/login.go
parent373ef5d15e1c2781bf47750554cb21e4ed5446c1 (diff)
downloadgitea-362d64df0483700ac435c4b05709265378887a4e.tar.gz
gitea-362d64df0483700ac435c4b05709265378887a4e.zip
#1620 add allowed domains for SMTP auth
Diffstat (limited to 'models/login.go')
-rw-r--r--models/login.go24
1 files changed, 18 insertions, 6 deletions
diff --git a/models/login.go b/models/login.go
index 33f0f27069..bcff4f9acd 100644
--- a/models/login.go
+++ b/models/login.go
@@ -67,11 +67,12 @@ func (cfg *LDAPConfig) ToDB() ([]byte, error) {
}
type SMTPConfig struct {
- Auth string
- Host string
- Port int
- TLS bool
- SkipVerify bool
+ Auth string
+ Host string
+ Port int
+ AllowedDomains string `xorm:"TEXT"`
+ TLS bool
+ SkipVerify bool
}
func (cfg *SMTPConfig) FromDB(bs []byte) error {
@@ -383,6 +384,16 @@ func SMTPAuth(a smtp.Auth, cfg *SMTPConfig) error {
// Create a local user if success
// Return the same LoginUserPlain semantic
func LoginUserSMTPSource(u *User, name, passwd string, sourceId int64, cfg *SMTPConfig, autoRegister bool) (*User, error) {
+ // Verify allowed domains.
+ if len(cfg.AllowedDomains) > 0 {
+ idx := strings.Index(name, "@")
+ if idx == -1 {
+ return nil, ErrUserNotExist{0, name}
+ } else if !com.IsSliceContainsStr(strings.Split(cfg.AllowedDomains, ","), name[idx+1:]) {
+ return nil, ErrUserNotExist{0, name}
+ }
+ }
+
var auth smtp.Auth
if cfg.Auth == SMTP_PLAIN {
auth = smtp.PlainAuth("", name, passwd, cfg.Host)
@@ -394,7 +405,8 @@ func LoginUserSMTPSource(u *User, name, passwd string, sourceId int64, cfg *SMTP
if err := SMTPAuth(auth, cfg); err != nil {
if strings.Contains(err.Error(), "Username and Password not accepted") {
- return nil, ErrUserNotExist{u.Id, u.Name}
+ fmt.Println(err)
+ return nil, ErrUserNotExist{0, name}
}
return nil, err
}