Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
make the API work with the redis backend
  • Loading branch information
Jeff Balogh committed Nov 2, 2011
1 parent 46523b5 commit bb31a23
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 49 deletions.
29 changes: 29 additions & 0 deletions README.rst
Expand Up @@ -16,3 +16,32 @@ Run the Server
::

paster serve etc/demoapp-dev.ini


The API
-------

::

GET /alias/

>>> 200 OK {"email": <email>, "aliases": [<alias>, <alias>, ...]}

Requires browserid auth.

::

POST /alias/

>>> 200 OK {"email": <email>, "alias": <alias>}

Requires browserid auth.


::

GET /alias/<alias>

>>> 200 OK {"email": <email>}

No auth required.
2 changes: 1 addition & 1 deletion demoapp/__init__.py
Expand Up @@ -30,7 +30,7 @@ def main(global_config, **settings):
config.include("mozsvc")

# adds application-specific views
config.add_route("whoami", "/whoami")
config.add_route("get_address", "/alias/{alias}")
config.scan("demoapp.views")

return config.make_wsgi_app()
9 changes: 0 additions & 9 deletions demoapp/storage/redis.py

This file was deleted.

20 changes: 20 additions & 0 deletions demoapp/storage/redis_.py
@@ -0,0 +1,20 @@
import logging

import redis


class Storage(object):

def __init__(self, **kw):
self.redis = redis.Redis(**kw)

def resolve_alias(self, alias):
return self.redis.get(alias)

def add_alias(self, email, alias):
self.redis.set(alias, email)
self.redis.rpush(email, alias)
return alias

def get_aliases(self, email):
return self.redis.lrange(email, 0, -1)
63 changes: 25 additions & 38 deletions demoapp/views.py
@@ -1,51 +1,38 @@
from collections import defaultdict
import uuid

from pyramid.exceptions import Forbidden
from pyramid.security import authenticated_userid, effective_principals
from pyramid.security import authenticated_userid
from pyramid.view import view_config

from cornice import Service

aliases = Service(name='aliases', path='/alias/',
description='Manage the email <=> alias store.')

info_desc = """\
This service is useful to get and set data for a user.
"""

def new_alias():
# TODO: get the domain from the config.
return '%s@%s' % (uuid.uuid4().hex, 'browserid.org')

user_info = Service(name='users', path='/{username}/info',
description=info_desc)

_USERS = defaultdict(dict)
@view_config(route_name='get_address', renderer='simplejson')
def get_address(request):
"""Get the real address for a given alias."""
db = request.registry['storage']
email = db.resolve_alias(request.matchdict['alias'])
return {'email': email}


@user_info.get()
def get_info(request):
"""Returns the public information about a **user**.
@aliases.get(permission='authenticated')
def list_aliases(request):
db = request.registry['storage']
email = authenticated_userid(request)
aliases = db.get_aliases(email) or []
return {'email': email, 'aliases': aliases}

If the user does not exists, returns an empty dataset.
"""
username = request.matchdict['username']
return _USERS[username]


@user_info.post()
def set_info(request):
"""Set the public information for a **user**.
You have to be that user, and *authenticated*.
Returns *True* or *False*.
"""
username = authenticated_userid(request)
if request.matchdict["username"] != username:
raise Forbidden()
_USERS[username] = request.json_body
return {'success': True}


@view_config(route_name="whoami", permission="authenticated", renderer="json")
def whoami(request):
"""View returning the authenticated user's credentials."""
username = authenticated_userid(request)
principals = effective_principals(request)
return {"username": username, "principals": principals}
@aliases.post(permission='authenticated')
def add_alias(request):
db = request.registry['storage']
email = authenticated_userid(request)
alias = db.add_alias(email, new_alias())
return {'email': email, 'alias': alias}
2 changes: 1 addition & 1 deletion etc/demoapp-dev.ini
Expand Up @@ -10,7 +10,7 @@ private_key = 6Le8OLwSAAAAAEKoqfc-DmoF4HNswD7RNdGwxRij
use_ssl = false

[storage]
backend = demoapp.storage.redis.Storage
backend = demoapp.storage.redis_.Storage
host = localhost
port = 6379

Expand Down

0 comments on commit bb31a23

Please sign in to comment.