-
Notifications
You must be signed in to change notification settings - Fork 339
feat(auth): Add bulk get/delete methods #400
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
Changes from 18 commits
aa8207d
a83f66e
c5f57d0
0e282f7
070c1a8
466890b
598ffc9
1993007
0b18203
65d7d63
b21f23e
d4a8a3e
3c6b776
2167764
9ade7a8
278ee4a
7087016
5990d44
ba60c65
e9dafc1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# Copyright 2020 Google Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Parse RFC3339 date strings""" | ||
|
||
from datetime import datetime, timezone | ||
import re | ||
|
||
def parse_to_epoch(datestr): | ||
"""Parse an RFC3339 date string and return the number of seconds since the | ||
epoch (as a float). | ||
|
||
In particular, this method is meant to parse the strings returned by the | ||
JSON mapping of protobuf google.protobuf.timestamp.Timestamp instances: | ||
https://github.com/protocolbuffers/protobuf/blob/4cf5bfee9546101d98754d23ff378ff718ba8438/src/google/protobuf/timestamp.proto#L99 | ||
|
||
This method has microsecond precision; nanoseconds will be truncated. | ||
|
||
Args: | ||
datestr: A string in RFC3339 format. | ||
Returns: | ||
Float: The number of seconds since the Unix epoch. | ||
Raises: | ||
ValueError: Raised if the `datestr` is not a valid RFC3339 date string. | ||
""" | ||
return _parse_to_datetime(datestr).timestamp() | ||
|
||
|
||
def _parse_to_datetime(datestr): | ||
"""Parse an RFC3339 date string and return a python datetime instance. | ||
|
||
Args: | ||
datestr: A string in RFC3339 format. | ||
Returns: | ||
datetime: The corresponding `datetime` (with timezone information). | ||
Raises: | ||
ValueError: Raised if the `datestr` is not a valid RFC3339 date string. | ||
""" | ||
# If more than 6 digits appear in the fractional seconds position, truncate | ||
# to just the most significant 6. (i.e. we only have microsecond precision; | ||
# nanos are truncated.) | ||
datestr_modified = re.sub(r'(\.\d{6})\d*', r'\1', datestr) | ||
|
||
# This format is the one we actually expect to occur from our backend. The | ||
# others are only present because the spec says we *should* accept them. | ||
try: | ||
return datetime.strptime( | ||
datestr_modified, '%Y-%m-%dT%H:%M:%S.%fZ' | ||
).replace(tzinfo=timezone.utc) | ||
except ValueError: | ||
pass | ||
|
||
try: | ||
return datetime.strptime( | ||
datestr_modified, '%Y-%m-%dT%H:%M:%SZ' | ||
).replace(tzinfo=timezone.utc) | ||
except ValueError: | ||
pass | ||
|
||
# Note: %z parses timezone offsets, but requires the timezone offset *not* | ||
# include a separating ':'. As of python 3.7, this was relaxed. | ||
# TODO(rsgowman): Once python3.7 becomes our floor, we can drop the regex | ||
# replacement. | ||
datestr_modified = re.sub(r'(\d\d):(\d\d)$', r'\1\2', datestr_modified) | ||
|
||
try: | ||
return datetime.strptime(datestr_modified, '%Y-%m-%dT%H:%M:%S.%f%z') | ||
except ValueError: | ||
pass | ||
|
||
try: | ||
return datetime.strptime(datestr_modified, '%Y-%m-%dT%H:%M:%S%z') | ||
except ValueError: | ||
pass | ||
|
||
raise ValueError('time data {0} does not match RFC3339 format'.format(datestr)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# Copyright 2020 Google Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Classes to uniquely identify a user.""" | ||
|
||
class UserIdentifier: | ||
"""Identifies a user to be looked up.""" | ||
|
||
hiranya911 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
class UidIdentifier(UserIdentifier): | ||
"""Used for looking up an account by uid. | ||
|
||
See ``auth.get_user()``. | ||
""" | ||
|
||
def __init__(self, uid): | ||
"""Constructs a new `UidIdentifier` object. | ||
|
||
Args: | ||
uid: A user ID string. | ||
""" | ||
self.uid = uid | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to validate these arguments? (Not empty, not None, string type etc) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It can't hurt, though implies we'll be doing some of the validation twice. (I don't think that's a problem.) Done. (Also, no longer doing validation twice; see other comment). Note that we don't validate arguments to (eg) Another improvement (that I don't want to make in this PR) is to use type annotations. All of our supported python versions now support type annotations and they'd give you some of this for free (and at "compile" time too.) |
||
|
||
|
||
class EmailIdentifier(UserIdentifier): | ||
"""Used for looking up an account by email. | ||
|
||
See ``auth.get_user()``. | ||
""" | ||
|
||
def __init__(self, email): | ||
"""Constructs a new `EmailIdentifier` object. | ||
|
||
Args: | ||
email: A user email address string. | ||
""" | ||
self.email = email | ||
|
||
|
||
class PhoneIdentifier(UserIdentifier): | ||
"""Used for looking up an account by phone number. | ||
|
||
See ``auth.get_user()``. | ||
""" | ||
|
||
def __init__(self, phone_number): | ||
"""Constructs a new `PhoneIdentifier` object. | ||
|
||
Args: | ||
phone_number: A phone number string. | ||
""" | ||
self.phone_number = phone_number | ||
|
||
|
||
class ProviderIdentifier(UserIdentifier): | ||
"""Used for looking up an account by provider. | ||
|
||
See ``auth.get_user()``. | ||
""" | ||
|
||
def __init__(self, provider_id, provider_uid): | ||
"""Constructs a new `ProviderIdentifier` object. | ||
|
||
Args: | ||
provider_id: A provider ID string. | ||
provider_uid: A provider UID string. | ||
""" | ||
self.provider_id = provider_id | ||
self.provider_uid = provider_uid |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -472,7 +472,12 @@ def standard_scrypt(cls, memory_cost, parallelization, block_size, derived_key_l | |
|
||
|
||
class ErrorInfo: | ||
"""Represents an error encountered while importing an ``ImportUserRecord``.""" | ||
"""Represents an error encountered while performing a batch operation such | ||
as importing users or deleting multiple user accounts. | ||
""" | ||
# TODO(rsgowman): This class used to be specific to importing users (hence | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. _auth_utils could be a good place. But feel free to defer that to future PR. |
||
# it's home in _user_import.py). It's now also used by bulk deletion of | ||
# users. Move this to a more common location. | ||
|
||
def __init__(self, error): | ||
self._index = error['index'] | ||
|
Uh oh!
There was an error while loading. Please reload this page.