Skip to content

Commit

Permalink
Show appropriate message when unauthorized user tried to access the a… (
Browse files Browse the repository at this point in the history
#571)

* Show appropriate message when unauthorized user tried to access the application

* Fix firebase logout
  • Loading branch information
harishmohanraj authored Nov 13, 2024
1 parent b648f11 commit 432f5a0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
21 changes: 12 additions & 9 deletions fastagency/ui/mesop/auth/firebase/firebase_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@


class FirebaseAuth: # implements AuthProtocol
SIGN_IN_MESSAGE = "Sign in to your account"
UN_AUTHORIZED_ERROR_MESSAGE = """You are not authorized to access this application. Please contact the application administrators for access."""

def __init__(
self,
sign_in_methods: list[Literal["google"]],
Expand Down Expand Up @@ -147,17 +150,16 @@ def on_auth_changed(self, e: mel.WebEvent) -> None:

if not firebase_auth_token:
state.authenticated_user = ""
state.auth_error = None
return

decoded_token = auth.verify_id_token(firebase_auth_token)

if not self.is_authorized(decoded_token):
raise me.MesopUserException(
"You are not authorized to access this application. "
"Please contact the application administrators for access."
)

state.authenticated_user = decoded_token["email"]
if self.is_authorized(decoded_token):
state.authenticated_user = decoded_token["email"]
state.auth_error = None
else:
state.authenticated_user = ""
state.auth_error = FirebaseAuth.UN_AUTHORIZED_ERROR_MESSAGE

# maybe me.Component is wrong
def auth_component(self) -> me.component:
Expand All @@ -171,7 +173,8 @@ def auth_component(self) -> me.component:
else:
with me.box(style=styles.login_box): # noqa: SIM117
with me.box(style=styles.login_btn_container):
me.text("Sign in to your account", style=styles.header_text)
message = state.auth_error or FirebaseAuth.SIGN_IN_MESSAGE
me.text(message, style=styles.header_text)
firebase_auth_component(
on_auth_changed=self.on_auth_changed, config=self.config
)
1 change: 1 addition & 0 deletions fastagency/ui/mesop/data_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ class State:
available_workflows_initialized = False
available_workflows_exception = False
authenticated_user: Optional[str] = None
auth_error: Optional[str] = None
6 changes: 3 additions & 3 deletions javascript/firebase_auth_component.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ class FirebaseAuthComponent extends LitElement {
></div>
<div
class="firebaseui-container firebaseui-page-provider-sign-in firebaseui-id-page-provider-sign-in firebaseui-use-spinner"
style="${this.isSignedIn ? "" : "display: none"}"
style="${this.isSignedIn ? "" : "display: none"} ; text-align:center"
>
<button
style="background-color:#ffffff"
class="firebaseui-idp-button mdl-button mdl-js-button mdl-button--raised firebaseui-idp-google firebaseui-id-idp-button"
style="background-color:#ffffff;margin-top:10px;"
class=" mdl-button mdl-js-button mdl-button--raised firebaseui-idp-google firebaseui-id-idp-button"
@click="${this.signOut}"
>
<span
Expand Down

0 comments on commit 432f5a0

Please sign in to comment.