Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

# -*- coding: utf-8 -*- 

# Copyright 2007, 2008, 2009 by Benoît Chesneau <benoitc@e-engura.org> 

#  

# Licensed under the Apache License, Version 2.0 (the "License"); 

# you may not use this file except in compliance with the License. 

# You may obtain a copy of the License at 

# 

#     http://www.apache.org/licenses/LICENSE-2.0 

# 

# Unless required by applicable law or agreed to in writing, software 

# distributed under the License is distributed on an "AS IS" BASIS, 

# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

# See the License for the specific language governing permissions and 

# limitations under the License. 

# 

 

import base64 

import operator 

import time 

import urllib 

try: 

    from hashlib import md5 as _md5 

except ImportError: 

    import md5 

    _md5 = md5.new 

 

from django.db.models.query import Q 

from django.conf import settings 

from openid.association import Association as OIDAssociation 

import openid.store.interface 

import openid.store 

 

from madrona.openid.models import Association, Nonce 

from madrona.openid.utils import OpenID 

 

class DjangoOpenIDStore(openid.store.interface.OpenIDStore): 

    def __init__(self): 

        self.max_nonce_age = 6 * 60 * 60 # Six hours 

 

    def storeAssociation(self, server_url, association): 

        assoc = Association( 

            server_url=server_url, 

            handle=association.handle, 

            secret=base64.encodestring(association.secret), 

            issued=association.issued, 

            lifetime=association.lifetime, 

            assoc_type=association.assoc_type 

        ) 

        assoc.save() 

 

    def getAssociation(self, server_url, handle=None): 

        assocs = [] 

        if handle is not None: 

            assocs = Association.objects.filter( 

                server_url=server_url, 

                handle=handle 

            ) 

        else: 

            assocs = Association.objects.filter( 

                server_url=server_url 

            ) 

        if not assocs: 

            return None 

        associations = [] 

        expired = [] 

        for assoc in assocs: 

            association = OIDAssociation( 

                assoc.handle, base64.decodestring(assoc.secret), assoc.issued, 

                assoc.lifetime, assoc.assoc_type 

            ) 

            if association.getExpiresIn() == 0: 

                expired.append(assoc) 

            else: 

                associations.append((association.issued, association)) 

 

        for assoc in expired: 

            assoc.delete() 

        if not associations: 

            return None 

        associations.sort() 

        return associations[-1][1] 

 

    def removeAssociation(self, server_url, handle): 

        assocs = list(Association.objects.filter( 

            server_url=server_url, 

            handle=handle 

        )) 

        assocs_exist = len(assocs) > 0 

        for assoc in assocs: 

            assoc.delete() 

        return assocs_exist 

 

    def useNonce(self, server_url, timestamp, salt): 

        if abs(timestamp - time.time()) > openid.store.nonce.SKEW: 

            return False 

 

        query = [ 

                Q(server_url__exact=server_url), 

                Q(timestamp__exact=timestamp), 

                Q(salt__exact=salt), 

        ] 

        try: 

            ononce = Nonce.objects.get(reduce(operator.and_, query)) 

        except Nonce.DoesNotExist: 

            ononce = Nonce( 

                    server_url=server_url, 

                    timestamp=timestamp, 

                    salt=salt 

            ) 

            ononce.save() 

            return True 

 

        return False 

 

    def cleanupNonces(self, _now=None): 

        if _now is None: 

            _now = int(time.time()) 

        expired = Nonce.objects.filter(timestamp__lt=(_now - openid.store.nonce.SKEW)) 

        count = expired.count() 

        if count: 

            expired.delete() 

        return count 

 

    def cleanupAssociations(self): 

        now = int(time.time()) 

        expired = Association.objects.extra( 

            where=['issued + lifetime < %d' % now]) 

        count = expired.count() 

        if count: 

            expired.delete() 

        return count 

 

    def getAuthKey(self): 

        # Use first AUTH_KEY_LEN characters of md5 hash of SECRET_KEY 

        return _md5(settings.SECRET_KEY).hexdigest()[:self.AUTH_KEY_LEN] 

 

    def isDumb(self): 

        return False