Revision bea584e1 snf-astakos-app/astakos/api/util.py
b/snf-astakos-app/astakos/api/util.py | ||
---|---|---|
39 | 39 |
from django.utils import simplejson as json |
40 | 40 |
from django.template.loader import render_to_string |
41 | 41 |
|
42 |
from astakos.im.models import AstakosUser, Service
|
|
42 |
from astakos.im.models import AstakosUser, Component
|
|
43 | 43 |
from snf_django.lib.api import faults |
44 | 44 |
from snf_django.lib.api.utils import isoformat |
45 | 45 |
|
... | ... | |
126 | 126 |
return wrapper |
127 | 127 |
|
128 | 128 |
|
129 |
def service_from_token(func):
|
|
130 |
"""Decorator for authenticating service by it's token.
|
|
129 |
def component_from_token(func):
|
|
130 |
"""Decorator for authenticating component by its token.
|
|
131 | 131 |
|
132 |
Check that a service with the corresponding token exists. Also,
|
|
133 |
if service's token has an expiration token, check that it has not
|
|
132 |
Check that a component with the corresponding token exists. Also,
|
|
133 |
if component's token has an expiration token, check that it has not
|
|
134 | 134 |
expired. |
135 | 135 |
|
136 | 136 |
""" |
... | ... | |
144 | 144 |
if not token: |
145 | 145 |
raise faults.Unauthorized("Invalid X-Auth-Token") |
146 | 146 |
try: |
147 |
service = Service.objects.get(auth_token=token)
|
|
148 |
except Service.DoesNotExist:
|
|
147 |
component = Component.objects.get(auth_token=token)
|
|
148 |
except Component.DoesNotExist:
|
|
149 | 149 |
raise faults.Unauthorized("Invalid X-Auth-Token") |
150 | 150 |
|
151 | 151 |
# Check if the token has expired |
152 |
expiration_date = service.auth_token_expires
|
|
152 |
expiration_date = component.auth_token_expires
|
|
153 | 153 |
if expiration_date: |
154 | 154 |
expires_at = mktime(expiration_date.timetuple()) |
155 | 155 |
if time() > expires_at: |
156 | 156 |
raise faults.Unauthorized("Authentication expired") |
157 | 157 |
|
158 |
request.service_instance = service
|
|
158 |
request.component_instance = component
|
|
159 | 159 |
return func(request, *args, **kwargs) |
160 | 160 |
return wrapper |
161 | 161 |
|
Also available in: Unified diff