Revision 08494423

b/snf-astakos-app/astakos/im/api/service.py
66 66
                        service = Service.objects.get(auth_token=x_auth_token)
67 67

  
68 68
                        # Check if the token has expired.
69
                        if (time() - mktime(service.auth_token_expires.timetuple())) > 0:
70
                            raise Unauthorized('Authentication expired')
69
                        if service.auth_token_expires:
70
                            if (time() - mktime(service.auth_token_expires.timetuple())) > 0:
71
                                raise Unauthorized('Authentication expired')
71 72
                    except Service.DoesNotExist, e:
72 73
                        raise Unauthorized('Invalid X-Auth-Token')
73 74
                response = func(request, *args, **kwargs)
b/snf-astakos-app/astakos/im/management/commands/service-token-renew.py
31 31
# interpreted as representing official policies, either expressed
32 32
# or implied, of GRNET S.A.
33 33

  
34
from optparse import make_option
35
from datetime import datetime, timedelta
36

  
34 37
from django.core.management.base import BaseCommand, CommandError
35 38

  
36 39
from astakos.im.models import Service
......
40 43
    args = "<name>"
41 44
    help = "Renew service token"
42 45

  
46
    option_list = BaseCommand.option_list + (
47
        make_option('--duration',
48
                    dest='duration',
49
                    metavar='NUM',
50
                    help="In how many days token is going to expire."),)
51

  
43 52
    def handle(self, *args, **options):
44 53
        if len(args) != 1:
45 54
            raise CommandError("Invalid number of arguments")
46 55

  
47 56
        try:
48 57
            service = Service.objects.get(name=args[0])
49
            service.renew_token()
58
            expires = None
59
            if options['duration']:
60
                try:
61
                    duration = int(options['duration'])
62
                except ValueError, e:
63
                    raise CommandError('Invalid duration')
64
                expires = datetime.now() + timedelta(days=duration)
65
            service.renew_token(expires)
50 66
            service.save()
51 67
            self.stdout.write('New service token: %s\n' % service.auth_token)
52 68
        except Service.DoesNotExist:
b/snf-astakos-app/astakos/im/models.py
111 111
    auth_token_expires = models.DateTimeField(
112 112
        _('Token expiration date'), null=True)
113 113

  
114
    def renew_token(self):
114
    def renew_token(self, expiration_date=None):
115 115
        md5 = hashlib.md5()
116 116
        md5.update(self.name.encode('ascii', 'ignore'))
117 117
        md5.update(self.url.encode('ascii', 'ignore'))
......
119 119

  
120 120
        self.auth_token = b64encode(md5.digest())
121 121
        self.auth_token_created = datetime.now()
122
        self.auth_token_expires = self.auth_token_created + \
123
            timedelta(hours=AUTH_TOKEN_DURATION)
122
        if expiration_date:
123
            self.auth_token_expires = expiration_date
124
        else:
125
            self.auth_token_expires = None
124 126

  
125 127
    def __str__(self):
126 128
        return self.name

Also available in: Unified diff