PageRenderTime 62ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/example_provider/src/main/scala/user.scala

http://github.com/whiter4bbit/oauth
Scala | 51 lines | 43 code | 8 blank | 0 comment | 3 complexity | 9ae7e682cf8ae171631531f7387a2f4c MD5 | raw file
  1. package info.whiter4bbit.oauth.scalatra.example
  2. import com.mongodb.casbah.Imports._
  3. import info.whiter4bbit.oauth.TokenGenerator
  4. import org.slf4j.LoggerFactory
  5. import scala.util.parsing.json._
  6. import scalaz._
  7. import Scalaz._
  8. trait UserMongoCollection {
  9. val users: MongoCollection
  10. }
  11. case class User(val login: String, val password: String, val consumerKey: String, val consumerSecret: String)
  12. trait UserService extends TokenGenerator { self: UserMongoCollection with TokenGenerator =>
  13. val logger = LoggerFactory.getLogger(getClass)
  14. def generateKey = self.generateToken(32)
  15. def create(login: String, password: String): Validation[String, User] = {
  16. val user = User(login, password, generateKey, generateKey)
  17. if (self.users.find(MongoDBObject("login" -> login)).size == 0) {
  18. self.users.insert(MongoDBObject("login" -> login,
  19. "password" -> password,
  20. "consumerKey" -> user.consumerKey,
  21. "consumerSecret" -> user.consumerSecret))
  22. logger.info("User %s has been created" format login)
  23. user.success
  24. } else {
  25. logger.info("User %s already exists" format login)
  26. "User %s already exists".format(login).fail
  27. }
  28. }
  29. def find(consumerKey: String): Validation[String, User] = {
  30. (for {
  31. found <- self.users.findOne(MongoDBObject("consumerKey" -> consumerKey));
  32. login <- found.getAs[String]("login");
  33. password <- found.getAs[String]("password");
  34. conumserKey <- found.getAs[String]("consumerKey");
  35. consumerSecret <- found.getAs[String]("consumerSecret")
  36. } yield {
  37. User(login, password, consumerKey, consumerSecret).success
  38. }).getOrElse({
  39. println("Consumer with key %s not found" format consumerKey)
  40. "Consumer with key %s not found".fail
  41. })
  42. }
  43. }