2021-07-28 06:23:37 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
|
|
|
"github.com/joho/godotenv"
|
|
|
|
)
|
|
|
|
|
2021-07-28 07:55:52 +00:00
|
|
|
// build variables
|
|
|
|
var Version string
|
|
|
|
|
2021-07-28 06:23:37 +00:00
|
|
|
// ParseArgs -> to parse the cli flag and get db url. This is useful with heroku button
|
|
|
|
func ParseArgs() {
|
|
|
|
dbURL := flag.String("database_url", "", "Database connection string")
|
2021-08-21 19:16:14 +00:00
|
|
|
dbType := flag.String("databse_type", "", "Database type, possible values are postgres,mysql,sqlite")
|
2021-08-18 18:09:47 +00:00
|
|
|
authorizerURL := flag.String("authorizer_url", "", "URL for authorizer instance, eg: https://xyz.herokuapp.com")
|
|
|
|
|
2021-07-28 06:23:37 +00:00
|
|
|
flag.Parse()
|
|
|
|
if *dbURL != "" {
|
|
|
|
constants.DATABASE_URL = *dbURL
|
|
|
|
}
|
|
|
|
|
|
|
|
if *dbType != "" {
|
|
|
|
constants.DATABASE_TYPE = *dbType
|
|
|
|
}
|
2021-07-28 11:22:51 +00:00
|
|
|
|
2021-08-04 06:48:57 +00:00
|
|
|
if *authorizerURL != "" {
|
|
|
|
constants.AUTHORIZER_URL = *authorizerURL
|
2021-07-28 11:22:51 +00:00
|
|
|
}
|
2021-07-28 06:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InitEnv -> to initialize env and through error if required env are not present
|
|
|
|
func InitEnv() {
|
2021-08-18 18:09:47 +00:00
|
|
|
envPath := `.env`
|
|
|
|
envFile := flag.String("env_file", "", "Env file path")
|
|
|
|
flag.Parse()
|
|
|
|
if *envFile != "" {
|
|
|
|
envPath = *envFile
|
|
|
|
}
|
|
|
|
err := godotenv.Load(envPath)
|
2021-07-28 06:23:37 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Error loading .env file")
|
|
|
|
}
|
|
|
|
|
2021-07-28 07:55:52 +00:00
|
|
|
constants.VERSION = Version
|
|
|
|
|
2021-07-28 13:08:04 +00:00
|
|
|
constants.ADMIN_SECRET = os.Getenv("ADMIN_SECRET")
|
2021-07-28 06:23:37 +00:00
|
|
|
constants.ENV = os.Getenv("ENV")
|
|
|
|
constants.DATABASE_TYPE = os.Getenv("DATABASE_TYPE")
|
|
|
|
constants.DATABASE_URL = os.Getenv("DATABASE_URL")
|
|
|
|
constants.SMTP_HOST = os.Getenv("SMTP_HOST")
|
|
|
|
constants.SMTP_PORT = os.Getenv("SMTP_PORT")
|
|
|
|
constants.SENDER_EMAIL = os.Getenv("SENDER_EMAIL")
|
|
|
|
constants.SENDER_PASSWORD = os.Getenv("SENDER_PASSWORD")
|
|
|
|
constants.JWT_SECRET = os.Getenv("JWT_SECRET")
|
|
|
|
constants.JWT_TYPE = os.Getenv("JWT_TYPE")
|
2021-08-04 06:48:57 +00:00
|
|
|
constants.AUTHORIZER_URL = strings.TrimSuffix(os.Getenv("AUTHORIZER_URL"), "/")
|
2021-07-28 06:23:37 +00:00
|
|
|
constants.PORT = os.Getenv("PORT")
|
|
|
|
constants.REDIS_URL = os.Getenv("REDIS_URL")
|
|
|
|
constants.COOKIE_NAME = os.Getenv("COOKIE_NAME")
|
|
|
|
constants.GOOGLE_CLIENT_ID = os.Getenv("GOOGLE_CLIENT_ID")
|
|
|
|
constants.GOOGLE_CLIENT_SECRET = os.Getenv("GOOGLE_CLIENT_SECRET")
|
|
|
|
constants.GITHUB_CLIENT_ID = os.Getenv("GITHUB_CLIENT_ID")
|
|
|
|
constants.GITHUB_CLIENT_SECRET = os.Getenv("GITHUB_CLIENT_SECRET")
|
2021-07-28 06:28:52 +00:00
|
|
|
constants.FACEBOOK_CLIENT_ID = os.Getenv("FACEBOOK_CLIENT_ID")
|
|
|
|
constants.FACEBOOK_CLIENT_SECRET = os.Getenv("FACEBOOK_CLIENT_SECRET")
|
|
|
|
constants.TWITTER_CLIENT_ID = os.Getenv("TWITTER_CLIENT_ID")
|
|
|
|
constants.TWITTER_CLIENT_SECRET = os.Getenv("TWITTER_CLIENT_SECRET")
|
2021-08-04 10:25:13 +00:00
|
|
|
constants.RESET_PASSWORD_URL = strings.TrimPrefix(os.Getenv("RESET_PASSWORD_URL"), "/")
|
2021-07-28 07:55:52 +00:00
|
|
|
constants.DISABLE_BASIC_AUTHENTICATION = os.Getenv("DISABLE_BASIC_AUTHENTICATION")
|
2021-08-06 18:14:20 +00:00
|
|
|
constants.DISABLE_EMAIL_VERIFICATION = os.Getenv("DISABLE_EMAIL_VERIFICATION")
|
2021-07-28 07:55:52 +00:00
|
|
|
|
2021-07-28 13:08:04 +00:00
|
|
|
if constants.ADMIN_SECRET == "" {
|
2021-07-28 07:55:52 +00:00
|
|
|
panic("root admin secret is required")
|
2021-07-28 06:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if constants.ENV == "" {
|
|
|
|
constants.ENV = "production"
|
|
|
|
}
|
|
|
|
|
|
|
|
if constants.ENV == "production" {
|
|
|
|
constants.IS_PROD = true
|
|
|
|
os.Setenv("GIN_MODE", "release")
|
|
|
|
} else {
|
|
|
|
constants.IS_PROD = false
|
|
|
|
}
|
|
|
|
|
2021-08-04 06:48:57 +00:00
|
|
|
allowedOriginsSplit := strings.Split(os.Getenv("ALLOWED_ORIGINS"), ",")
|
|
|
|
allowedOrigins := []string{}
|
|
|
|
for _, val := range allowedOriginsSplit {
|
|
|
|
trimVal := strings.TrimSpace(val)
|
|
|
|
if trimVal != "" {
|
|
|
|
allowedOrigins = append(allowedOrigins, trimVal)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(allowedOrigins) == 0 {
|
|
|
|
allowedOrigins = []string{"*"}
|
|
|
|
}
|
|
|
|
constants.ALLOWED_ORIGINS = allowedOrigins
|
|
|
|
|
|
|
|
allowedCallbackSplit := strings.Split(os.Getenv("ALLOWED_CALLBACK_URLS"), ",")
|
|
|
|
allowedCallbacks := []string{}
|
|
|
|
for _, val := range allowedCallbackSplit {
|
|
|
|
trimVal := strings.TrimSpace(val)
|
|
|
|
if trimVal != "" {
|
|
|
|
allowedCallbacks = append(allowedCallbacks, trimVal)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(allowedCallbackSplit) == 0 {
|
|
|
|
allowedCallbackSplit = []string{"*"}
|
|
|
|
}
|
|
|
|
constants.ALLOWED_CALLBACK_URLS = allowedCallbackSplit
|
|
|
|
|
2021-07-28 06:23:37 +00:00
|
|
|
ParseArgs()
|
|
|
|
if constants.DATABASE_URL == "" {
|
|
|
|
panic("Database url is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
if constants.DATABASE_TYPE == "" {
|
|
|
|
panic("Database type is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
if constants.JWT_TYPE == "" {
|
|
|
|
constants.JWT_TYPE = "HS256"
|
|
|
|
}
|
|
|
|
|
|
|
|
if constants.COOKIE_NAME == "" {
|
|
|
|
constants.COOKIE_NAME = "authorizer"
|
|
|
|
}
|
|
|
|
|
2021-07-28 07:55:52 +00:00
|
|
|
if constants.DISABLE_BASIC_AUTHENTICATION == "" {
|
|
|
|
constants.DISABLE_BASIC_AUTHENTICATION = "false"
|
|
|
|
}
|
|
|
|
|
2021-08-06 18:14:20 +00:00
|
|
|
if constants.DISABLE_EMAIL_VERIFICATION == "" && constants.DISABLE_BASIC_AUTHENTICATION == "false" {
|
2021-07-28 07:55:52 +00:00
|
|
|
if constants.SMTP_HOST == "" || constants.SENDER_EMAIL == "" || constants.SENDER_PASSWORD == "" {
|
2021-08-06 18:14:20 +00:00
|
|
|
constants.DISABLE_EMAIL_VERIFICATION = "true"
|
2021-07-28 07:55:52 +00:00
|
|
|
} else {
|
2021-08-06 18:14:20 +00:00
|
|
|
constants.DISABLE_EMAIL_VERIFICATION = "false"
|
2021-07-28 07:55:52 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-28 06:23:37 +00:00
|
|
|
}
|