Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / api / service.py @ ee7a2b87

History | View | Annotate | Download (3.8 kB)

1
# Copyright 2011-2013 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
from time import time, mktime
35
from functools import wraps
36
from django.views.decorators.csrf import csrf_exempt
37

    
38
from . import  __get_uuid_displayname_catalogs, __send_feedback
39
from snf_django.lib import api
40
from snf_django.lib.api import faults
41
from astakos.im.models import Service
42

    
43
import logging
44
logger = logging.getLogger(__name__)
45

    
46

    
47
def service_from_token(func):
48
    """Decorator for authenticating service by it's token.
49

50
    Check that a service with the corresponding token exists. Also,
51
    if service's token has an expiration token, check that it has not
52
    expired.
53

54
    """
55
    @wraps(func)
56
    def wrapper(request, *args, **kwargs):
57
        try:
58
            token = request.x_auth_token
59
        except AttributeError:
60
            raise faults.Unauthorized("No authentication token")
61

    
62
        if not token:
63
            raise faults.Unauthorized("Invalid X-Auth-Token")
64
        try:
65
            service = Service.objects.get(auth_token=token)
66
        except Service.DoesNotExist:
67
            raise faults.Unauthorized("Invalid X-Auth-Token")
68

    
69
        # Check if the token has expired
70
        expiration_date = service.auth_token_expires
71
        if expiration_date:
72
            expires_at = mktime(expiration_date.timetuple())
73
            if time() > expires_at:
74
                raise faults.Unauthorized("Authentication expired")
75

    
76
        return func(request, *args, **kwargs)
77
    return wrapper
78

    
79

    
80
@csrf_exempt
81
@api.api_method(http_method='POST', token_required=True, user_required=False,
82
            logger=logger)
83
@service_from_token  # Authenticate service !!
84
def get_uuid_displayname_catalogs(request):
85
    # Normal Response Codes: 200
86
    # Error Response Codes: internalServerError (500)
87
    #                       badRequest (400)
88
    #                       unauthorised (401)
89
    return __get_uuid_displayname_catalogs(request, user_call=False)
90

    
91

    
92
@csrf_exempt
93
@api.api_method(http_method='POST', token_required=True, user_required=False,
94
            logger=logger)
95
@service_from_token  # Authenticate service !!
96
def send_feedback(request, email_template_name='im/feedback_mail.txt'):
97
    # Normal Response Codes: 200
98
    # Error Response Codes: internalServerError (500)
99
    #                       badRequest (400)
100
    #                       unauthorised (401)
101
    return __send_feedback(request, email_template_name)