<commit_before>package org.gradschoolsearch.www.Auth

import javax.servlet.http.{HttpServletRequest, HttpServletResponse}

import org.gradschoolsearch.db.Tables._
import org.gradschoolsearch.models.User
import org.mindrot.jbcrypt.BCrypt
import org.scalatra.ScalatraBase
import org.scalatra.auth.strategy.{BasicAuthStrategy, BasicAuthSupport}
import org.scalatra.auth.{ScentryConfig, ScentrySupport}

import scala.slick.driver.MySQLDriver.simple._
import scala.slick.jdbc.JdbcBackend.Database.dynamicSession


class OurBasicAuthStrategy(protected override val app: ScalatraBase, realm: String, db:Database)
  extends BasicAuthStrategy[User](app, realm) {

  // Why do we need this?
  protected def getUserId(user: User)
                         (implicit request: HttpServletRequest, response: HttpServletResponse): String = {
    user.id.toString
  }

  // Required method that returns a matching user or None
  protected def validate(email: String, password: String)
                        (implicit request: HttpServletRequest, response: HttpServletResponse): Option[User] = {
    db withDynSession {
      val userOpt = users.filter(_.email === email).firstOption
      userOpt match {
        case None => None
        case Some(user) => if (BCrypt.checkpw(password, user.passwordHash)) Some(user) else None
      }
    }
  }

  override def authenticate()(implicit request: HttpServletRequest, response: HttpServletResponse) = {
    println("in authenticate")
    println("request")
    println(request)
    println(request.username)
    println(request.password)
    println("response")
    println(response)
    validate(request.username, request.password)
  }

  override def unauthenticated()(implicit request: HttpServletRequest, response: HttpServletResponse) {
    println("IN UNAUTH")
  }

}


trait AuthenticationSupport extends ScentrySupport[User] with BasicAuthSupport[User] {
  self: ScalatraBase =>

  val db:Database

  // What are these vars?
  val realm = "Scalatra Basic Auth Example"
  protected val scentryConfig = (new ScentryConfig {}).asInstanceOf[ScentryConfiguration]

  // User object -> session string
  protected def toSession   = { case usr: User => usr.id.get.toString }

  // Session string -> User object
  protected def fromSession = { case id: String =>
    db withDynSession {
      users.filter(_.id === id.toInt).firstOption.get
    }
  }

  protected def ourBasicAuth()(implicit request: HttpServletRequest, response: HttpServletResponse) = {
    println("IN OUR BASIC AUTH!")
    // TAKE THE REQUEST
    // if username and password, try authenticating?
    //
    scentry.authenticate("Basic")
  }

  // Registers our Auth strategy with Scentry
  override protected def registerAuthStrategies = {
    scentry.register("Basic", app => new OurBasicAuthStrategy(app, realm, db))
  }

  // Not sure what this does
  override protected def configureScentry = {
    scentry.unauthenticated {
      scentry.strategies("Basic").unauthenticated()
    }
  }


}<commit_msg>Load the username and password from the request body. Needs to be simplified cleaned up.<commit_after>package org.gradschoolsearch.www.Auth

import javax.servlet.http.{HttpServletRequest, HttpServletResponse}

import org.gradschoolsearch.db.Tables._
import org.gradschoolsearch.models.User
import org.mindrot.jbcrypt.BCrypt
import org.scalatra.ScalatraBase
import org.scalatra.auth.strategy.{BasicAuthStrategy, BasicAuthSupport}
import org.scalatra.auth.{ScentryConfig, ScentrySupport}

import scala.slick.driver.MySQLDriver.simple._
import scala.slick.jdbc.JdbcBackend.Database.dynamicSession
import net.iharder.Base64
import scala.io.Codec
import org.scalatra.auth.strategy.BasicAuthStrategy.BasicAuthRequest
import org.gradschoolsearch.www.Auth.OurBasicAuthStrategy.OurBasicAuthRequest

object OurBasicAuthStrategy {
  class OurBasicAuthRequest(r: HttpServletRequest) {

    def username2 = r.getParameter("username")
    def password2 = r.getParameter("password")
  }
}

class OurBasicAuthStrategy(protected override val app: ScalatraBase, realm: String, db:Database)
  extends BasicAuthStrategy[User](app, realm) {

  implicit def request2OurBasicAuthRequest(r: HttpServletRequest) = new OurBasicAuthRequest(r)

  // Why do we need this?
  protected def getUserId(user: User)
                         (implicit request: HttpServletRequest, response: HttpServletResponse): String = {
    user.id.toString
  }

  // Required method that returns a matching user or None
  protected def validate(email: String, password: String)
                        (implicit request: HttpServletRequest, response: HttpServletResponse): Option[User] = {
    db withDynSession {
      val userOpt = users.filter(_.email === email).firstOption
      userOpt match {
        case None => None
        case Some(user) => if (BCrypt.checkpw(password, user.passwordHash)) Some(user) else None
      }
    }
  }

  override def authenticate()(implicit request: HttpServletRequest, response: HttpServletResponse) = {
    println("in authenticate")
    println("request")
    println(request)
    println(request.username2)
    println(request.password2)
    println("response")
    println(response)
    validate(request.username2, request.password2)
  }

  override def unauthenticated()(implicit request: HttpServletRequest, response: HttpServletResponse) {
    println("IN UNAUTH")
  }

}


trait AuthenticationSupport extends ScentrySupport[User] with BasicAuthSupport[User] {
  self: ScalatraBase =>

  val db:Database

  // What are these vars?
  val realm = "Scalatra Basic Auth Example"
  protected val scentryConfig = (new ScentryConfig {}).asInstanceOf[ScentryConfiguration]

  // User object -> session string
  protected def toSession   = { case usr: User => usr.id.get.toString }

  // Session string -> User object
  protected def fromSession = { case id: String =>
    db withDynSession {
      users.filter(_.id === id.toInt).firstOption.get
    }
  }

  protected def ourBasicAuth()(implicit request: HttpServletRequest,
                                                                 response: HttpServletResponse) = {
    val baReq = new OurBasicAuthStrategy.OurBasicAuthRequest(request)
    /*if(!baReq.providesAuth) {
      response.setHeader("WWW-Authenticate", "Basic realm=\"%s\"" format realm)
      halt(401, "Unauthenticated")
    }
    if(!baReq.isBasicAuth) {
      halt(400, "Bad Request")
    }*/
    scentry.authenticate("Basic")
  }

  // Registers our Auth strategy with Scentry
  override protected def registerAuthStrategies = {
    scentry.register("Basic", app => new OurBasicAuthStrategy(app, realm, db))
  }

  // Not sure what this does
  override protected def configureScentry = {
    scentry.unauthenticated {
      scentry.strategies("Basic").unauthenticated()
    }
  }


}
{'fraction_non_alphanumeric': 0.09709165687426558, 'fraction_numerical': 0.0022032902467685076, 'mean_word_length': 3.795070422535211, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4666324', 'n_tokens_mistral': 1973, 'n_tokens_neox': 1907, 'n_words': 474}