Skip to content

Commit b45a7f2

Browse files
feat(auth): add 𝘤𝘳𝘦𝘢𝘵𝘦_𝘢𝘶𝘵𝘩𝘦𝘯𝘵𝘪𝘤𝘢𝘵𝘪𝘰𝘯_𝘶𝘳𝘪 method
1️⃣ 𝘼𝙪𝙩𝙝 class now receives an optional param 𝘤𝘭𝘪𝘦𝘯𝘵_𝘴𝘦𝘤𝘳𝘦𝘵 which used to pass social providers client secret.
1 parent b62cf0b commit b45a7f2

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

firebase/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,19 @@ def __init__(self, config):
5151
if config.get("serviceAccount"):
5252
self.credentials = _service_account_creds_from_secret(config['serviceAccount'])
5353

54-
def auth(self):
54+
def auth(self, client_secret=None):
5555
"""Initializes and returns a new Firebase Authentication
5656
instance.
5757
58+
:type client_secret: str or dict
59+
:param client_secret: (Optional) Dict object from social
60+
client secret file, defaults to :data:`None`.
61+
5862
:return: A newly initialized instance of Auth.
5963
:rtype: Auth
6064
"""
6165

62-
return Auth(self.api_key, self.credentials, self.requests)
66+
return Auth(self.api_key, self.credentials, self.requests, client_secret=client_secret)
6367

6468
def database(self):
6569
"""Initializes and returns a new Firebase Realtime Database

firebase/auth/__init__.py

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,74 @@ class Auth:
3131
3232
:type requests: :class:`~requests.Session`
3333
:param requests: Session to make HTTP requests
34+
35+
:type client_secret: str or dict
36+
:param client_secret: (Optional) Dict object from social client
37+
secret file, defaults to :data:`None`.
3438
"""
3539

36-
def __init__(self, api_key, credentials, requests):
40+
def __init__(self, api_key, credentials, requests, client_secret=None):
3741
""" Constructor method """
3842

3943
self.api_key = api_key
4044
self.credentials = credentials
4145
self.requests = requests
4246

4347
self.current_user = None
48+
self.provider_id = None
49+
self.session_id = None
50+
51+
if client_secret:
52+
self.client_secret = client_secret
53+
54+
def create_authentication_uri(self, provider_id):
55+
""" Creates an authentication URI for the given social
56+
provider.
57+
58+
| For more details:
59+
| |section-fetch-providers-for-email|_
60+
61+
.. |section-fetch-providers-for-email| replace::
62+
Firebase Auth REST API | Fetch providers for email
63+
64+
.. _section-fetch-providers-for-email:
65+
https://firebase.google.com/docs/reference/rest/auth#section-fetch-providers-for-email
66+
67+
68+
:type provider_id: str
69+
:param provider_id: The IdP ID. For white listed IdPs it's a
70+
short domain name e.g. 'google.com', 'aol.com', 'live.net'
71+
and 'yahoo.com'. For other OpenID IdPs it's the OP
72+
identifier.
73+
74+
75+
:return: The URI used by the IDP to authenticate the user.
76+
:rtype: str
77+
"""
78+
79+
request_ref = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/createAuthUri?key={0}".format(self.api_key)
80+
81+
data = {
82+
"authFlowType": 'CODE_FLOW',
83+
"clientId": self.client_secret['client_id'],
84+
"providerId": provider_id,
85+
"continueUri": self.client_secret['redirect_uris'][0],
86+
"customParameter": {
87+
"access_type": 'offline',
88+
"prompt": 'select_account',
89+
"include_granted_scopes": 'true',
90+
}
91+
}
92+
93+
headers = {"content-type": "application/json; charset=UTF-8"}
94+
request_object = self.requests.post(request_ref, headers=headers, json=data)
95+
96+
raise_detailed_error(request_object)
97+
98+
self.provider_id = provider_id
99+
self.session_id = request_object.json()['sessionId']
100+
101+
return request_object.json()['authUri']
44102

45103
def sign_in_with_email_and_password(self, email, password):
46104
""" Sign in a user with an email and password.

0 commit comments

Comments
 (0)