From 10cb36214da6cc648d9e8a9dd1e9a6a3c762a008 Mon Sep 17 00:00:00 2001 From: freelancing-solutions Date: Mon, 17 Apr 2023 18:36:47 +0200 Subject: [PATCH] Adding SignIn With Google --- Dockerfile | 2 +- requirements.txt | Bin 3444 -> 4942 bytes src/config/config.py | 14 +++++++++ src/main.py | 2 ++ src/routes/authentication/dance.py | 20 +++++++++++++ src/static/build/css/custom.min.css | 41 +++++++++++++++++++++++++++ src/template/layouts/authlayout.html | 7 +++-- 7 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 src/routes/authentication/dance.py diff --git a/Dockerfile b/Dockerfile index 73eb4e90..3087e5f9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,7 +15,7 @@ COPY ./requirements.txt /var/www/app/requirements.txt RUN pip install -r /var/www/app/requirements.txt RUN pip install gunicorn COPY . /var/www/app -ENV PORT 8080 +ENV PORT 8000 EXPOSE $PORT # to be equal to the cores available. CMD exec gunicorn --bind :$PORT run:app --workers 2 --threads 8 --timeout 3600 diff --git a/requirements.txt b/requirements.txt index 07075009b3e371270609f283003202e519902b4c..789b38b63f003f2b041aa5e74ee36fe3ff092c15 100644 GIT binary patch delta 1464 zcmah}O=}Zj5T4b>s!7PHlvF|xKOl#cY(8o{Nn z`x6R6Z$f^A2f>pbM7-!v&>!G4Gg)_=q)6Be?94OIGtbQX{@MC`^TQV_Q>ASxbvP9eiU-CSp;x(3M157qMkTKohV$L&Ot&k6hqA zd<$#Tse(#NHKsy47I2tq(=DV{R?=x^5mY1m0tk0$A0ipcjj=CFBr%R6B(_MaFm}5C z4_Z5e)B%YPh#Jo%6^v;$O_aOrBKKcjY>NAjgW6Cx!ZXBsaR@BXUM#SH_aIYS(!u>t z=ClYC$AApw9gTp=Rv8%=t3nTejc_ZcJm0LLi1n$DpKeO9g+=qFS1rQxt5KcF&67l7 zI)FR}u+wYjqF^6iU&4=Q^4#&x+#kicsLgS$%I|QiqIglfX4l8_Mq=6KRic$YYuCzh zI7;SQ;%jaXiUxE5_kB9V;xj8-!HZHcCl`KYPw$=1NLIoet?ylB{-?kW&{rFKIhbqF zrGXo>kLxwdJWi)FQA2E#ZGK;T)3}fGcNY?x*wrW=PLNM$AOeRd?ZM1Y9&hvQ+2qo9 zYm@I62j@zUJ$oSM$uyFixmDWLO%~8d_HT5gJmdND@Sxt8ONEKJ4nI|MoIELn*Absx zz0o*_ed3C`-(&ROGY`&fq{C*^$7ZUKe?$-I4&B4bK8IEZzHyG;o$Pft)x6R|nP7YtF}@BJtGu%CCX>!zpI{PfQ~W|@{d%J6vSEJ#oo)K? delta 160 zcmX@7_C;#L6-HK520aGj%|98}Gj6`XEWtQgfHh!p4{OonSsY4}oj9W=AK<(&c@x*Y z$(y)cHY@PxFitMuZQ5+Yca3p!jzHMtcLIwhKM_=ztRZAKxkD&xvVgFZ8!rPFgD*oO zLlHwZLkdGaLpehpLmh)HgE7!16Oe5TAQb@&l??6-nG7Wi84RTiNnjO*Kv`1;3m|F8 F002GuD!%{# diff --git a/src/config/config.py b/src/config/config.py index d44dc518..a1d390fc 100644 --- a/src/config/config.py +++ b/src/config/config.py @@ -77,10 +77,23 @@ class Config: env_file_encoding = 'utf-8' +class GoogleSettings(BaseSettings): + GOOGLE_ANALYTICS_ID: str = Field(..., env="GOOGLE_ANALYTICS_ID") + GOOGLE_ANALYTICS_DOMAIN: str = Field(..., env="GOOGLE_ANALYTICS_DOMAIN") + GOOGLE_CLIENT_ID: str = Field(..., env="GOOGLE_CLIENT_ID") + GOOGLE_CLIENT_SECRET: str = Field(..., env="GOOGLE_CLIENT_SECRET") + + class Config: + case_sensitive = True + env_file = '.env.development' + env_file_encoding = 'utf-8' + + class Settings(BaseSettings): EMAIL_SETTINGS: EmailSettings = EmailSettings() CELERY_SETTINGS = CelerySettings() SECRET_KEY: str = Field(..., env="SECRET_TOKEN") + DATABASE_SETTINGS: DatabaseSettings = DatabaseSettings() DEVELOPMENT_SERVER_NAME: str = Field(default="DESKTOP-T9V7F59") LOGGING: Logging = Logging() @@ -89,6 +102,7 @@ class Settings(BaseSettings): SERVER_NAME: str = Field(default_factory=get_server_name) APPLICATION_ROOT: str = Field(default="/") PREFERRED_URL_SCHEME: str = Field(default="https://") + GOOGLE_SETTINGS: GoogleSettings = GoogleSettings() GITHUB_SETTINGS: GithubSettings = GithubSettings() SEARCH_CONSOLE_API_KEY: str = Field(..., env="SEARCH_CONSOLE_API_KEY") EOD_STOCK_API_KEY: str = Field(..., env="EOD_STOCK_API_KEY") diff --git a/src/main.py b/src/main.py index 05a51b78..4fe4f782 100644 --- a/src/main.py +++ b/src/main.py @@ -6,6 +6,7 @@ from src.config import config_instance from src.exceptions import UnAuthenticatedError from src.logger import init_logger +from src.routes.authentication.dance import google_dance from src.routes.blog.github import GithubBlog user_session = {} @@ -62,6 +63,7 @@ def create_app(config=config_instance()) -> Flask: app.register_blueprint(plan_routes) app.register_blueprint(sitemap_bp) app.register_blueprint(github_blog_route) + app.register_blueprint(google_dance) # Handle API Errors, all errors are re raised as HTTPException from src.exceptions import (InvalidSignatureError, ServerInternalError, UnresponsiveServer) diff --git a/src/routes/authentication/dance.py b/src/routes/authentication/dance.py new file mode 100644 index 00000000..f39db193 --- /dev/null +++ b/src/routes/authentication/dance.py @@ -0,0 +1,20 @@ +from flask import Flask, redirect, url_for +from flask_dance.contrib.google import make_google_blueprint, google + +from src.config import config_instance + +app = Flask(__name__) +app.secret_key = config_instance().SECRET_KEY + +google_dance = make_google_blueprint(client_id=config_instance().GOOGLE_SETTINGS.GOOGLE_CLIENT_ID, + client_secret=config_instance().GOOGLE_SETTINGS.GOOGLE_CLIENT_SECRET, + scope=["profile", "email"]) + + +@google_dance.route("/login/google") +def login_google(): + if not google.authorized: + return redirect(url_for("google.login")) + resp = google.get("/oauth2/v2/userinfo") + assert resp.ok, resp.text + return "You are {email} on Google".format(email=resp.json()["email"]) \ No newline at end of file diff --git a/src/static/build/css/custom.min.css b/src/static/build/css/custom.min.css index 69b05af5..edc71006 100644 --- a/src/static/build/css/custom.min.css +++ b/src/static/build/css/custom.min.css @@ -6020,3 +6020,44 @@ ul.notifications { #slash { display: none; } + +.btn-google { + background-color: #00243F; + border: none; + color: floralwhite; + padding: 10px 20px; + text-align: center; + text-decoration: none; + display: inline-block; + font-size: 16px; + border-radius: 50px; +} + +.btn-google:hover { + background-color: #357AE8; +} + +.btn-google i { + margin-right: 10px; +} + +.btn-email { + background-color: #88000F; + border: none; + color: floralwhite; + padding: 10px 20px; + text-align: center; + text-decoration: none; + display: inline-block; + font-size: 16px; + border-radius: 50px; +} + +.btn-email:hover { + background-color: #8a4182; +} + +.btn-email i { + margin-right: 10px; +} + diff --git a/src/template/layouts/authlayout.html b/src/template/layouts/authlayout.html index 734619bb..0546b7cf 100644 --- a/src/template/layouts/authlayout.html +++ b/src/template/layouts/authlayout.html @@ -53,8 +53,11 @@

Login

- Log in - Create Account? + Sign in (Email) + Sign in with Google + + Create Account +