Coverage for bookie.lib.access : 69%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
"""Handle auth and authz activities in bookie"""
"""Manage the inner workings of authorizing things"""
def check_api(submitted_key, users_key): """Verify the api key is valid""" if users_key != submitted_key: return False else: return True
"""Check that the user is logged in correctly
:param username: a username to make sure the current user is in fact
"""
# if we have a username we're told to check against, make sure the # username matches return False
"""Handle the Forbidden exception unless redirect is there
The idea is that if there's a redirect we shoot them to the login form instead
""" if redirect is None: raise HTTPForbidden('Deactivated Account') else: raise HTTPFound(location=request.route_url(redirect))
"""A context manager that works with either Api key or logged in user"""
redirect=None): self.request = request self.api_key = api_key self.user_acct = user_acct self.username = username
if redirect: self.redirect = redirect
"""Handle the verification side
Logged in user checked first, then api matching
"""
# if the user account is not activated then no go if not self.user_acct.activated: raise HTTPForbidden('Deactivated Account')
if AuthHelper.check_login(self.request, username=self.username): return True
if AuthHelper.check_api(self.api_key, self.user_acct.api_key): return True
raise HTTPForbidden('Invalid Authorization')
"""No cleanup to do here""" pass
"""Context manager to check if the user is authorized
use: with ApiAuthorize(some_key): # do work
Will return NotAuthorized if it fails
"""
"""Create the context manager""" self.user = user
def user(self): # <your database connection, however you get it, the below line # is just an example> # dbconn = self.registry.settings['dbconn'] user_id = unauthenticated_userid(self) if user_id is not None: # this should return None if the user doesn't exist # in the database user = UserMgr.get(user_id=user_id) return user
"""Verify api key set in constructor""" # if the user account is not activated then no go if not self.user.activated: raise HTTPForbidden('Deactivated Account')
if not AuthHelper.check_api(self.check_key, self.user.api_key): raise HTTPForbidden('Invalid Authorization')
"""No cleanup work to do after usage""" pass
"""Context manager to check if the user is logged in
use: with ReqAuthorize(request): # do work
Will return NotAuthorized if it fails
"""
"""Create the context manager"""
"""Verify api key set in constructor"""
"""No cleanup work to do after usage"""
def user(self): # <your database connection, however you get it, the below line # is just an example> # dbconn = self.registry.settings['dbconn'] # this should return None if the user doesn't exist # in the database
"""View decorator to set check the client is permitted
Since api calls can come from the api via a api_key or a logged in user via the website, we need to check/authorize both
If this is an api call and the api key is valid, stick the user object found onto the request.user so that the view can find it there in one place.
"""
""" :param api_field: the name of the data in the request.params and the User object we compare to make sure they match :param user_fetcher: a callable that I can give a username to and get back the user object
:sample: @ApiAuth('api_key', UserMgr.get)
"""
""" Return :meth:`wrap_action` as the decorator for ``action_``. """
"""If admin only, verify current api belongs to an admin user"""
else: user = request.user
""" Wrap the controller action ``action_``.
:param action_: The controller action to be wrapped.
``args`` and ``kwargs`` are the positional and named arguments which will be passed to ``action_`` when called.
""" # check request.user to see if this is a logged in user # if so, then make sure it matches the matchdict user
# request should be the one and only arg to the view function
# if this is admin only, you're either an admin or not else: request.response.status_int = 403 return {'error': "Not authorized for request."}
if AuthHelper.check_login(request, username): # then we're good, this is a valid user for this url return action_(*args, **kwargs)
# get the user the api key belongs to # we've got a request with url params
# we've got a ajax request with post data
# now get what this user should be based on the api_key
# if there's a username in the url (rdict) then make sure the user # the api belongs to is the same as the url. You can't currently # use the api to get info for other users.
# if this api call accepts anon requests then let it through
# otherwise, we're done, you're not allowed request.response.status_int = 403 return {'error': "Not authorized for request."} |