Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update on allowing user to add credentials from config #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions src/streamlit_google_auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,37 @@
# https://github.com/mkhorasani/Streamlit-Authenticator

import os
import json
import time
import streamlit as st
from typing import Literal
import google_auth_oauthlib.flow
from googleapiclient.discovery import build


from .cookie import CookieHandler

class Authenticate:
def __init__(self, secret_credentials_path:str, redirect_uri: str, cookie_name: str, cookie_key: str, cookie_expiry_days: float=30.0):
def __init__(self, redirect_uri:str, cookie_name: str, cookie_key: str, cookie_expiry_days: float=30.0):
st.session_state['connected'] = st.session_state.get('connected', False)
self.secret_credentials_path = secret_credentials_path
self.secret_credentials_config = None
self.redirect_uri = redirect_uri
self.cookie_handler = CookieHandler(cookie_name,
cookie_key,
cookie_expiry_days)


def load_credentials_from_json(self,secret_credentials_path:str):
with open(secret_credentials_path, "r") as json_file:
self.secret_credentials_config = json.load(json_file)


def load_credentials(self, secret_credentials_config:dict):
self.secret_credentials_config = secret_credentials_config


def get_authorization_url(self) -> str:
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
self.secret_credentials_path, # replace with you json credentials from your google auth app
flow = google_auth_oauthlib.flow.Flow.from_client_config(
self.secret_credentials_config, # replace with you json credentials from your google auth app
scopes=["openid", "https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/userinfo.email"],
redirect_uri=self.redirect_uri,
)
Expand All @@ -31,11 +42,12 @@ def get_authorization_url(self) -> str:
include_granted_scopes="true",
)
return authorization_url


def login(self, color:Literal['white', 'blue']='blue', justify_content: str="center") -> tuple:
if not st.session_state['connected']:
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
self.secret_credentials_path, # replace with you json credentials from your google auth app
flow = google_auth_oauthlib.flow.Flow.from_client_config(
self.secret_credentials_config, # replace with you json credentials from your google auth app
scopes=["openid", "https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/userinfo.email"],
redirect_uri=self.redirect_uri,
)
Expand Down Expand Up @@ -78,8 +90,8 @@ def check_authentification(self):
auth_code = st.query_params.get("code")
st.query_params.clear()
if auth_code:
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
self.secret_credentials_path, # replace with you json credentials from your google auth app
flow = google_auth_oauthlib.flow.Flow.from_client_config(
self.secret_credentials_config, # replace with you json credentials from your google auth app
scopes=["openid", "https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/userinfo.email"],
redirect_uri=self.redirect_uri,
)
Expand Down