pyios-firebase 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyios_firebase/__init__.py +26 -0
- pyios_firebase/analytics/__init__.py +434 -0
- pyios_firebase/analytics/names.py +251 -0
- pyios_firebase/app_check/__init__.py +483 -0
- pyios_firebase/auth/__init__.py +131 -0
- pyios_firebase/auth/_apple_name_cache.py +97 -0
- pyios_firebase/auth/_common.py +121 -0
- pyios_firebase/auth/_ui.py +21 -0
- pyios_firebase/auth/apple.py +326 -0
- pyios_firebase/auth/auth.py +592 -0
- pyios_firebase/auth/credentials.py +551 -0
- pyios_firebase/auth/errors.py +321 -0
- pyios_firebase/auth/facebook.py +240 -0
- pyios_firebase/auth/google.py +167 -0
- pyios_firebase/auth/multi_factor.py +394 -0
- pyios_firebase/auth/results.py +361 -0
- pyios_firebase/auth/user.py +457 -0
- pyios_firebase/core/__init__.py +32 -0
- pyios_firebase/crashlytics/__init__.py +463 -0
- pyios_firebase/database/__init__.py +62 -0
- pyios_firebase/database/database.py +144 -0
- pyios_firebase/database/errors.py +80 -0
- pyios_firebase/database/query.py +569 -0
- pyios_firebase/database/reference.py +428 -0
- pyios_firebase/database/snapshot.py +218 -0
- pyios_firebase/database/values.py +271 -0
- pyios_firebase/firestore/__init__.py +59 -0
- pyios_firebase/firestore/_limits.py +48 -0
- pyios_firebase/firestore/_query_state.py +228 -0
- pyios_firebase/firestore/_write.py +18 -0
- pyios_firebase/firestore/aggregate.py +137 -0
- pyios_firebase/firestore/collection_reference.py +100 -0
- pyios_firebase/firestore/document_change.py +63 -0
- pyios_firebase/firestore/document_reference.py +229 -0
- pyios_firebase/firestore/document_snapshot.py +102 -0
- pyios_firebase/firestore/enums.py +66 -0
- pyios_firebase/firestore/field_value.py +94 -0
- pyios_firebase/firestore/filter.py +319 -0
- pyios_firebase/firestore/firestore.py +184 -0
- pyios_firebase/firestore/listener_registration.py +55 -0
- pyios_firebase/firestore/query.py +679 -0
- pyios_firebase/firestore/query_snapshot.py +103 -0
- pyios_firebase/firestore/snapshot_metadata.py +36 -0
- pyios_firebase/firestore/transaction.py +130 -0
- pyios_firebase/firestore/write_batch.py +125 -0
- pyios_firebase/functions/__init__.py +329 -0
- pyios_firebase/messaging/__init__.py +37 -0
- pyios_firebase/messaging/messaging.py +435 -0
- pyios_firebase/messaging/notifications.py +292 -0
- pyios_firebase/performance/__init__.py +486 -0
- pyios_firebase/py.typed +0 -0
- pyios_firebase/remote_config/__init__.py +660 -0
- pyios_firebase/storage/__init__.py +41 -0
- pyios_firebase/storage/errors.py +103 -0
- pyios_firebase/storage/metadata.py +202 -0
- pyios_firebase/storage/reference.py +402 -0
- pyios_firebase/storage/storage.py +120 -0
- pyios_firebase/storage/task.py +198 -0
- pyios_firebase/types.py +93 -0
- pyios_firebase/util.py +413 -0
- pyios_firebase-0.2.0.dist-info/METADATA +1969 -0
- pyios_firebase-0.2.0.dist-info/RECORD +65 -0
- pyios_firebase-0.2.0.dist-info/WHEEL +5 -0
- pyios_firebase-0.2.0.dist-info/licenses/LICENSE +21 -0
- pyios_firebase-0.2.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,551 @@
|
|
|
1
|
+
"""Sign-in credentials and the providers that make them.
|
|
2
|
+
|
|
3
|
+
A credential proves who the user is to one provider. Pass it to
|
|
4
|
+
``Auth.sign_in_with_credential``, ``User.link_with_credential`` or
|
|
5
|
+
``User.reauthenticate_with_credential``::
|
|
6
|
+
|
|
7
|
+
credential = EmailAuthProvider.credential("ada@example.com", "s3cret!")
|
|
8
|
+
Auth.auth().sign_in_with_credential(credential, on_signed_in)
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
__all__ = (
|
|
14
|
+
"AuthCredential",
|
|
15
|
+
"EmailAuthProvider",
|
|
16
|
+
"FacebookAuthProvider",
|
|
17
|
+
"GameCenterAuthProvider",
|
|
18
|
+
"GitHubAuthProvider",
|
|
19
|
+
"GoogleAuthProvider",
|
|
20
|
+
"OAuthCredential",
|
|
21
|
+
"OAuthProvider",
|
|
22
|
+
"PhoneAuthProvider",
|
|
23
|
+
"TwitterAuthProvider",
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
from collections.abc import Callable, Mapping, Sequence
|
|
27
|
+
from typing import TYPE_CHECKING, Final
|
|
28
|
+
|
|
29
|
+
from rubicon.objc import ns_from_py
|
|
30
|
+
|
|
31
|
+
from ..types import ObjCObject
|
|
32
|
+
from ..util import objc_class, to_python
|
|
33
|
+
from ._common import missing_callback_scheme, result_block, string_block, text
|
|
34
|
+
from .errors import AuthError, simple_auth_error
|
|
35
|
+
|
|
36
|
+
if TYPE_CHECKING:
|
|
37
|
+
from .auth import Auth
|
|
38
|
+
from .multi_factor import MultiFactorSession, PhoneMultiFactorInfo
|
|
39
|
+
|
|
40
|
+
CredentialCallback = Callable[["AuthCredential | None", AuthError | None], None]
|
|
41
|
+
"""``(credential, error)``: exactly one of them is ``None``."""
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class AuthCredential:
|
|
45
|
+
"""A sign-in credential for one provider.
|
|
46
|
+
|
|
47
|
+
Make credentials with the provider classes (``EmailAuthProvider``,
|
|
48
|
+
``GoogleAuthProvider``, ...); you do not create them yourself.
|
|
49
|
+
|
|
50
|
+
Args:
|
|
51
|
+
native: the native ``FIRAuthCredential``.
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
def __init__(self, native: ObjCObject) -> None:
|
|
55
|
+
self._native = native
|
|
56
|
+
|
|
57
|
+
@property
|
|
58
|
+
def native(self) -> ObjCObject:
|
|
59
|
+
"""The ``FIRAuthCredential``, for SDK calls this wrapper does not cover."""
|
|
60
|
+
return self._native
|
|
61
|
+
|
|
62
|
+
@property
|
|
63
|
+
def provider(self) -> str:
|
|
64
|
+
"""The provider ID, for example ``"password"`` or ``"google.com"``."""
|
|
65
|
+
return str(self._native.provider)
|
|
66
|
+
|
|
67
|
+
def __repr__(self) -> str:
|
|
68
|
+
return f"{type(self).__name__}(provider={self.provider!r})"
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class OAuthCredential(AuthCredential):
|
|
72
|
+
"""A credential from an OAuth provider (Apple, Microsoft, Yahoo, ...).
|
|
73
|
+
|
|
74
|
+
Args:
|
|
75
|
+
native: the native ``FIROAuthCredential``.
|
|
76
|
+
"""
|
|
77
|
+
|
|
78
|
+
@property
|
|
79
|
+
def id_token(self) -> str | None:
|
|
80
|
+
"""The OpenID Connect ID token, if the provider gave one."""
|
|
81
|
+
return text(self._native.IDToken)
|
|
82
|
+
|
|
83
|
+
@property
|
|
84
|
+
def access_token(self) -> str | None:
|
|
85
|
+
"""The OAuth access token, if the provider gave one."""
|
|
86
|
+
return text(self._native.accessToken)
|
|
87
|
+
|
|
88
|
+
@property
|
|
89
|
+
def secret(self) -> str | None:
|
|
90
|
+
"""The OAuth 1.0 token secret, if the provider gave one."""
|
|
91
|
+
return text(self._native.secret)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _wrap_credential(native: ObjCObject) -> AuthCredential:
|
|
95
|
+
if native.isKindOfClass(objc_class("FIROAuthCredential")):
|
|
96
|
+
return OAuthCredential(native)
|
|
97
|
+
return AuthCredential(native)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
# ---- providers ------------------------------------------------------------------
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
class EmailAuthProvider:
|
|
104
|
+
"""Credentials for email and password, or email link, sign-in."""
|
|
105
|
+
|
|
106
|
+
PROVIDER_ID: Final = "password"
|
|
107
|
+
"""The provider ID of email sign-in (both password and link)."""
|
|
108
|
+
|
|
109
|
+
@staticmethod
|
|
110
|
+
def credential(email: str, password: str) -> AuthCredential:
|
|
111
|
+
"""A credential for an email address and password.
|
|
112
|
+
|
|
113
|
+
Args:
|
|
114
|
+
email: the email address.
|
|
115
|
+
password: the password.
|
|
116
|
+
|
|
117
|
+
Returns:
|
|
118
|
+
An ``AuthCredential`` for ``"password"``.
|
|
119
|
+
"""
|
|
120
|
+
return AuthCredential(
|
|
121
|
+
objc_class("FIREmailAuthProvider").credentialWithEmail_password_(
|
|
122
|
+
email, password
|
|
123
|
+
)
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
@staticmethod
|
|
127
|
+
def credential_with_link(email: str, link: str) -> AuthCredential:
|
|
128
|
+
"""A credential for an email address and a sign-in link sent to it.
|
|
129
|
+
|
|
130
|
+
Args:
|
|
131
|
+
email: the email address the link was sent to.
|
|
132
|
+
link: the full sign-in link the user opened.
|
|
133
|
+
|
|
134
|
+
Returns:
|
|
135
|
+
An ``AuthCredential`` for ``"password"``.
|
|
136
|
+
"""
|
|
137
|
+
return AuthCredential(
|
|
138
|
+
objc_class("FIREmailAuthProvider").credentialWithEmail_link_(email, link)
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
class GoogleAuthProvider:
|
|
143
|
+
"""Credentials from Google Sign-In tokens. For the whole flow, see
|
|
144
|
+
``pyios_firebase.auth.google``."""
|
|
145
|
+
|
|
146
|
+
PROVIDER_ID: Final = "google.com"
|
|
147
|
+
"""The provider ID of Google sign-in."""
|
|
148
|
+
|
|
149
|
+
@staticmethod
|
|
150
|
+
def credential(id_token: str, access_token: str) -> AuthCredential:
|
|
151
|
+
"""A credential from the tokens Google Sign-In gives.
|
|
152
|
+
|
|
153
|
+
Args:
|
|
154
|
+
id_token: the Google ID token (``GIDGoogleUser.idToken.tokenString``).
|
|
155
|
+
access_token: the Google access token
|
|
156
|
+
(``GIDGoogleUser.accessToken.tokenString``).
|
|
157
|
+
|
|
158
|
+
Returns:
|
|
159
|
+
An ``AuthCredential`` for ``"google.com"``.
|
|
160
|
+
"""
|
|
161
|
+
return AuthCredential(
|
|
162
|
+
objc_class("FIRGoogleAuthProvider").credentialWithIDToken_accessToken_(
|
|
163
|
+
id_token, access_token
|
|
164
|
+
)
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
class FacebookAuthProvider:
|
|
169
|
+
"""Credentials from Facebook Login access tokens."""
|
|
170
|
+
|
|
171
|
+
PROVIDER_ID: Final = "facebook.com"
|
|
172
|
+
"""The provider ID of Facebook sign-in."""
|
|
173
|
+
|
|
174
|
+
@staticmethod
|
|
175
|
+
def credential(access_token: str) -> AuthCredential:
|
|
176
|
+
"""A credential from a Facebook access token.
|
|
177
|
+
|
|
178
|
+
Args:
|
|
179
|
+
access_token: the access token Facebook Login gave.
|
|
180
|
+
|
|
181
|
+
Returns:
|
|
182
|
+
An ``AuthCredential`` for ``"facebook.com"``.
|
|
183
|
+
"""
|
|
184
|
+
return AuthCredential(
|
|
185
|
+
objc_class("FIRFacebookAuthProvider").credentialWithAccessToken_(
|
|
186
|
+
access_token
|
|
187
|
+
)
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
class GitHubAuthProvider:
|
|
192
|
+
"""Credentials from GitHub OAuth access tokens (for the web flow, use
|
|
193
|
+
``OAuthProvider("github.com")``)."""
|
|
194
|
+
|
|
195
|
+
PROVIDER_ID: Final = "github.com"
|
|
196
|
+
"""The provider ID of GitHub sign-in."""
|
|
197
|
+
|
|
198
|
+
@staticmethod
|
|
199
|
+
def credential(token: str) -> AuthCredential:
|
|
200
|
+
"""A credential from a GitHub OAuth access token.
|
|
201
|
+
|
|
202
|
+
Args:
|
|
203
|
+
token: the GitHub access token.
|
|
204
|
+
|
|
205
|
+
Returns:
|
|
206
|
+
An ``AuthCredential`` for ``"github.com"``.
|
|
207
|
+
"""
|
|
208
|
+
return AuthCredential(
|
|
209
|
+
objc_class("FIRGitHubAuthProvider").credentialWithToken_(token)
|
|
210
|
+
)
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
class TwitterAuthProvider:
|
|
214
|
+
"""Credentials from Twitter (X) OAuth 1.0 tokens."""
|
|
215
|
+
|
|
216
|
+
PROVIDER_ID: Final = "twitter.com"
|
|
217
|
+
"""The provider ID of Twitter sign-in."""
|
|
218
|
+
|
|
219
|
+
@staticmethod
|
|
220
|
+
def credential(token: str, secret: str) -> AuthCredential:
|
|
221
|
+
"""A credential from a Twitter OAuth token and secret.
|
|
222
|
+
|
|
223
|
+
Args:
|
|
224
|
+
token: the OAuth token.
|
|
225
|
+
secret: the OAuth token secret.
|
|
226
|
+
|
|
227
|
+
Returns:
|
|
228
|
+
An ``AuthCredential`` for ``"twitter.com"``.
|
|
229
|
+
"""
|
|
230
|
+
return AuthCredential(
|
|
231
|
+
objc_class("FIRTwitterAuthProvider").credentialWithToken_secret_(
|
|
232
|
+
token, secret
|
|
233
|
+
)
|
|
234
|
+
)
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
_NOT_GENERIC: Final = {
|
|
238
|
+
"facebook.com": "Facebook sign-in needs the Facebook SDK: pass its access token to "
|
|
239
|
+
"FacebookAuthProvider.credential",
|
|
240
|
+
"apple.com": "Sign in with Apple needs Apple's sheet: use pyios_firebase.auth.apple, "
|
|
241
|
+
"or OAuthProvider.apple_credential with Apple's identity token",
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
class OAuthProvider:
|
|
246
|
+
"""A generic OAuth provider: Apple, Microsoft, Yahoo, GitHub, or any OIDC /
|
|
247
|
+
SAML provider set up in the Firebase console.
|
|
248
|
+
|
|
249
|
+
As an object, it runs the provider's web sign-in flow
|
|
250
|
+
(``Auth.sign_in_with_provider``, ``get_credential``). The static methods
|
|
251
|
+
make credentials from tokens you already have.
|
|
252
|
+
|
|
253
|
+
::
|
|
254
|
+
|
|
255
|
+
microsoft = OAuthProvider("microsoft.com", scopes=["mail.read"])
|
|
256
|
+
Auth.auth().sign_in_with_provider(microsoft, on_signed_in)
|
|
257
|
+
|
|
258
|
+
Args:
|
|
259
|
+
provider_id: the provider ID, for example ``"microsoft.com"``,
|
|
260
|
+
``"yahoo.com"``, ``"github.com"``, ``"oidc.my-provider"``. Not
|
|
261
|
+
``"facebook.com"`` or ``"apple.com"`` (except on the emulator):
|
|
262
|
+
those need their own SDKs.
|
|
263
|
+
scopes: OAuth scopes to ask for, beyond the provider's defaults.
|
|
264
|
+
custom_parameters: extra OAuth parameters, for example
|
|
265
|
+
``{"prompt": "consent"}`` or ``{"tenant": "my-tenant-id"}``.
|
|
266
|
+
auth: the ``Auth`` instance to use (default: ``Auth.auth()``).
|
|
267
|
+
|
|
268
|
+
Raises:
|
|
269
|
+
ValueError: ``provider_id`` is ``"facebook.com"`` or ``"apple.com"``
|
|
270
|
+
outside the emulator (the SDK would stop the app).
|
|
271
|
+
"""
|
|
272
|
+
|
|
273
|
+
APPLE_PROVIDER_ID: Final = "apple.com"
|
|
274
|
+
"""The provider ID of Sign in with Apple."""
|
|
275
|
+
|
|
276
|
+
def __init__(
|
|
277
|
+
self,
|
|
278
|
+
provider_id: str,
|
|
279
|
+
scopes: Sequence[str] | None = None,
|
|
280
|
+
custom_parameters: Mapping[str, str] | None = None,
|
|
281
|
+
auth: Auth | None = None,
|
|
282
|
+
) -> None:
|
|
283
|
+
from .auth import Auth
|
|
284
|
+
|
|
285
|
+
self._auth = auth if auth is not None else Auth.auth()
|
|
286
|
+
if provider_id in _NOT_GENERIC and not self._auth.is_using_emulator:
|
|
287
|
+
# the SDK stops the app for these: say what to use instead
|
|
288
|
+
raise ValueError(_NOT_GENERIC[provider_id])
|
|
289
|
+
fir = objc_class("FIROAuthProvider")
|
|
290
|
+
if auth is None:
|
|
291
|
+
self._native: ObjCObject = fir.providerWithProviderID_(provider_id)
|
|
292
|
+
else:
|
|
293
|
+
self._native = fir.providerWithProviderID_auth_(provider_id, auth.native)
|
|
294
|
+
if scopes is not None:
|
|
295
|
+
self.scopes = list(scopes)
|
|
296
|
+
if custom_parameters is not None:
|
|
297
|
+
self.custom_parameters = dict(custom_parameters)
|
|
298
|
+
|
|
299
|
+
@property
|
|
300
|
+
def native(self) -> ObjCObject:
|
|
301
|
+
"""The ``FIROAuthProvider``."""
|
|
302
|
+
return self._native
|
|
303
|
+
|
|
304
|
+
@property
|
|
305
|
+
def provider_id(self) -> str:
|
|
306
|
+
"""The provider ID."""
|
|
307
|
+
return str(self._native.providerID)
|
|
308
|
+
|
|
309
|
+
@property
|
|
310
|
+
def scopes(self) -> list[str]:
|
|
311
|
+
"""The OAuth scopes asked for (beyond the provider's defaults)."""
|
|
312
|
+
scopes = self._native.scopes
|
|
313
|
+
return [] if scopes is None else [str(scope) for scope in scopes]
|
|
314
|
+
|
|
315
|
+
@scopes.setter
|
|
316
|
+
def scopes(self, scopes: Sequence[str]) -> None:
|
|
317
|
+
self._native.scopes = ns_from_py(list(scopes))
|
|
318
|
+
|
|
319
|
+
@property
|
|
320
|
+
def custom_parameters(self) -> dict[str, str]:
|
|
321
|
+
"""Extra OAuth parameters sent to the provider."""
|
|
322
|
+
params = self._native.customParameters
|
|
323
|
+
return (
|
|
324
|
+
{}
|
|
325
|
+
if params is None
|
|
326
|
+
else {str(k): str(v) for k, v in to_python(params).items()}
|
|
327
|
+
)
|
|
328
|
+
|
|
329
|
+
@custom_parameters.setter
|
|
330
|
+
def custom_parameters(self, params: Mapping[str, str]) -> None:
|
|
331
|
+
self._native.customParameters = ns_from_py(dict(params))
|
|
332
|
+
|
|
333
|
+
def get_credential(self, callback: CredentialCallback) -> None:
|
|
334
|
+
"""Run the provider's web sign-in flow and give back the credential,
|
|
335
|
+
without signing in (for example to link it to the current user).
|
|
336
|
+
|
|
337
|
+
Shows the provider's page in an in-app browser. The app's
|
|
338
|
+
``Info.plist`` needs the URL scheme Firebase documents for the provider.
|
|
339
|
+
|
|
340
|
+
Args:
|
|
341
|
+
callback: gets ``(credential, None)``, or ``(None, error)``.
|
|
342
|
+
``error.cancelled`` is True when the user closed the browser.
|
|
343
|
+
If the callback URL scheme is missing from ``Info.plist``, the
|
|
344
|
+
error says which one to add (the SDK itself would stop the app).
|
|
345
|
+
"""
|
|
346
|
+
problem = missing_callback_scheme(self._auth.native)
|
|
347
|
+
if problem is not None:
|
|
348
|
+
callback(None, simple_auth_error(problem))
|
|
349
|
+
return
|
|
350
|
+
self._native.getCredentialWithUIDelegate_completion_(
|
|
351
|
+
None, result_block(callback, _wrap_credential)
|
|
352
|
+
)
|
|
353
|
+
|
|
354
|
+
@staticmethod
|
|
355
|
+
def credential(
|
|
356
|
+
provider_id: str,
|
|
357
|
+
id_token: str | None = None,
|
|
358
|
+
access_token: str | None = None,
|
|
359
|
+
raw_nonce: str | None = None,
|
|
360
|
+
) -> OAuthCredential:
|
|
361
|
+
"""A credential from tokens you already have.
|
|
362
|
+
|
|
363
|
+
Args:
|
|
364
|
+
provider_id: the provider ID, for example ``"microsoft.com"``.
|
|
365
|
+
id_token: the OpenID Connect ID token, if any.
|
|
366
|
+
access_token: the OAuth access token, if any.
|
|
367
|
+
raw_nonce: the raw (unhashed) nonce the ID token was made for, if any.
|
|
368
|
+
|
|
369
|
+
Returns:
|
|
370
|
+
An ``OAuthCredential``.
|
|
371
|
+
|
|
372
|
+
Raises:
|
|
373
|
+
ValueError: neither ``id_token`` nor ``access_token`` is given, or
|
|
374
|
+
``raw_nonce`` is given without ``id_token``.
|
|
375
|
+
"""
|
|
376
|
+
# check the arguments before touching the SDK
|
|
377
|
+
if id_token is None and access_token is None:
|
|
378
|
+
raise ValueError("give an id_token, an access_token, or both")
|
|
379
|
+
if raw_nonce is not None and id_token is None:
|
|
380
|
+
raise ValueError("raw_nonce needs an id_token")
|
|
381
|
+
fir = objc_class("FIROAuthProvider")
|
|
382
|
+
if id_token is not None and raw_nonce is not None:
|
|
383
|
+
if access_token is not None:
|
|
384
|
+
native = fir.credentialWithProviderID_IDToken_rawNonce_accessToken_(
|
|
385
|
+
provider_id, id_token, raw_nonce, access_token
|
|
386
|
+
)
|
|
387
|
+
else:
|
|
388
|
+
native = fir.credentialWithProviderID_IDToken_rawNonce_(
|
|
389
|
+
provider_id, id_token, raw_nonce
|
|
390
|
+
)
|
|
391
|
+
elif id_token is not None:
|
|
392
|
+
native = fir.credentialWithProviderID_IDToken_accessToken_(
|
|
393
|
+
provider_id, id_token, access_token
|
|
394
|
+
)
|
|
395
|
+
else:
|
|
396
|
+
native = fir.credentialWithProviderID_accessToken_(
|
|
397
|
+
provider_id, access_token
|
|
398
|
+
)
|
|
399
|
+
return OAuthCredential(native)
|
|
400
|
+
|
|
401
|
+
@staticmethod
|
|
402
|
+
def apple_credential(
|
|
403
|
+
id_token: str,
|
|
404
|
+
raw_nonce: str | None,
|
|
405
|
+
given_name: str | None = None,
|
|
406
|
+
family_name: str | None = None,
|
|
407
|
+
) -> OAuthCredential:
|
|
408
|
+
"""A Sign in with Apple credential. For the whole flow, see
|
|
409
|
+
``pyios_firebase.auth.apple``.
|
|
410
|
+
|
|
411
|
+
Args:
|
|
412
|
+
id_token: the identity token Apple gave (as a string).
|
|
413
|
+
raw_nonce: the raw nonce whose SHA-256 was sent to Apple, or ``None``.
|
|
414
|
+
given_name: the user's given name, which Apple shares only on the
|
|
415
|
+
first sign-in. Passing it saves it in Firebase.
|
|
416
|
+
family_name: the user's family name (same as ``given_name``).
|
|
417
|
+
|
|
418
|
+
Returns:
|
|
419
|
+
An ``OAuthCredential`` for ``"apple.com"``.
|
|
420
|
+
"""
|
|
421
|
+
full_name = None
|
|
422
|
+
if given_name or family_name:
|
|
423
|
+
full_name = objc_class("NSPersonNameComponents").alloc().init()
|
|
424
|
+
full_name.givenName = given_name or None
|
|
425
|
+
full_name.familyName = family_name or None
|
|
426
|
+
return OAuthCredential(
|
|
427
|
+
objc_class(
|
|
428
|
+
"FIROAuthProvider"
|
|
429
|
+
).appleCredentialWithIDToken_rawNonce_fullName_(
|
|
430
|
+
id_token, raw_nonce, full_name
|
|
431
|
+
)
|
|
432
|
+
)
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
class PhoneAuthProvider:
|
|
436
|
+
"""Phone number sign-in: send a code by SMS, then make a credential from it.
|
|
437
|
+
|
|
438
|
+
::
|
|
439
|
+
|
|
440
|
+
phone = PhoneAuthProvider()
|
|
441
|
+
phone.verify_phone_number("+15555550100", on_code_sent)
|
|
442
|
+
# later, with the code the user typed:
|
|
443
|
+
credential = phone.credential(verification_id, "123456")
|
|
444
|
+
|
|
445
|
+
Real phone sign-in needs push notifications (APNs) or reCAPTCHA set up; see
|
|
446
|
+
Firebase's phone auth guide. With ``Auth.use_emulator`` it works without
|
|
447
|
+
them. Either way, the app's ``Info.plist`` needs the callback URL scheme:
|
|
448
|
+
the reversed client ID, or ``app-`` + the GOOGLE_APP_ID with ``-`` for ``:``.
|
|
449
|
+
|
|
450
|
+
Args:
|
|
451
|
+
auth: the ``Auth`` instance to use (default: ``Auth.auth()``).
|
|
452
|
+
"""
|
|
453
|
+
|
|
454
|
+
PROVIDER_ID: Final = "phone"
|
|
455
|
+
"""The provider ID of phone sign-in."""
|
|
456
|
+
|
|
457
|
+
def __init__(self, auth: Auth | None = None) -> None:
|
|
458
|
+
fir = objc_class("FIRPhoneAuthProvider")
|
|
459
|
+
self._native: ObjCObject = (
|
|
460
|
+
fir.provider() if auth is None else fir.providerWithAuth_(auth.native)
|
|
461
|
+
)
|
|
462
|
+
self._auth_native: ObjCObject = (
|
|
463
|
+
objc_class("FIRAuth").auth() if auth is None else auth.native
|
|
464
|
+
)
|
|
465
|
+
|
|
466
|
+
def verify_phone_number(
|
|
467
|
+
self,
|
|
468
|
+
phone_number: str,
|
|
469
|
+
callback: Callable[[str | None, AuthError | None], None],
|
|
470
|
+
multi_factor_session: MultiFactorSession | None = None,
|
|
471
|
+
) -> None:
|
|
472
|
+
"""Send a verification code to ``phone_number`` by SMS.
|
|
473
|
+
|
|
474
|
+
Args:
|
|
475
|
+
phone_number: the number in E.164 format, for example ``"+15555550100"``.
|
|
476
|
+
callback: gets ``(verification_id, None)``, or ``(None, error)``.
|
|
477
|
+
Keep ``verification_id`` for ``credential``. If the callback
|
|
478
|
+
URL scheme is missing from ``Info.plist``, the error says which
|
|
479
|
+
one to add (the SDK itself would stop the app).
|
|
480
|
+
multi_factor_session: to enroll the number as a second factor: the
|
|
481
|
+
session from ``MultiFactor.get_session``. ``None`` for sign-in
|
|
482
|
+
with the number.
|
|
483
|
+
"""
|
|
484
|
+
problem = missing_callback_scheme(self._auth_native)
|
|
485
|
+
if problem is not None:
|
|
486
|
+
callback(None, simple_auth_error(problem))
|
|
487
|
+
return
|
|
488
|
+
session = None if multi_factor_session is None else multi_factor_session.native
|
|
489
|
+
self._native.verifyPhoneNumber_UIDelegate_multiFactorSession_completion_(
|
|
490
|
+
phone_number, None, session, string_block(callback)
|
|
491
|
+
)
|
|
492
|
+
|
|
493
|
+
def verify_phone_number_for_sign_in(
|
|
494
|
+
self,
|
|
495
|
+
hint: PhoneMultiFactorInfo,
|
|
496
|
+
session: MultiFactorSession,
|
|
497
|
+
callback: Callable[[str | None, AuthError | None], None],
|
|
498
|
+
) -> None:
|
|
499
|
+
"""Send the SMS code of an enrolled second factor, to finish a sign-in.
|
|
500
|
+
|
|
501
|
+
Args:
|
|
502
|
+
hint: the phone factor chosen from ``MultiFactorResolver.hints``.
|
|
503
|
+
session: ``MultiFactorResolver.session``.
|
|
504
|
+
callback: gets ``(verification_id, None)``, or ``(None, error)``.
|
|
505
|
+
"""
|
|
506
|
+
problem = missing_callback_scheme(self._auth_native)
|
|
507
|
+
if problem is not None:
|
|
508
|
+
callback(None, simple_auth_error(problem))
|
|
509
|
+
return
|
|
510
|
+
self._native.verifyPhoneNumberWithMultiFactorInfo_UIDelegate_multiFactorSession_completion_(
|
|
511
|
+
hint.native, None, session.native, string_block(callback)
|
|
512
|
+
)
|
|
513
|
+
|
|
514
|
+
def credential(
|
|
515
|
+
self, verification_id: str, verification_code: str
|
|
516
|
+
) -> AuthCredential:
|
|
517
|
+
"""A credential from the code the user received.
|
|
518
|
+
|
|
519
|
+
Args:
|
|
520
|
+
verification_id: what ``verify_phone_number`` gave.
|
|
521
|
+
verification_code: the code from the SMS.
|
|
522
|
+
|
|
523
|
+
Returns:
|
|
524
|
+
An ``AuthCredential`` for ``"phone"``.
|
|
525
|
+
"""
|
|
526
|
+
return AuthCredential(
|
|
527
|
+
self._native.credentialWithVerificationID_verificationCode_(
|
|
528
|
+
verification_id, verification_code
|
|
529
|
+
)
|
|
530
|
+
)
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
class GameCenterAuthProvider:
|
|
534
|
+
"""Credentials for the Game Center player signed in on the device."""
|
|
535
|
+
|
|
536
|
+
PROVIDER_ID: Final = "gc.apple.com"
|
|
537
|
+
"""The provider ID of Game Center sign-in."""
|
|
538
|
+
|
|
539
|
+
@staticmethod
|
|
540
|
+
def get_credential(callback: CredentialCallback) -> None:
|
|
541
|
+
"""A credential for the local Game Center player.
|
|
542
|
+
|
|
543
|
+
The player must already be signed in to Game Center (``GKLocalPlayer``).
|
|
544
|
+
|
|
545
|
+
Args:
|
|
546
|
+
callback: gets ``(credential, None)``, or ``(None, error)``
|
|
547
|
+
(``LOCAL_PLAYER_NOT_AUTHENTICATED`` if nobody is signed in).
|
|
548
|
+
"""
|
|
549
|
+
objc_class("FIRGameCenterAuthProvider").getCredentialWithCompletion_(
|
|
550
|
+
result_block(callback, _wrap_credential)
|
|
551
|
+
)
|