PageRenderTime 72ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/mongodb/src/main/scala/storage.scala

https://github.com/whiter4bbit/oauth
Scala | 87 lines | 80 code | 7 blank | 0 comment | 0 complexity | 5d3b527de690442fc9d02737d5e8d32f MD5 | raw file
  1. package info.whiter4bbit.oauth.mongodb
  2. import info.whiter4bbit.oauth._
  3. import com.mongodb.casbah.Imports._
  4. import scala.collection.mutable.{HashMap, MultiMap, Set => MSet}
  5. trait OAuthCollections {
  6. val requests: MongoCollection
  7. val nonces: MongoCollection
  8. val consumers: MongoCollection
  9. }
  10. trait OAuthMongoStorage extends OAuthStandardStorage {
  11. self: OAuthCollections =>
  12. override def getConsumer(key: String) = {
  13. self.consumers.findOne(MongoDBObject("consumerKey" -> key)).map((request) => {
  14. Consumer(key, request("consumerSecret").toString)
  15. })
  16. }
  17. override def store(request: ConsumerRequest) = {
  18. self.requests.insert(request match {
  19. case TokenRequest(consumer, callback, token, tokenSecret) => {
  20. MongoDBObject("consumerKey" -> consumer.consumerKey,
  21. "callback" -> callback,
  22. "token" -> token,
  23. "tokenSecret" -> tokenSecret,
  24. "type" -> "token")
  25. }
  26. case VerificationRequest(consumer, callback, verifier, token, tokenSecret) => {
  27. MongoDBObject("consumerKey" -> consumer.consumerKey,
  28. "callback" -> callback,
  29. "verifier" -> verifier,
  30. "token" -> token,
  31. "tokenSecret" -> tokenSecret,
  32. "type" -> "verification")
  33. }
  34. case AccessTokenRequest(consumer, token, tokenSecret) => {
  35. MongoDBObject("consumerKey" -> consumer.consumerKey,
  36. "token" -> token,
  37. "tokenSecret" -> tokenSecret,
  38. "type" -> "access")
  39. }
  40. case ResourceRequest(consumer, token) => {
  41. MongoDBObject("consumerKey" -> consumer.consumerKey,
  42. "token" -> token,
  43. "type" -> "resource")
  44. }
  45. })
  46. }
  47. override def get(key: String) = {
  48. val p = self.requests.find(MongoDBObject("token" -> key))
  49. .sort(MongoDBObject("_id" -> -1)).limit(1)
  50. p.toList.headOption.flatMap( (request) => {
  51. val consumer = getConsumer(request("consumerKey").toString).get
  52. request("type") match {
  53. case "token" => Some(TokenRequest(consumer,
  54. request("callback").toString,
  55. request("token").toString,
  56. request("tokenSecret").toString))
  57. case "verification" => Some(VerificationRequest(consumer,
  58. request("callback").toString,
  59. request("verifier").toString,
  60. request("token").toString,
  61. request("tokenSecret").toString))
  62. case "access" => Some(AccessTokenRequest(consumer,
  63. request("token").toString,
  64. request("tokenSecret").toString))
  65. case "resource" => Some(ResourceRequest(consumer,
  66. request("token").toString))
  67. case _ => None
  68. }
  69. })
  70. }
  71. override def addNonce(timestamp: String, nonce: String) = {
  72. nonces += MongoDBObject("timestamp" -> timestamp,
  73. "nonce" -> nonce)
  74. }
  75. override def getNonces(timestamp: String): Set[String] = {
  76. nonces.find(MongoDBObject("timestamp" -> timestamp)).map( (resp) => {
  77. resp("nonce").toString
  78. }).toSet
  79. }
  80. }