1 # -*- coding: utf-8 -*-
2 from django
.conf
import settings
3 from django
.contrib
.auth
.models
import User
4 from django
.db
import models
6 import md5
, random
, sys
, os
, time
8 __all__
= ['Nonce', 'Association', 'UserAssociation',
9 'UserPasswordQueueManager', 'UserPasswordQueue']
11 class Nonce(models
.Model
):
13 server_url
= models
.CharField(max_length
=255)
14 timestamp
= models
.IntegerField()
15 salt
= models
.CharField(max_length
=40)
17 def __unicode__(self
):
18 return u
"Nonce: %s" % self
.id
21 class Association(models
.Model
):
22 """ association openid url and lifetime """
23 server_url
= models
.TextField(max_length
=2047)
24 handle
= models
.CharField(max_length
=255)
25 secret
= models
.TextField(max_length
=255) # Stored base64 encoded
26 issued
= models
.IntegerField()
27 lifetime
= models
.IntegerField()
28 assoc_type
= models
.TextField(max_length
=64)
30 def __unicode__(self
):
31 return u
"Association: %s, %s" % (self
.server_url
, self
.handle
)
33 class UserAssociation(models
.Model
):
35 model to manage association between openid and user
37 openid_url
= models
.CharField(blank
=False, max_length
=255)
38 user
= models
.ForeignKey(User
, unique
=True)
40 def __unicode__(self
):
41 return "Openid %s with user %s" % (self
.openid_url
, self
.user
)
43 class UserPasswordQueueManager(models
.Manager
):
44 """ manager for UserPasswordQueue object """
45 def get_new_confirm_key(self
):
46 "Returns key that isn't being used."
47 # The random module is seeded when this Apache child is created.
48 # Use SECRET_KEY as added salt.
50 confirm_key
= md5
.new("%s%s%s%s" % (
51 random
.randint(0, sys
.maxint
- 1), os
.getpid(),
52 time
.time(), settings
.SECRET_KEY
)).hexdigest()
54 self
.get(confirm_key
=confirm_key
)
55 except self
.model
.DoesNotExist
:
60 class UserPasswordQueue(models
.Model
):
62 model for new password queue.
64 user
= models
.ForeignKey(User
, unique
=True)
65 new_password
= models
.CharField(max_length
=30)
66 confirm_key
= models
.CharField(max_length
=40)
68 objects
= UserPasswordQueueManager()
70 def __unicode__(self
):
71 return self
.user
.username