Hexa
09/13/2018, 5:51 PMclass ValidateUserHandler : RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
private val logger = Logger.getLogger(ValidateUserHandler::class.java)
override fun handleRequest(input: APIGatewayProxyRequestEvent?, context: Context?): APIGatewayProxyResponseEvent {
try {
val token: String? by lazy {
if (input!!.headers["Authorization"]?.startsWith("Bearer ") == true) {
val token = input.headers["Authorization"]?.substring("Bearer ".length)?.trim()
if (!token.isNullOrEmpty()) token else null
} else {
null
}
}
val authoriserResult = validateToken(token!!)
return when (authoriserResult.authorised) {
true -> {
APIGatewayProxyResponseEvent()
.withBody("OK")
.withStatusCode(200)
}
else -> {
APIGatewayProxyResponseEvent()
.withBody("Unauthorized}")
.withStatusCode(401)
}
}
} catch (ex: Exception) {
logger.error("Unexpected error occured: $ex", ex)
return APIGatewayProxyResponseEvent()
.withBody("System error")
.withStatusCode(500)
}
}
}