Remove nuxt + 100 other things..
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package be.vandewalleh.auth
|
||||
|
||||
import be.vandewalleh.extensions.ApplicationBuilder
|
||||
import io.ktor.application.*
|
||||
import io.ktor.auth.*
|
||||
import io.ktor.auth.jwt.*
|
||||
import io.ktor.http.*
|
||||
import io.ktor.http.auth.*
|
||||
import io.ktor.request.*
|
||||
import io.ktor.response.*
|
||||
import io.ktor.util.pipeline.*
|
||||
|
||||
class AuthenticationModule(authJwt: SimpleJWT) : ApplicationBuilder({
|
||||
install(Authentication) {
|
||||
jwt {
|
||||
verifier(authJwt.verifier)
|
||||
authHeader { call ->
|
||||
val token = call.request.header(HttpHeaders.Authorization)
|
||||
?: call.request.cookies["Authorization"]
|
||||
token?.let {
|
||||
try {
|
||||
parseAuthorizationHeader(it)
|
||||
} catch (e: IllegalArgumentException) {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
validate {
|
||||
UserPrincipal(
|
||||
id = it.payload.getClaim("id").asInt(),
|
||||
username = it.payload.getClaim("username").asString()
|
||||
)
|
||||
}
|
||||
challenge { scheme, realm ->
|
||||
authChallenge(scheme, realm)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
private suspend fun PipelineContext<*, ApplicationCall>.authChallenge(scheme: String, realm: String) {
|
||||
if (call.request.uri.startsWith("/api"))
|
||||
call.respond(
|
||||
UnauthorizedResponse(
|
||||
HttpAuthHeader.Parameterized(
|
||||
scheme,
|
||||
mapOf(HttpAuthHeader.Parameters.Realm to realm)
|
||||
)
|
||||
)
|
||||
)
|
||||
else {
|
||||
call.respondRedirect("/login")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package be.vandewalleh.auth
|
||||
|
||||
import com.auth0.jwt.JWT
|
||||
import com.auth0.jwt.JWTVerifier
|
||||
import com.auth0.jwt.algorithms.Algorithm
|
||||
import java.util.*
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
class SimpleJWT(secret: String, validity: Long, unit: TimeUnit) {
|
||||
private val validityInMs = TimeUnit.MILLISECONDS.convert(validity, unit)
|
||||
private val algorithm = Algorithm.HMAC256(secret)
|
||||
|
||||
val verifier: JWTVerifier = JWT.require(algorithm).build()
|
||||
fun sign(id: Int, username: String): String = JWT.create()
|
||||
.withClaim("id", id)
|
||||
.withClaim("username", username)
|
||||
.withExpiresAt(getExpiration())
|
||||
.sign(algorithm)
|
||||
|
||||
private fun getExpiration() = Date(System.currentTimeMillis() + validityInMs)
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package be.vandewalleh.auth
|
||||
|
||||
import io.ktor.auth.*
|
||||
|
||||
/**
|
||||
* Represents a simple user's principal identified by it's [id] and [username]
|
||||
* @property id
|
||||
* @property username
|
||||
*/
|
||||
data class UserPrincipal(val id: Int, val username: String) : Principal
|
||||
@@ -0,0 +1,10 @@
|
||||
package be.vandewalleh.auth
|
||||
|
||||
import io.ktor.auth.*
|
||||
|
||||
/**
|
||||
* Represents a simple user [username] and [password] credential pair
|
||||
* @property username
|
||||
* @property password
|
||||
*/
|
||||
data class UsernamePasswordCredential(val username: String, val password: String) : Credential
|
||||
Reference in New Issue
Block a user