From 5728511e094aff3978c98fc1d616dce4b60a5a23 Mon Sep 17 00:00:00 2001 From: Kali Kaneko Date: Fri, 4 Sep 2015 00:38:59 -0400 Subject: factor out http utils, add decorator for authenticated methods --- src/leap/bonafide/_decorators.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/leap/bonafide/_decorators.py (limited to 'src/leap/bonafide/_decorators.py') diff --git a/src/leap/bonafide/_decorators.py b/src/leap/bonafide/_decorators.py new file mode 100644 index 0000000..cfd6ec0 --- /dev/null +++ b/src/leap/bonafide/_decorators.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- +# _decorators.py +# Copyright (C) 2015 LEAP +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +""" +Decorators used in bonafide. +""" + + +def needs_authentication(func): + """ + Decorate a method so that it will not be called if the instance + attribute `is_authenticated` does not evaluate to True. + """ + def decorated(*args, **kwargs): + instance = args[0] + allowed = getattr(instance, 'is_authenticated') + if not allowed: + raise RuntimeError('This method requires authentication') + return func(*args, **kwargs) + return decorated -- cgit v1.2.3