Artur Bosch
03/02/2017, 7:37 PMfun PipelineContext<ApplicationCall>.withValidCredentials(body: PipelineContext<ApplicationCall>.(ApplicationCall) -> Unit) {
val credentials = call.request.basicAuthenticationCredentials()
val principal = authenticate(credentials)
if (principal != null) {
body.invoke(this, call)
} else throw InvalidCredentialsException()
}
and I use it like this:
fun Routing.backup() {
post("/backup") {
withValidCredentials {
val multipart = call.request.content.get<MultiPartData>()
val credentials = call.request.basicAuthenticationCredentials()!!
call.response.contentType(ContentType.Text.Plain)
call.respondWrite {
if (!call.request.isMultipart()) {
appendln("Not a multipart request")
} else {
multipart.parts.forEach { handlePart(it, credentials.name) }
}
}
}
}
}
because withValidCredentionals is not defined on a Route the call.respondWrite
is invalid