Created
June 10, 2015 14:10
-
-
Save felipehummel/3750ce4bb1f62b74ce1b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.busk.searchserver.api | |
import javax.servlet.http.{HttpServletRequest, HttpServletResponse} | |
import org.scalatra.ScalatraBase | |
import org.scalatra.auth.strategy.{BasicAuthStrategy, BasicAuthSupport} | |
import org.scalatra.auth.{ScentryConfig, ScentrySupport} | |
/** | |
* Created by felipehummel on 12/01/15. | |
*/ | |
class SpixApiBasicAuthStrategy(userRepository: ApiUserRepository, protected override val app: ScalatraBase, realm: String) | |
extends BasicAuthStrategy[ApiUser](app, realm) { | |
protected def validate(apiKey: String, password: String) | |
(implicit request: HttpServletRequest, response: HttpServletResponse): Option[ApiUser] = { | |
userRepository.userFromCredentials(apiKey, password) | |
} | |
protected def getUserId(user: ApiUser) | |
(implicit request: HttpServletRequest, response: HttpServletResponse): String = | |
user.apiKey | |
} | |
trait SpixBasicAuthenticationSupport extends ScentrySupport[ApiUser] with BasicAuthSupport[ApiUser] { | |
self: ScalatraBase => | |
val realm = "Spix API Basic Auth" | |
before() { | |
basicAuth() | |
} | |
def userRepository: ApiUserRepository | |
protected def fromSession = { | |
case apiKey: String => | |
userRepository.userFromApiKey(apiKey).getOrElse(throw new Exception(s"Retrieving user ApiKey ($apiKey) from a session but ApiKey does not exists")) | |
} | |
protected def toSession = { case usr: ApiUser => usr.apiKey } | |
protected val scentryConfig = (new ScentryConfig {}).asInstanceOf[ScentryConfiguration] | |
override protected def configureScentry = { | |
scentry.unauthenticated { | |
scentry.strategies("Basic").unauthenticated() | |
} | |
} | |
override protected def registerAuthStrategies = { | |
scentry.register("Basic", app => new SpixApiBasicAuthStrategy(userRepository, app, realm)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment