| 1 |
.. _secure_controller: |
| 2 |
|
| 3 |
Security and Authentication |
| 4 |
=========================== |
| 5 |
|
| 6 |
Pecan provides no out-of-the-box support for authentication, but it |
| 7 |
does give you the necessary tools to handle authentication and |
| 8 |
authorization as you see fit. |
| 9 |
|
| 10 |
``secure`` Decorator Basics |
| 11 |
--------------------------- |
| 12 |
|
| 13 |
You can wrap entire controller subtrees *or* individual method calls |
| 14 |
with access controls using the :func:`~pecan.secure.secure` decorator. |
| 15 |
|
| 16 |
To decorate a method, use one argument:: |
| 17 |
|
| 18 |
secure('<check_permissions_method_name>') |
| 19 |
|
| 20 |
To secure a class, invoke with two arguments:: |
| 21 |
|
| 22 |
secure(object_instance, '<check_permissions_method_name>') |
| 23 |
|
| 24 |
:: |
| 25 |
|
| 26 |
from pecan import expose |
| 27 |
from pecan.secure import secure |
| 28 |
|
| 29 |
class HighlyClassifiedController(object): |
| 30 |
pass |
| 31 |
|
| 32 |
class UnclassifiedController(object): |
| 33 |
pass |
| 34 |
|
| 35 |
class RootController(object): |
| 36 |
|
| 37 |
@classmethod |
| 38 |
def check_permissions(cls): |
| 39 |
if user_is_admin(): |
| 40 |
return True |
| 41 |
return False |
| 42 |
|
| 43 |
@expose() |
| 44 |
def index(self): |
| 45 |
# |
| 46 |
# This controller is unlocked to everyone, |
| 47 |
# and will not run any security checks. |
| 48 |
# |
| 49 |
return dict() |
| 50 |
|
| 51 |
@secure('check_permissions') |
| 52 |
@expose() |
| 53 |
def topsecret(self): |
| 54 |
# |
| 55 |
# This controller is top-secret, and should |
| 56 |
# only be reachable by administrators. |
| 57 |
# |
| 58 |
return dict() |
| 59 |
|
| 60 |
highly_classified = secure(HighlyClassifiedController(), 'check_permissions') |
| 61 |
unclassified = UnclassifiedController() |
| 62 |
|
| 63 |
|
| 64 |
``SecureController`` |
| 65 |
-------------------- |
| 66 |
|
| 67 |
Alternatively, the same functionality can also be accomplished by |
| 68 |
subclassing Pecan's :class:`~pecan.secure.SecureController`. Implementations of |
| 69 |
:class:`~pecan.secure.SecureController` should extend the |
| 70 |
:meth:`~pecan.secure.SecureControllerBase.check_permissions` class method to |
| 71 |
return ``True`` if the user has permissions to the controller branch and |
| 72 |
``False`` if they do not. |
| 73 |
|
| 74 |
:: |
| 75 |
|
| 76 |
from pecan import expose |
| 77 |
from pecan.secure import SecureController, unlocked |
| 78 |
|
| 79 |
class HighlyClassifiedController(object): |
| 80 |
pass |
| 81 |
|
| 82 |
class UnclassifiedController(object): |
| 83 |
pass |
| 84 |
|
| 85 |
class RootController(SecureController): |
| 86 |
|
| 87 |
@classmethod |
| 88 |
def check_permissions(cls): |
| 89 |
if user_is_admin(): |
| 90 |
return True |
| 91 |
return False |
| 92 |
|
| 93 |
@expose() |
| 94 |
@unlocked |
| 95 |
def index(self): |
| 96 |
# |
| 97 |
# This controller is unlocked to everyone, |
| 98 |
# and will not run any security checks. |
| 99 |
# |
| 100 |
return dict() |
| 101 |
|
| 102 |
@expose() |
| 103 |
def topsecret(self): |
| 104 |
# |
| 105 |
# This controller is top-secret, and should |
| 106 |
# only be reachable by administrators. |
| 107 |
# |
| 108 |
return dict() |
| 109 |
|
| 110 |
highly_classified = HighlyClassifiedController() |
| 111 |
unclassified = unlocked(UnclassifiedController()) |
| 112 |
|
| 113 |
|
| 114 |
Also note the use of the :func:`~pecan.secure.unlocked` decorator in the above |
| 115 |
example, which can be used similarly to explicitly unlock a controller for |
| 116 |
public access without any security checks. |
| 117 |
|
| 118 |
|
| 119 |
Writing Authentication/Authorization Methods |
| 120 |
-------------------------------------------- |
| 121 |
|
| 122 |
The :meth:`~pecan.secure.SecureControllerBase.check_permissions` method should |
| 123 |
be used to determine user authentication and authorization. The code you |
| 124 |
implement here could range from simple session assertions (the existing user is |
| 125 |
authenticated as an administrator) to connecting to an LDAP service. |
| 126 |
|
| 127 |
|
| 128 |
More on ``secure`` |
| 129 |
------------------ |
| 130 |
|
| 131 |
The :func:`~pecan.secure.secure` method has several advanced uses that allow |
| 132 |
you to create robust security policies for your application. |
| 133 |
|
| 134 |
First, you can pass via a string the name of either a class method or an |
| 135 |
instance method of the controller to use as the |
| 136 |
:meth:`~pecan.secure.SecureControllerBase.check_permissions` method. Instance |
| 137 |
methods are particularly useful if you wish to authorize access to attributes |
| 138 |
of a model instance. Consider the following example of a basic virtual |
| 139 |
filesystem. |
| 140 |
|
| 141 |
:: |
| 142 |
|
| 143 |
from pecan import expose |
| 144 |
from pecan.secure import secure |
| 145 |
|
| 146 |
from myapp.session import get_current_user |
| 147 |
from myapp.model import FileObject |
| 148 |
|
| 149 |
class FileController(object): |
| 150 |
def __init__(self, name): |
| 151 |
self.file_object = FileObject(name) |
| 152 |
|
| 153 |
def read_access(self): |
| 154 |
self.file_object.read_access(get_current_user()) |
| 155 |
|
| 156 |
def write_access(self): |
| 157 |
self.file_object.write_access(get_current_user()) |
| 158 |
|
| 159 |
@secure('write_access') |
| 160 |
@expose() |
| 161 |
def upload_file(self): |
| 162 |
pass |
| 163 |
|
| 164 |
@secure('read_access') |
| 165 |
@expose() |
| 166 |
def download_file(self): |
| 167 |
pass |
| 168 |
|
| 169 |
class RootController(object): |
| 170 |
@expose() |
| 171 |
def _lookup(self, name, *remainder): |
| 172 |
return FileController(name), remainder |
| 173 |
|
| 174 |
|
| 175 |
The :func:`~pecan.secure.secure` method also accepts a function argument. When |
| 176 |
passing a function, make sure that the function is imported from another |
| 177 |
file or defined in the same file before the class definition, otherwise |
| 178 |
you will likely get error during module import. |
| 179 |
|
| 180 |
:: |
| 181 |
|
| 182 |
from pecan import expose |
| 183 |
from pecan.secure import secure |
| 184 |
|
| 185 |
from myapp.auth import user_authenitcated |
| 186 |
|
| 187 |
class RootController(object): |
| 188 |
@secure(user_authenticated) |
| 189 |
@expose() |
| 190 |
def index(self): |
| 191 |
return 'Logged in' |
| 192 |
|
| 193 |
|
| 194 |
You can also use the :func:`~pecan.secure.secure` method to change the behavior |
| 195 |
of a :class:`~pecan.secure.SecureController`. Decorating a method or wrapping |
| 196 |
a subcontroller tells Pecan to use another security function other than the |
| 197 |
default controller method. This is useful for situations where you want |
| 198 |
a different level or type of security. |
| 199 |
|
| 200 |
:: |
| 201 |
|
| 202 |
from pecan import expose |
| 203 |
from pecan.secure import SecureController, secure |
| 204 |
|
| 205 |
from myapp.auth import user_authenticated, admin_user |
| 206 |
|
| 207 |
class ApiController(object): |
| 208 |
pass |
| 209 |
|
| 210 |
class RootController(SecureController): |
| 211 |
@classmethod |
| 212 |
def check_permissions(cls): |
| 213 |
return user_authenticated() |
| 214 |
|
| 215 |
@classmethod |
| 216 |
def check_api_permissions(cls): |
| 217 |
return admin_user() |
| 218 |
|
| 219 |
@expose() |
| 220 |
def index(self): |
| 221 |
return 'logged in user' |
| 222 |
|
| 223 |
api = secure(ApiController(), 'check_api_permissions') |
| 224 |
|
| 225 |
In the example above, pecan will *only* call :func:`admin_user` when a request is |
| 226 |
made for ``/api/``. |
| 227 |
|
| 228 |
|
| 229 |
Multiple Secure Controllers |
| 230 |
--------------------------- |
| 231 |
|
| 232 |
Secure controllers can be nested to provide increasing levels of |
| 233 |
security on subcontrollers. In the example below, when a request is |
| 234 |
made for ``/admin/index/``, Pecan first calls |
| 235 |
:func:`~pecan.secure.SecureControllerBase.check_permissions` on the |
| 236 |
:class:`RootController` and then |
| 237 |
calls :func:`~pecan.secure.SecureControllerBase.check_permissions` on the |
| 238 |
:class:`AdminController`. |
| 239 |
|
| 240 |
:: |
| 241 |
|
| 242 |
from pecan import expose |
| 243 |
from pecan.secure import SecureController |
| 244 |
|
| 245 |
from myapp.auth import user_logged_in, is_admin |
| 246 |
|
| 247 |
class AdminController(SecureController): |
| 248 |
@classmethod |
| 249 |
def check_permissions(cls): |
| 250 |
return is_admin() |
| 251 |
|
| 252 |
@expose() |
| 253 |
def index(self): |
| 254 |
return 'admin dashboard' |
| 255 |
|
| 256 |
class RootController(SecureController): |
| 257 |
@classmethod |
| 258 |
def check_permissions(cls): |
| 259 |
return user_logged_in |
| 260 |
|
| 261 |
@expose() |
| 262 |
def index(self): |
| 263 |
return 'user dashboard' |