2021-12-20 12:03:11 +00:00
|
|
|
package env
|
2021-07-28 06:23:37 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
2021-10-13 16:41:41 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/utils"
|
2021-07-28 06:23:37 +00:00
|
|
|
"github.com/joho/godotenv"
|
|
|
|
)
|
|
|
|
|
2021-07-28 07:55:52 +00:00
|
|
|
// build variables
|
2021-10-09 18:19:20 +00:00
|
|
|
var (
|
2021-12-20 12:03:11 +00:00
|
|
|
VERSION string
|
2021-10-09 18:19:20 +00:00
|
|
|
ARG_DB_URL *string
|
|
|
|
ARG_DB_TYPE *string
|
|
|
|
ARG_AUTHORIZER_URL *string
|
|
|
|
ARG_ENV_FILE *string
|
|
|
|
)
|
2021-07-28 06:23:37 +00:00
|
|
|
|
|
|
|
// InitEnv -> to initialize env and through error if required env are not present
|
|
|
|
func InitEnv() {
|
2021-12-20 12:03:11 +00:00
|
|
|
if constants.ENV_PATH == "" {
|
|
|
|
constants.ENV_PATH = `.env`
|
|
|
|
}
|
2021-10-09 18:19:20 +00:00
|
|
|
ARG_DB_URL = flag.String("database_url", "", "Database connection string")
|
|
|
|
ARG_DB_TYPE = flag.String("database_type", "", "Database type, possible values are postgres,mysql,sqlite")
|
|
|
|
ARG_AUTHORIZER_URL = flag.String("authorizer_url", "", "URL for authorizer instance, eg: https://xyz.herokuapp.com")
|
|
|
|
ARG_ENV_FILE = flag.String("env_file", "", "Env file path")
|
|
|
|
|
2021-08-18 18:09:47 +00:00
|
|
|
flag.Parse()
|
2021-10-09 18:19:20 +00:00
|
|
|
if *ARG_ENV_FILE != "" {
|
2021-12-20 12:03:11 +00:00
|
|
|
constants.ENV_PATH = *ARG_ENV_FILE
|
2021-08-18 18:09:47 +00:00
|
|
|
}
|
2021-10-09 18:19:20 +00:00
|
|
|
|
2021-12-20 12:03:11 +00:00
|
|
|
err := godotenv.Load(constants.ENV_PATH)
|
2021-07-28 06:23:37 +00:00
|
|
|
if err != nil {
|
2021-12-20 12:03:11 +00:00
|
|
|
log.Printf("error loading %s file", constants.ENV_PATH)
|
2021-07-28 06:23:37 +00:00
|
|
|
}
|
|
|
|
|
2021-12-20 12:03:11 +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")
|
2021-12-17 15:55:07 +00:00
|
|
|
constants.DATABASE_NAME = os.Getenv("DATABASE_NAME")
|
2021-07-28 06:23:37 +00:00
|
|
|
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-12-20 12:03:11 +00:00
|
|
|
constants.DISABLE_BASIC_AUTHENTICATION = os.Getenv("DISABLE_BASIC_AUTHENTICATION") == "true"
|
|
|
|
constants.DISABLE_EMAIL_VERIFICATION = os.Getenv("DISABLE_EMAIL_VERIFICATION") == "true"
|
|
|
|
constants.DISABLE_MAGIC_LOGIN = os.Getenv("DISABLE_MAGIC_LOGIN") == "true"
|
2021-09-20 05:06:26 +00:00
|
|
|
constants.JWT_ROLE_CLAIM = os.Getenv("JWT_ROLE_CLAIM")
|
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{}
|
2021-12-21 13:16:54 +00:00
|
|
|
hasWildCard := false
|
|
|
|
|
2021-08-04 06:48:57 +00:00
|
|
|
for _, val := range allowedOriginsSplit {
|
|
|
|
trimVal := strings.TrimSpace(val)
|
|
|
|
if trimVal != "" {
|
2021-12-21 13:16:54 +00:00
|
|
|
if trimVal != "*" {
|
|
|
|
host, port := utils.GetHostParts(trimVal)
|
|
|
|
allowedOrigins = append(allowedOrigins, host+":"+port)
|
|
|
|
} else {
|
|
|
|
hasWildCard = true
|
|
|
|
allowedOrigins = append(allowedOrigins, trimVal)
|
|
|
|
break
|
|
|
|
}
|
2021-08-04 06:48:57 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-21 13:16:54 +00:00
|
|
|
|
|
|
|
if len(allowedOrigins) > 1 && hasWildCard {
|
|
|
|
allowedOrigins = []string{"*"}
|
|
|
|
}
|
|
|
|
|
2021-08-04 06:48:57 +00:00
|
|
|
if len(allowedOrigins) == 0 {
|
|
|
|
allowedOrigins = []string{"*"}
|
|
|
|
}
|
2021-12-21 13:16:54 +00:00
|
|
|
|
2021-08-04 06:48:57 +00:00
|
|
|
constants.ALLOWED_ORIGINS = allowedOrigins
|
|
|
|
|
2021-10-09 18:19:20 +00:00
|
|
|
if *ARG_AUTHORIZER_URL != "" {
|
|
|
|
constants.AUTHORIZER_URL = *ARG_AUTHORIZER_URL
|
|
|
|
}
|
|
|
|
|
|
|
|
if *ARG_DB_URL != "" {
|
|
|
|
constants.DATABASE_URL = *ARG_DB_URL
|
|
|
|
}
|
|
|
|
|
|
|
|
if *ARG_DB_TYPE != "" {
|
|
|
|
constants.DATABASE_TYPE = *ARG_DB_TYPE
|
|
|
|
}
|
|
|
|
|
2021-07-28 06:23:37 +00:00
|
|
|
if constants.DATABASE_URL == "" {
|
|
|
|
panic("Database url is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
if constants.DATABASE_TYPE == "" {
|
|
|
|
panic("Database type is required")
|
|
|
|
}
|
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
if constants.DATABASE_NAME == "" {
|
|
|
|
constants.DATABASE_NAME = "authorizer"
|
|
|
|
}
|
|
|
|
|
2021-07-28 06:23:37 +00:00
|
|
|
if constants.JWT_TYPE == "" {
|
|
|
|
constants.JWT_TYPE = "HS256"
|
|
|
|
}
|
|
|
|
|
|
|
|
if constants.COOKIE_NAME == "" {
|
|
|
|
constants.COOKIE_NAME = "authorizer"
|
|
|
|
}
|
|
|
|
|
2021-11-14 22:42:28 +00:00
|
|
|
if constants.SMTP_HOST == "" || constants.SENDER_EMAIL == "" || constants.SENDER_PASSWORD == "" {
|
2021-12-20 12:03:11 +00:00
|
|
|
constants.DISABLE_EMAIL_VERIFICATION = true
|
|
|
|
constants.DISABLE_MAGIC_LOGIN = true
|
2021-07-28 07:55:52 +00:00
|
|
|
}
|
2021-09-20 05:06:26 +00:00
|
|
|
|
2021-12-20 12:03:11 +00:00
|
|
|
if constants.DISABLE_EMAIL_VERIFICATION {
|
|
|
|
constants.DISABLE_MAGIC_LOGIN = true
|
|
|
|
}
|
2021-11-14 22:42:28 +00:00
|
|
|
|
2021-09-20 05:06:26 +00:00
|
|
|
rolesSplit := strings.Split(os.Getenv("ROLES"), ",")
|
|
|
|
roles := []string{}
|
2021-10-13 16:41:41 +00:00
|
|
|
if len(rolesSplit) == 0 {
|
|
|
|
roles = []string{"user"}
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultRoleSplit := strings.Split(os.Getenv("DEFAULT_ROLES"), ",")
|
|
|
|
defaultRoles := []string{}
|
|
|
|
|
|
|
|
if len(defaultRoleSplit) == 0 {
|
|
|
|
defaultRoles = []string{"user"}
|
|
|
|
}
|
|
|
|
|
|
|
|
protectedRolesSplit := strings.Split(os.Getenv("PROTECTED_ROLES"), ",")
|
|
|
|
protectedRoles := []string{}
|
|
|
|
|
|
|
|
if len(protectedRolesSplit) > 0 {
|
|
|
|
for _, val := range protectedRolesSplit {
|
|
|
|
trimVal := strings.TrimSpace(val)
|
|
|
|
protectedRoles = append(protectedRoles, trimVal)
|
|
|
|
}
|
|
|
|
}
|
2021-09-20 05:06:26 +00:00
|
|
|
|
|
|
|
for _, val := range rolesSplit {
|
|
|
|
trimVal := strings.TrimSpace(val)
|
|
|
|
if trimVal != "" {
|
|
|
|
roles = append(roles, trimVal)
|
|
|
|
}
|
|
|
|
|
2021-10-19 07:27:59 +00:00
|
|
|
if utils.StringSliceContains(defaultRoleSplit, trimVal) {
|
2021-10-13 16:41:41 +00:00
|
|
|
defaultRoles = append(defaultRoles, trimVal)
|
2021-09-20 05:06:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-13 16:41:41 +00:00
|
|
|
if len(roles) > 0 && len(defaultRoles) == 0 && len(defaultRoleSplit) > 0 {
|
|
|
|
panic(`Invalid DEFAULT_ROLE environment variable. It can be one from give ROLES environment variable value`)
|
2021-09-20 05:06:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constants.ROLES = roles
|
2021-10-13 16:41:41 +00:00
|
|
|
constants.DEFAULT_ROLES = defaultRoles
|
|
|
|
constants.PROTECTED_ROLES = protectedRoles
|
2021-09-20 05:06:26 +00:00
|
|
|
|
|
|
|
if constants.JWT_ROLE_CLAIM == "" {
|
|
|
|
constants.JWT_ROLE_CLAIM = "role"
|
|
|
|
}
|
2021-10-03 21:47:50 +00:00
|
|
|
|
|
|
|
if os.Getenv("ORGANIZATION_NAME") != "" {
|
|
|
|
constants.ORGANIZATION_NAME = os.Getenv("ORGANIZATION_NAME")
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.Getenv("ORGANIZATION_LOGO") != "" {
|
|
|
|
constants.ORGANIZATION_LOGO = os.Getenv("ORGANIZATION_LOGO")
|
|
|
|
}
|
2021-07-28 06:23:37 +00:00
|
|
|
}
|