501f63ae9ebe0b458a1091aa9875c647f4c6762a
[django-cas-provider.git] / cas_provider / utils.py
1 from random import Random
2 import string
3
4 from models import ServiceTicket, LoginTicket
5
6 def _generate_string(length=8, chars=string.letters + string.digits):
7     return ''.join(Random().sample(string.letters+string.digits, length))
8
9 def create_service_ticket(user, service):
10     ticket_string = 'ST-' + _generate_string(29) # Total ticket length = 29 + 3 = 32
11     ticket = ServiceTicket(service=service, user=user, ticket=ticket_string)
12     ticket.save()
13     return ticket
14
15 def create_login_ticket():
16     ticket_string = 'LT-' + _generate_string(29)
17     ticket = LoginTicket(ticket=ticket_string)
18     ticket.save()
19     return ticket_string