How to implement Generic slick dao with oauth2 in scala

143 Views Asked by At

To work with OAuth2, Slick, Akka-HTTP in scala, I'm using the code from cdiniz https://github.com/cdiniz/slick-akka-http-oauth2.

I tried to create a DAO: UserDao and respective API: UserApi to fetch all records(in JSON) from database after successful verification of OAuth2.

BaseDao Code:

trait BaseDao[T,A] {
  ...
  def getAll(): Future[Seq[A]]
}

class BaseDaoImpl[T <: BaseTable[A], A <: BaseEntity]()(implicit val tableQ: TableQuery[T], implicit val db: JdbcProfile#Backend#Database,implicit val profile: JdbcProfile) extends BaseDao[T,A] with Profile with DbModule {

  import profile.api._

  ...
  override def getAll(): Future[Seq[A]] = {
    db.run(tableQ.result)
  }

}

UserDao Code:

object UserService {
  val modules = new ConfigurationServiceImpl with ActorServiceImpl with PersistenceServiceImpl
  import modules._    
  class UserDao extends BaseDaoImpl[UserTable,UserEntity1] {}

}

UserApi Code:

//using io.circe for json encoder
class UserApi(val modules: Configuration with PersistenceService, val db: DatabaseService)(implicit executionContext: ExecutionContext) extends BaseApi {
  override val oauth2DataHandler = modules.oauth2DataHandler

  val userService = new UserDao
  val testApi = pathPrefix("auth") {
    path("users") {
      pathEndOrSingleSlash {
        get {
          authenticateOAuth2Async[AuthInfo[OauthAccount]]("realm", oauth2Authenticator) {
            auth => complete(userService.getAll().map(_.asJson))
          }

        }
      }
    }
  }
}

Supporting Codes:

trait Profile {
  val profile: JdbcProfile
}


trait DbModule extends Profile{
  val db: JdbcProfile#Backend#Database
}

trait PersistenceService {
  val accountsDao: AccountsDao
  val oAuthAuthorizationCodesDao: OAuthAuthorizationCodesDao
  val oauthClientsDao: OAuthClientsDao
  val oauthAccessTokensDao:  OAuthAccessTokensDao
  val oauth2DataHandler : DataHandler[OauthAccount]
}


trait PersistenceServiceImpl extends PersistenceService with DbModule{
  this: Configuration  =>

  private val dbConfig : DatabaseConfig[JdbcProfile]  = DatabaseConfig.forConfig[JdbcProfile]("mysqldb")

  override implicit val profile: JdbcProfile = dbConfig.driver
  override implicit val db: JdbcProfile#Backend#Database = dbConfig.db


  override val accountsDao = new AccountsDaoImpl
  override val oAuthAuthorizationCodesDao = new OAuthAuthorizationCodesDaoImpl
  override val oauthClientsDao = new OAuthClientsDaoImpl(this)
  override val oauthAccessTokensDao = new  OAuthAccessTokensDaoImpl(this)
  override val oauth2DataHandler = new OAuth2DataHandler(this)
}

Here, The code runs without any issue but doesn't get any records as the response though there are records.

What is the problem in this code that I cannot look into? Any suggestion would be appreciable.

0

There are 0 best solutions below