Add Bearer Authentication to REST API

This commit is contained in:
Torsten Grote
2018-09-19 12:45:27 -03:00
parent 98d1ea7730
commit d6d132a9cf
2 changed files with 27 additions and 3 deletions

View File

@@ -17,6 +17,7 @@ import java.lang.System.setProperty
import java.nio.file.Files.setPosixFilePermissions import java.nio.file.Files.setPosixFilePermissions
import java.nio.file.attribute.PosixFilePermission import java.nio.file.attribute.PosixFilePermission
import java.nio.file.attribute.PosixFilePermission.* import java.nio.file.attribute.PosixFilePermission.*
import java.util.UUID.randomUUID
import java.util.logging.Level.* import java.util.logging.Level.*
import java.util.logging.LogManager import java.util.logging.LogManager
@@ -63,14 +64,17 @@ class Main : CliktCommand(
setProperty(DEFAULT_LOG_LEVEL_KEY, levelSlf4j); setProperty(DEFAULT_LOG_LEVEL_KEY, levelSlf4j);
LogManager.getLogManager().getLogger("").level = level LogManager.getLogManager().getLogger("").level = level
val dataDir = getDataDir()
val authToken = getOrCreateAuthToken(dataDir)
val app = val app =
DaggerBriarHeadlessApp.builder().headlessModule(HeadlessModule(getDataDir())).build() DaggerBriarHeadlessApp.builder().headlessModule(HeadlessModule(dataDir)).build()
// We need to load the eager singletons directly after making the // We need to load the eager singletons directly after making the
// dependency graphs // dependency graphs
BrambleCoreModule.initEagerSingletons(app) BrambleCoreModule.initEagerSingletons(app)
BriarCoreModule.initEagerSingletons(app) BriarCoreModule.initEagerSingletons(app)
app.router().start(port, debug) app.router().start(authToken, port, debug)
} }
private fun getDataDir(): File { private fun getDataDir(): File {
@@ -87,6 +91,19 @@ class Main : CliktCommand(
setPosixFilePermissions(file.toPath(), perms); setPosixFilePermissions(file.toPath(), perms);
return file return file
} }
private fun getOrCreateAuthToken(dataDir: File): String {
val tokenFile = File(dataDir, "auth_token")
return if (tokenFile.isFile) {
tokenFile.readText()
} else {
// TODO use better way of getting random token?
val authToken = randomUUID().toString()
tokenFile.writeText(authToken)
authToken
}
}
} }
fun main(args: Array<String>) = Main().main(args) fun main(args: Array<String>) = Main().main(args)

View File

@@ -25,7 +25,7 @@ constructor(
private val blogController: BlogController private val blogController: BlogController
) { ) {
fun start(port: Int, debug: Boolean) { fun start(authToken: String, port: Int, debug: Boolean) {
briarService.start() briarService.start()
getRuntime().addShutdownHook(Thread(Runnable { briarService.stop() })) getRuntime().addShutdownHook(Thread(Runnable { briarService.stop() }))
@@ -39,6 +39,13 @@ constructor(
if (debug) app.enableDebugLogging() if (debug) app.enableDebugLogging()
app.start() app.start()
app.accessManager { handler, ctx, _ ->
if (ctx.header("Authorization") == "Bearer $authToken") {
handler.handle(ctx)
} else {
ctx.status(401).result("Unauthorized")
}
}
app.routes { app.routes {
path("/v1") { path("/v1") {
path("/contacts") { path("/contacts") {