Skip to content
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

import official firebase-token-generator #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 38 additions & 25 deletions firebase/firebase.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
try:
import urlparse
except ImportError:
#py3k
# py3k
from urllib import parse as urlparse

import json
from firebase_token_generator import create_token

from .firebase_token_generator import FirebaseTokenGenerator
from .decorators import http_connection

from .async import process_pool
Expand Down Expand Up @@ -93,8 +93,7 @@ def make_post_request(url, data, params, headers, connection):
response => {u'name': u'-Inw6zol_2f5ThHwVcSe'} or {'error': 'Permission denied.'}
"""
timeout = getattr(connection, 'timeout')
response = connection.post(url, data=data, params=params, headers=headers,
timeout=timeout)
response = connection.post(url, data=data, params=params, headers=headers, timeout=timeout)
if response.ok or response.status_code == 403:
return response.json() if response.content else None
else:
Expand Down Expand Up @@ -122,8 +121,7 @@ def make_patch_request(url, data, params, headers, connection):
response => {'Ozgur Vatansever'} or {'error': 'Permission denied.'}
"""
timeout = getattr(connection, 'timeout')
response = connection.patch(url, data=data, params=params, headers=headers,
timeout=timeout)
response = connection.patch(url, data=data, params=params, headers=headers, timeout=timeout)
if response.ok or response.status_code == 403:
return response.json() if response.content else None
else:
Expand Down Expand Up @@ -162,11 +160,16 @@ class FirebaseUser(object):
Class that wraps the credentials of the authenticated user. Think of
this as a container that holds authentication related data.
"""
def __init__(self, email, firebase_auth_token, provider, id=None):

def __init__(self, email, firebase_auth_token, provider, uid=None):
self.email = email
self.firebase_auth_token = firebase_auth_token
self.provider = provider
self.id = id
self.uid = uid

def __repr__(self):
return '<%s email="%s" uid="%s" provider="%s" firebase_auth_token="%s">' % (
self.__class__.__name__, self.email, self.uid, self.provider, self.firebase_auth_token)


class FirebaseAuthentication(object):
Expand All @@ -177,21 +180,26 @@ class does not trigger a connection, simply fakes the auth action.
In addition, the provided email and password information is totally
useless and they never appear in the ``auth`` variable at the server.
"""
def __init__(self, secret, email, debug=False, admin=False, extra=None):
self.authenticator = FirebaseTokenGenerator(secret, debug, admin)

def __init__(self, secret, email, auth_payload=None):
assert secret, 'Your Firebase SECRET is not valid'
self.secret = secret
self.email = email
self.provider = 'password'
self.extra = (extra or {}).copy()
self.extra.update({'debug': debug, 'admin': admin,
'email': self.email, 'provider': self.provider})
self.auth_payload = (auth_payload or {}).copy()

def get_user(self):
def get_user(self, expires=None, not_before=None, admin=False, debug=False, simulate=False):
"""
Method that gets the authenticated user. The returning user has
the token, email and the provider data.
"""
token = self.authenticator.create_token(self.extra)
user_id = self.extra.get('id')
options = {'admin': admin, 'debug': debug, 'simulate': simulate}
if expires is not None:
options['expires'] = expires
if not_before is not None:
options['notBefore'] = not_before
token = create_token(self.secret, self.auth_payload, options)
user_id = self.auth_payload.get('uid')
return FirebaseUser(self.email, token, self.provider, user_id)


Expand Down Expand Up @@ -240,7 +248,7 @@ def _build_endpoint_url(self, url, name=None):
full_url => 'http://firebase.localhost/users/1.json'
"""
if not url.endswith(self.URL_SEPERATOR):
url = url + self.URL_SEPERATOR
url += self.URL_SEPERATOR
if name is None:
name = ''
return '%s%s%s' % (urlparse.urljoin(self.dsn, url), name,
Expand All @@ -259,14 +267,15 @@ def _authenticate(self, params, headers):
if self.authentication:
user = self.authentication.get_user()
params.update({'auth': user.firebase_auth_token})
headers.update(self.authentication.authenticator.HEADERS)
# headers.update(self.authentication.authenticator.HEADERS)

@http_connection(60)
def get(self, url, name, params=None, headers=None, connection=None):
"""
Synchronous GET request.
"""
if name is None: name = ''
if name is None:
name = ''
params = params or {}
headers = headers or {}
endpoint = self._build_endpoint_url(url, name)
Expand All @@ -277,13 +286,14 @@ def get_async(self, url, name, callback=None, params=None, headers=None):
"""
Asynchronous GET request with the process pool.
"""
if name is None: name = ''
if name is None:
name = ''
params = params or {}
headers = headers or {}
endpoint = self._build_endpoint_url(url, name)
self._authenticate(params, headers)
process_pool.apply_async(make_get_request,
args=(endpoint, params, headers), callback=callback)
args=(endpoint, params, headers), callback=callback)

@http_connection(60)
def put(self, url, name, data, params=None, headers=None, connection=None):
Expand All @@ -305,7 +315,8 @@ def put_async(self, url, name, data, callback=None, params=None, headers=None):
"""
Asynchronous PUT request with the process pool.
"""
if name is None: name = ''
if name is None:
name = ''
params = params or {}
headers = headers or {}
endpoint = self._build_endpoint_url(url, name)
Expand Down Expand Up @@ -372,7 +383,8 @@ def delete(self, url, name, params=None, headers=None, connection=None):
"""
Synchronous DELETE request. ``data`` must be a JSONable value.
"""
if not name: name = ''
if not name:
name = ''
params = params or {}
headers = headers or {}
endpoint = self._build_endpoint_url(url, name)
Expand All @@ -383,10 +395,11 @@ def delete_async(self, url, name, callback=None, params=None, headers=None):
"""
Asynchronous DELETE request with the process pool.
"""
if not name: name = ''
if not name:
name = ''
params = params or {}
headers = headers or {}
endpoint = self._build_endpoint_url(url, name)
self._authenticate(params, headers)
process_pool.apply_async(make_delete_request,
args=(endpoint, params, headers), callback=callback)
args=(endpoint, params, headers), callback=callback)
116 changes: 0 additions & 116 deletions firebase/firebase_token_generator.py

This file was deleted.

1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
requests>=1.1.0
firebase-token-generator>=2.0.1
5 changes: 3 additions & 2 deletions tests/firebase_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ def setUp(self):
self.SECRET = 'FAKE_FIREBASE_SECRET'
self.DSN = 'https://firebase.localhost'
self.EMAIL = '[email protected]'
self.UID = '123'
self.authentication = FirebaseAuthentication(self.SECRET, self.EMAIL,
None)
auth_payload={'uid': self.UID})
self.firebase = FirebaseApplication(self.DSN, self.authentication)

def test_build_endpoint_url(self):
Expand Down Expand Up @@ -95,4 +96,4 @@ def test_make_delete_request(self):
connection = MockConnection(response)
result = self.firebase.delete('url', 'snapshot', params={}, headers={},
connection=connection)
self.assertEqual(result, json.loads(response.content))
self.assertEqual(result, json.loads(response.content))