Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
6 changed files
with
76 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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 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 was deleted.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -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) |
This file contains 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 |
---|---|---|
@@ -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} |
This file contains 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