/database/associations.py

https://github.com/terrasea/oauth-2.0-provider
Python | 166 lines | 125 code | 25 blank | 16 comment | 29 complexity | 8b1804330482b12ff35e3bfc1a0ed8b9 MD5 | raw file
  1. from DB import DB
  2. from errors import *
  3. from models import Association
  4. from tokens import get_token, delete_token
  5. from client import get_client
  6. from user import get_user
  7. import logging
  8. #import transaction
  9. from copy import deepcopy
  10. def isassociated(user_id, client_id, refresh_token_str):
  11. db = DB()
  12. try:
  13. key = 'client_association_' + str(user_id)
  14. if db.contains(key):
  15. association = db.get(key)
  16. return client_id in association.clients
  17. else:
  18. return False
  19. except Exception, e:
  20. logging.error('isassociated: ' + str(e))
  21. raise e
  22. finally:
  23. db.close()
  24. return False
  25. def associate_client_with_user(user_id, client_id, refresh_token_str):
  26. """
  27. Adds client to list of authorised clients who can access the users resources on a long term basis
  28. """
  29. client = get_client(client_id)
  30. user = get_user(user_id)
  31. refresh_token = get_token(client.id, client.secret, refresh_token_str)
  32. ## before going further, see if client is confidential or not.
  33. ## If confidential then it is assumed to be able to keep the
  34. ## username and password secret from itself. If this is the
  35. ## case then it's allowed to continue, else throw a
  36. ## ConfindentialError.
  37. if client.type.lower() != 'confidential':
  38. client_id = refresh_token.client
  39. raise ConfidentailError('Client ' + client_id + \
  40. ' is not a confidentail client')
  41. db = DB()
  42. try:
  43. key = 'client_association_' + str(user.id)
  44. if db.contains(key):
  45. association = db.get(key)
  46. if client.id not in association.clients:
  47. association.clients[client.id] = refresh_token.code
  48. db.update(key, association)
  49. else:
  50. raise AssociationExistsWarning(''.join(['Client ',
  51. str(client.id),
  52. ' is already associated with ',
  53. str(user.id)]))
  54. else:
  55. association = Association(user.id)
  56. association.clients[client.id] = refresh_token.code
  57. db.put(key, association)
  58. db.commit()
  59. except Exception, e:
  60. logging.error(''.join(['associate_client_with_user: ', str(e)]))
  61. raise e
  62. db.abort()
  63. finally:
  64. db.close()
  65. def get_associations(user):
  66. db = DB()
  67. try:
  68. key = 'client_association_' + str(user.id)
  69. if db.contains(key):
  70. return deepcopy(db.get(key))
  71. except Exception, e:
  72. logging.error('get_associations: ' + str(e))
  73. finally:
  74. db.close()
  75. return False
  76. def update_association(user_id, client_id, refresh_token_str):
  77. client = get_client(client_id)
  78. user = get_user(user_id)
  79. logging.warn('update_associations 1: ' + str(refresh_token_str))
  80. refresh_token = get_token(client_id, client.secret, refresh_token_str)
  81. #always check to see if it is confidential or not.
  82. #it shouldn't be if it's using update_association, but you never know
  83. #and it's good to have a log message to possible alert the admin that
  84. #this is going on.
  85. if client.type.lower() != 'confidential':
  86. raise ConfidentailError('Client ' + client_id + \
  87. ' is not a confidentail client')
  88. db = DB()
  89. try:
  90. key = 'client_association_' + str(user.id)
  91. if db.contains(key):
  92. association = db.get(key)
  93. if client.id in association.clients:
  94. logging.warn('update_associations 2: ' + str(association.clients[client.id]))
  95. old_refresh = get_token(client.id, client.secret, association.clients[client.id])
  96. delete_token(old_refresh.access_token)
  97. delete_token(old_refresh.code)
  98. association.clients[client.id] = refresh_token.code
  99. logging.warn('update_associations 3: ' + str(refresh_token.code) + ', ' + str(association.clients[client.id]))
  100. db.update(key, association)
  101. db.commit()
  102. #except Exception, e:
  103. # logging.error('update_associations: ' + str(e))
  104. # db.abort()
  105. finally:
  106. db.close()
  107. return False
  108. if __name__ == '__main__':
  109. from user import add_user, get_user, delete_user
  110. from client import create_client, get_client, delete_client, client_exists
  111. from models import AccessToken, RefreshToken
  112. add_user('jim', 'password')
  113. user = get_user('jim')
  114. if not client_exists('bobby fiet3'):
  115. client = create_client('bob',
  116. 'bobby fiet3',
  117. 'iamcool',
  118. 'http://whaever.com')
  119. else:
  120. client = get_client('bobby fiet3')
  121. access_token = AccessToken(client, user)
  122. refresh_token = RefreshToken(access_token, client, user)
  123. db = DB()
  124. try:
  125. db.put(access_token.code, access_token)
  126. db.put(refresh_token.code, refresh_token)
  127. db.commit()
  128. finally:
  129. db.close()
  130. try:
  131. associate_client_with_user(user, client, refresh_token.code)
  132. except:
  133. pass
  134. associations = get_associations(user)
  135. print
  136. print associations.user.id, associations.user.password
  137. print
  138. print 'clients'
  139. print
  140. for x in associations.clients:
  141. print x