andrewoma
01/17/2017, 1:01 AMval server = embeddedJettyServer(9797) {
intercept(ApplicationCallPipeline.Infrastructure) { it.attributes.put(GsonAttribute, gson) }
install(CallLogging)
withSessions<Session> {
withCookieByValue {
settings = SessionCookiesSettings(transformers = listOf(SessionCookieTransformerMessageAuthentication(hashKey)))
}
}
routing {
post("/api/login") {
val login = call.fromJson<UserPasswordCredential>()
val user = stores.users.values.firstOrNull { it.name == login.name }
if (user != null && BCrypt.checkpw(login.password, user.password)) {
call.session(Session(nextNonce(), login.name))
call.toJson(UserIdPrincipal(login.name))
} else {
call.clearSession()
call.toJson(Errors(squawk.server.Error(ErrorCode.UNAUTHORISED, "Invalid user or password")), HttpStatusCode.BadRequest)
}
}
route("api") {
authentication {
intercept(AuthenticationPipeline.RequestAuthentication) { context ->
if (context.call.sessionOrNull<Session>() == null) {
context.call.respond(UnauthorizedResponse(*emptyArray()))
}
}
crud(this@route, "users", stores.users)
crud(this@route, "foos", stores.foos)
...
}
}
...
}
}