Skip to content

Commit

Permalink
Merge pull request #7 from yujular/feat-Registerwhitelist
Browse files Browse the repository at this point in the history
feat: Add email whitelist for registration
  • Loading branch information
JingYiJun authored Sep 27, 2023
2 parents 879b8e1 + 6dcad48 commit 6191861
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
33 changes: 17 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@ issuing tokens

Environment Variables

| Name | Default | Valid values | Description |
|:-------------------------:|:---------------:|:----------------------------:|:----------------------------------------------------------------------------:|
| MODE | dev | dev, production, test, bench | if dev, log gorm debug sql |
| DB_URL | | | Database DSN, required in "production" mode |
| KONG_URL | | | if STANDALONE is false, required to connect to kong gateway |
| REDIS_URL | | | if not set, use go-cache instead |
| NOTIFICATION_URL | | | if not set, no notification will be sent |
| EMAIL_WHITELIST | | | use ',' to separate emails; if not set, allow all emails |
| EMAIL_SERVER_NO_REPLY_URL | | | required in "production" mode; if not set, unable to send verification email |
| EMAIL_DOMAIN | | | required in "production" mode; if not set, unable to send verification email |
| EMAIL_DEV | [email protected] | | send email if shamir update failed |
| SHAMIR_FEATURE | true | | if enabled, check email shamir encryption when users register and login |
| STANDALONE | false | | if not set, this application not required to set KONG_URL |
| VERIFICATION_CODE_EXPIRES | 10 | integers | register verification code expiration time |
| SITE_NAME | Open Tree Hole | | title prefix of verification email |
| ENABLE_REGISTER_QUESTIONS | false | | if set, user will be set "have not answered questions" when registered |
| Name | Default | Valid values | Description |
|:-------------------------:|:---------------:|:----------------------------:|:------------------------------------------------------------------------------------:|
| MODE | dev | dev, production, test, bench | if dev, log gorm debug sql |
| DB_URL | | | Database DSN, required in "production" mode |
| KONG_URL | | | if STANDALONE is false, required to connect to kong gateway |
| REDIS_URL | | | if not set, use go-cache instead |
| NOTIFICATION_URL | | | if not set, no notification will be sent |
| EMAIL_WHITELIST | | | use ',' to separate emails; if not set, allow all emails |
| VALIDATE_EMAIL_WHITELIST | | | use ',' to separate emails; the emails in it will not be checked for year vs. suffix |
| EMAIL_SERVER_NO_REPLY_URL | | | required in "production" mode; if not set, unable to send verification email |
| EMAIL_DOMAIN | | | required in "production" mode; if not set, unable to send verification email |
| EMAIL_DEV | [email protected] | | send email if shamir update failed |
| SHAMIR_FEATURE | true | | if enabled, check email shamir encryption when users register and login |
| STANDALONE | false | | if not set, this application not required to set KONG_URL |
| VERIFICATION_CODE_EXPIRES | 10 | integers | register verification code expiration time |
| SITE_NAME | Open Tree Hole | | title prefix of verification email |
| ENABLE_REGISTER_QUESTIONS | false | | if set, user will be set "have not answered questions" when registered |

File settings, required in production mode

Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var Config struct {
RedisUrl string
NotificationUrl string
EmailWhitelist []string
ValidateEmailWhitelist []string
EmailServerNoReplyUrl url.URL `env:"EMAIL_SERVER_NO_REPLY_URL"`
EmailDomain string
EmailDev string `envDefault:"[email protected]"`
Expand Down
9 changes: 7 additions & 2 deletions utils/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,16 @@ func ValidateEmailFudan(email string) error {

if emailSplit[1] == "fudan.edu.cn" {
if year >= 21 {
return common.BadRequest("21级及以后的同学请使用m.fudan.edu.cn邮箱。" + messageSuffix)
// check in whitelist
if !InUnorderedSlice(config.Config.ValidateEmailWhitelist, email) {
return common.BadRequest("21级及以后的同学请使用m.fudan.edu.cn邮箱。" + messageSuffix)
}
}
} else if emailSplit[1] == "m.fudan.edu.cn" {
if year <= 20 {
return common.BadRequest("20级及以前的同学请使用fudan.edu.cn邮箱。" + messageSuffix)
if !InUnorderedSlice(config.Config.ValidateEmailWhitelist, email) {
return common.BadRequest("20级及以前的同学请使用fudan.edu.cn邮箱。" + messageSuffix)
}
}
}
return nil
Expand Down

0 comments on commit 6191861

Please sign in to comment.