Skip to content

Commit

Permalink
Merge branch 'master' of github.com:sneridagh/osiris
Browse files Browse the repository at this point in the history
  • Loading branch information
sneridagh committed Dec 3, 2013
2 parents b3c3ac1 + cc629c3 commit d1e751d
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 8 deletions.
18 changes: 18 additions & 0 deletions CHANGES.txt → CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
ChangeLog
=========

1.4 (unreleased)
----------------

- Nothing changed yet.


1.3 (2013-08-02)
----------------

* Added use of greenlets and handle reconnects if cluster is enabled [Victor Fernandez de Alba]
* Support for mongoDB cluster [Victor Fernandez de Alba]

1.2 (2013-06-13)
------------------

- Update the deprecated method to connect to a MongoDB database.
- Added ability to connect to a MongoDB replica set.

1.1 (2013-06-04)
------------------

Expand Down
7 changes: 5 additions & 2 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
include *.txt *.ini *.cfg *.rst
recursive-include osiris *.ico *.png *.css *.gif *.jpg *.pt *.txt *.mak *.mako *.js *.html *.xml
include *.rst *.ini

recursive-include osiris *

global-exclude *.pyc
5 changes: 5 additions & 0 deletions development.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ osiris.store.db = osiris
osiris.store.collection = tokens
osiris.tokenexpiry = 0

osiris.mongodb.cluster = false
osiris.mongodb.hosts = localhost:27017,localhost:27018
osiris.mongodb.replica_set = osiris
osiris.mongodb.use_greenlets = false

osiris.whoconfig = %(here)s/who.ini
# Check README instructions if you want to enable LDAP auth backend,
# for development purposes it's better use other kind of backend like the
Expand Down
52 changes: 48 additions & 4 deletions osiris/store/mongodb_store.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
"""MongoDB UserStore implementation"""

import datetime
from pymongo import Connection
from pymongo import MongoClient
from pymongo import MongoReplicaSetClient
from pymongo.errors import ConnectionFailure
from pymongo.errors import OperationFailure
from pymongo.errors import AutoReconnect

from pyramid.exceptions import ConfigurationError
from pyramid.decorator import reify
from pyramid.exceptions import ConfigurationError
from pyramid.settings import asbool

from osiris.store.interface import TokenStore

Expand All @@ -18,34 +21,73 @@ def includeme(config):
db = settings.get('osiris.store.db', 'osiris')
collection = settings.get('osiris.store.collection', 'tokens')

enable_cluster = asbool(settings.get('osiris.mongodb.cluster', False))
hosts = settings.get('osiris.mongodb.hosts', 'localhost:27017')
replica_set = settings.get('osiris.mongodb.replica_set', '')
use_greenlets = settings.get('osiris.mongodb.use_greenlets', '')

store = MongoDBStore(
host=host, port=port, db=db, collection=collection,
enable_cluster=enable_cluster, hosts=hosts, replica_set=replica_set,
use_greenlets=use_greenlets
)

config.registry.osiris_store = store


def handle_reconnects(fun):
def replacement(*args, **kwargs):
# Handle exceptions in case of database operational error
try:
response = fun(*args, **kwargs)
except AutoReconnect:
tryin_to_reconnect = True
while tryin_to_reconnect:
try:
response = fun(*args, **kwargs)
except AutoReconnect:
pass
else:
tryin_to_reconnect = False
else:
return response
return replacement


class MongoDBStore(TokenStore):
"""MongoDB Storage for oAuth tokens"""
def __init__(self, host='localhost', port=27017, db="osiris",
collection='tokens'):
collection='tokens', enable_cluster=False, hosts='',
replica_set='', use_greenlets=False):
self.host = host
self.port = port
self.db = db
self.collection = collection
self.enable_cluster = enable_cluster
self.hosts = hosts
self.replica_set = replica_set
self.use_greenlets = use_greenlets

@reify
def _conn(self):
"""The MongoDB connection, cached for this call"""
try:
db_conn = Connection(self.host, self.port, slave_okay=False)
if not self.enable_cluster:
db_conn = MongoClient(self.host, self.port, slave_okay=False)
else:
db_conn = MongoReplicaSetClient(self.hosts,
replicaSet=self.replica_set,
use_greenlets=self.use_greenlets)
except ConnectionFailure:
raise Exception('Unable to connect to MongoDB')
conn = db_conn[self.db]

if not self.collection in conn.collection_names():
conn.create_collection(self.collection)

return conn

@handle_reconnects
def retrieve(self, **kwargs):
query = dict([(k, v) for k, v in kwargs.items() if v])
data = self._conn[self.collection].find_one(query)
Expand All @@ -54,6 +96,7 @@ def retrieve(self, **kwargs):
else:
return None

@handle_reconnects
def store(self, token, username, scope, expires_in):
data = {}
try:
Expand All @@ -74,6 +117,7 @@ def store(self, token, username, scope, expires_in):
else:
return True

@handle_reconnects
def delete(self, token):
try:
self._conn[self.collection].remove({'token': token})
Expand Down
5 changes: 5 additions & 0 deletions production.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ osiris.store.db = osiris
osiris.store.collection = tokens
osiris.tokenexpiry = 0

osiris.mongodb.cluster = true
osiris.mongodb.hosts = localhost:27017,localhost:27018
osiris.mongodb.replica_set = osiris
osiris.mongodb.use_greenlets = true

osiris.whoconfig = %(here)s/who.ini
# Check README instructions if you want to enable LDAP auth backend
osiris.ldap_enabled = true
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

here = os.path.abspath(os.path.dirname(__file__))
README = open(os.path.join(here, 'README.rst')).read()
CHANGES = open(os.path.join(here, 'CHANGES.txt')).read()
CHANGES = open(os.path.join(here, 'CHANGES.rst')).read()

requires = ['pyramid',
'pyramid_debugtoolbar',
Expand All @@ -13,7 +13,7 @@
'waitress']

setup(name='osiris',
version='1.1',
version='1.4.dev0',
description='Pyramid based oAuth server',
long_description=README + '\n\n' + CHANGES,
classifiers=[
Expand Down

0 comments on commit d1e751d

Please sign in to comment.