-
Notifications
You must be signed in to change notification settings - Fork 335
feat(auth): Add auth emulator support via the FIREBASE_AUTH_EMULATOR_HOST environment variable. #531
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
Merged
Merged
feat(auth): Add auth emulator support via the FIREBASE_AUTH_EMULATOR_HOST environment variable. #531
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
65ad7c5
Support auth emulator via FIREBASE_AUTH_EMULATOR_HOST
muru 69b8784
Tests for emulator support in auth, user mgmt and token gen
muru 6624d41
fallback for monkeypatch in python 3.5
muru b70cd9f
Token verification for the auth emulator
muru 1e65c84
Accommodate auth emulator behaviour in tests.
muru File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,19 @@ | |
METADATA_SERVICE_URL = ('http://metadata.google.internal/computeMetadata/v1/instance/' | ||
'service-accounts/default/email') | ||
|
||
# Emulator fake account | ||
AUTH_EMULATOR_EMAIL = '[email protected]' | ||
|
||
|
||
class _EmulatedSigner(google.auth.crypt.Signer): | ||
key_id = None | ||
|
||
def __init__(self): | ||
pass | ||
|
||
def sign(self, message): | ||
return b'' | ||
|
||
|
||
class _SigningProvider: | ||
"""Stores a reference to a google.auth.crypto.Signer.""" | ||
|
@@ -78,21 +91,28 @@ def from_iam(cls, request, google_cred, service_account): | |
signer = iam.Signer(request, google_cred, service_account) | ||
return _SigningProvider(signer, service_account) | ||
|
||
@classmethod | ||
def for_emulator(cls): | ||
return _SigningProvider(_EmulatedSigner(), AUTH_EMULATOR_EMAIL) | ||
|
||
|
||
class TokenGenerator: | ||
"""Generates custom tokens and session cookies.""" | ||
|
||
ID_TOOLKIT_URL = 'https://identitytoolkit.googleapis.com/v1' | ||
|
||
def __init__(self, app, http_client): | ||
def __init__(self, app, http_client, url_override=None): | ||
self.app = app | ||
self.http_client = http_client | ||
self.request = transport.requests.Request() | ||
self.base_url = '{0}/projects/{1}'.format(self.ID_TOOLKIT_URL, app.project_id) | ||
url_prefix = url_override or self.ID_TOOLKIT_URL | ||
self.base_url = '{0}/projects/{1}'.format(url_prefix, app.project_id) | ||
self._signing_provider = None | ||
|
||
def _init_signing_provider(self): | ||
"""Initializes a signing provider by following the go/firebase-admin-sign protocol.""" | ||
if _auth_utils.is_emulated(): | ||
return _SigningProvider.for_emulator() | ||
# If the SDK was initialized with a service account, use it to sign bytes. | ||
google_cred = self.app.credential.get_credential() | ||
if isinstance(google_cred, google.oauth2.service_account.Credentials): | ||
|
@@ -285,20 +305,22 @@ def verify(self, token, request): | |
verify_id_token_msg = ( | ||
'See {0} for details on how to retrieve {1}.'.format(self.url, self.short_name)) | ||
|
||
emulated = _auth_utils.is_emulated() | ||
|
||
error_message = None | ||
if audience == FIREBASE_AUDIENCE: | ||
error_message = ( | ||
'{0} expects {1}, but was given a custom ' | ||
'token.'.format(self.operation, self.articled_short_name)) | ||
elif not header.get('kid'): | ||
elif not emulated and not header.get('kid'): | ||
if header.get('alg') == 'HS256' and payload.get( | ||
'v') == 0 and 'uid' in payload.get('d', {}): | ||
error_message = ( | ||
'{0} expects {1}, but was given a legacy custom ' | ||
'token.'.format(self.operation, self.articled_short_name)) | ||
else: | ||
error_message = 'Firebase {0} has no "kid" claim.'.format(self.short_name) | ||
elif header.get('alg') != 'RS256': | ||
elif not emulated and header.get('alg') != 'RS256': | ||
error_message = ( | ||
'Firebase {0} has incorrect algorithm. Expected "RS256" but got ' | ||
'"{1}". {2}'.format(self.short_name, header.get('alg'), verify_id_token_msg)) | ||
|
@@ -329,11 +351,14 @@ def verify(self, token, request): | |
raise self._invalid_token_error(error_message) | ||
|
||
try: | ||
verified_claims = google.oauth2.id_token.verify_token( | ||
token, | ||
request=request, | ||
audience=self.project_id, | ||
certs_url=self.cert_url) | ||
if emulated: | ||
verified_claims = payload | ||
else: | ||
verified_claims = google.oauth2.id_token.verify_token( | ||
token, | ||
request=request, | ||
audience=self.project_id, | ||
certs_url=self.cert_url) | ||
verified_claims['uid'] = verified_claims['sub'] | ||
return verified_claims | ||
except google.auth.exceptions.TransportError as error: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.