diff --git a/controllers/auth.go b/controllers/auth.go index 1a349799..55b6e45c 100644 --- a/controllers/auth.go +++ b/controllers/auth.go @@ -22,6 +22,7 @@ import ( "io" "net/http" "net/url" + "regexp" "strconv" "strings" @@ -617,6 +618,17 @@ func (c *ApiController) Login() { c.ResponseError(fmt.Sprintf(c.T("auth:Failed to login in: %s"), err.Error())) return } + + if provider.EmailRegex != "" { + reg, err := regexp.Compile(provider.EmailRegex) + if err != nil { + c.ResponseError(fmt.Sprintf(c.T("auth:Failed to login in: %s"), err.Error())) + return + } + if !reg.MatchString(userInfo.Email) { + c.ResponseError(fmt.Sprintf(c.T("check:Email is invalid"))) + } + } } if authForm.Method == "signup" { diff --git a/object/provider.go b/object/provider.go index cbd91d51..d634b2a3 100644 --- a/object/provider.go +++ b/object/provider.go @@ -16,6 +16,7 @@ package object import ( "fmt" + "regexp" "strings" "github.com/beego/beego/context" @@ -70,6 +71,7 @@ type Provider struct { IdP string `xorm:"mediumtext" json:"idP"` IssuerUrl string `xorm:"varchar(100)" json:"issuerUrl"` EnableSignAuthnRequest bool `json:"enableSignAuthnRequest"` + EmailRegex string `xorm:"varchar(200)" json:"emailRegex"` ProviderUrl string `xorm:"varchar(200)" json:"providerUrl"` } @@ -200,6 +202,13 @@ func UpdateProvider(id string, provider *Provider) (bool, error) { return false, nil } + if provider.EmailRegex != "" { + _, err := regexp.Compile(provider.EmailRegex) + if err != nil { + return false, err + } + } + if name != provider.Name { err := providerChangeTrigger(name, provider.Name) if err != nil { @@ -234,6 +243,13 @@ func AddProvider(provider *Provider) (bool, error) { provider.IntranetEndpoint = util.GetEndPoint(provider.IntranetEndpoint) } + if provider.EmailRegex != "" { + _, err := regexp.Compile(provider.EmailRegex) + if err != nil { + return false, err + } + } + affected, err := ormer.Engine.Insert(provider) if err != nil { return false, err diff --git a/web/src/ProviderEditPage.js b/web/src/ProviderEditPage.js index cfdd5cf1..b8409792 100644 --- a/web/src/ProviderEditPage.js +++ b/web/src/ProviderEditPage.js @@ -633,6 +633,20 @@ class ProviderEditPage extends React.Component { ) } + { + this.state.provider.category === "OAuth" ? ( + + + {Setting.getLabel(i18next.t("provider:Email regex"), i18next.t("provider:Email regex - Tooltip"))} : + + +