Skip to content

Commit

Permalink
SignUp Config Validation
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnGrubba committed Jul 26, 2024
1 parent ff685d0 commit 1055a32
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 22 deletions.
7 changes: 6 additions & 1 deletion src/api/oauth_providers/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
from crud.sessions import create_login_session
from api.model import LoginResponse

github_cnf = json.load(open("/src/app/config/github_client_secret.env.json"))
try:
github_cnf = json.load(open("/src/app/config/github_client_secret.env.json"))
except FileNotFoundError:
raise FileNotFoundError(
"GitHub OAuth Config File not found (github_client_secret.env.json). Please disable this OAuth Provider, or create the file as described in the Docs."
)
REDIRECT_URI = SignupConfig.oauth_base_url + "/oauth/github/callback"
CLIENT_ID = github_cnf["client_id"]
CLIENT_SECRET = github_cnf["client_secret"]
Expand Down
23 changes: 14 additions & 9 deletions src/api/oauth_providers/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,20 @@
)

# Initialize Googles OAuth Flow
flow = Flow.from_client_secrets_file(
client_secrets_file="/src/app/config/google_client_secret.env.json",
scopes=[
"https://www.googleapis.com/auth/userinfo.email",
"openid",
"https://www.googleapis.com/auth/userinfo.profile",
],
redirect_uri=SignupConfig.oauth_base_url + "/oauth/google/callback",
)
try:
flow = Flow.from_client_secrets_file(
client_secrets_file="/src/app/config/google_client_secret.env.json",
scopes=[
"https://www.googleapis.com/auth/userinfo.email",
"openid",
"https://www.googleapis.com/auth/userinfo.profile",
],
redirect_uri=SignupConfig.oauth_base_url + "/oauth/google/callback",
)
except FileNotFoundError:
raise FileNotFoundError(
"Google OAuth Config File not found (google_client_secret.env.json). Please disable this OAuth Provider, or create the file as described in the Docs."
)


@router.get("/login")
Expand Down
45 changes: 33 additions & 12 deletions src/tools/conf/SignupConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,47 @@ class SignupConfig:
oauth_providers: list[str] = config["signup"]["oauth"]["providers_enabled"]
oauth_base_url: str = str(config["signup"]["oauth"]["base_url"]).removesuffix("/")

def validate(self) -> bool:
"""This is to Type Check the Configuration
Raises:
ValueError: _description_
ValueError: _description_
Returns:
bool: _description_
"""
if isinstance(self.enable_conf_email, bool):
def validate_types(self) -> bool:
"""This is to Type Check the Configuration"""
if not isinstance(self.enable_conf_email, bool):
raise ValueError(
"signup.enable_conf_email must be a boolean (got type {})".format(
type(self.enable_conf_email)
)
)
if isinstance(self.conf_code_expiry, int):
if not isinstance(self.conf_code_expiry, int):
raise ValueError(
"signup.conf_code_expiry must be an integer (got type {})".format(
type(self.conf_code_expiry)
)
)
if not isinstance(self.conf_code_complexity, int):
raise ValueError(
"signup.conf_code_complexity must be an integer (got type {})".format(
type(self.conf_code_complexity)
)
)
if not isinstance(self.enable_welcome_email, bool):
raise ValueError(
"signup.enable_welcome_email must be a boolean (got type {})".format(
type(self.enable_welcome_email)
)
)
if not isinstance(self.oauth_providers, list):
raise ValueError(
"signup.oauth.providers_enabled must be a list (got type {})".format(
type(self.oauth_providers)
)
)
if not all(isinstance(i, str) for i in self.oauth_providers):
raise ValueError("signup.oauth.providers_enabled must be a list of strings")
if not isinstance(self.oauth_base_url, str):
raise ValueError(
"signup.oauth.base_url must be a string (got type {})".format(
type(self.oauth_base_url)
)
)
pass


SignupConfig().validate_types()

0 comments on commit 1055a32

Please sign in to comment.