Skip to content

Commit

Permalink
add optional conf isPublicFileServer
Browse files Browse the repository at this point in the history
if true router registers the serve file endpoint as public otherwise
behind auth (default is false)
  • Loading branch information
zingmane committed Mar 13, 2024
1 parent 962b3ac commit b978eef
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/main/scala/com/campudus/tableaux/Starter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ object Starter {
val DEFAULT_WORKING_DIRECTORY = "./"
val DEFAULT_UPLOADS_DIRECTORY = "uploads/"
val DEFAULT_ROLE_PERMISSIONS_PATH = "./role-permissions.json"
val DEFAULT_IS_PUBLIC_FILE_SERVER = false
}

class Starter extends ScalaVerticle with LazyLogging {
Expand Down Expand Up @@ -54,6 +55,7 @@ class Starter extends ScalaVerticle with LazyLogging {
val authConfig = config.getJsonObject("auth", Json.obj())
val rolePermissionsPath = getStringDefault(config, "rolePermissionsPath", Starter.DEFAULT_ROLE_PERMISSIONS_PATH)
val openApiUrl = Option(getStringDefault(config, "openApiUrl", null))
val isPublicFileServer = config.getBoolean("isPublicFileServer", Starter.DEFAULT_IS_PUBLIC_FILE_SERVER)

val rolePermissions = FileUtils(vertxAccessContainer()).readJsonFile(rolePermissionsPath, Json.emptyObj())

Expand All @@ -64,7 +66,8 @@ class Starter extends ScalaVerticle with LazyLogging {
workingDirectory = workingDirectory,
uploadsDirectory = uploadsDirectory,
rolePermissions = rolePermissions,
openApiUrl = openApiUrl
openApiUrl = openApiUrl,
isPublicFileServer = isPublicFileServer
)

connection = SQLConnection(vertxAccessContainer(), databaseConfig)
Expand Down
3 changes: 2 additions & 1 deletion src/main/scala/com/campudus/tableaux/TableauxConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class TableauxConfig(
workingDirectory: String,
uploadsDirectory: String,
val rolePermissions: JsonObject,
val openApiUrl: Option[String] = None
val openApiUrl: Option[String] = None,
val isPublicFileServer: Boolean = false
) extends VertxAccess {

def uploadsDirectoryPath(): Path = {
Expand Down
14 changes: 13 additions & 1 deletion src/main/scala/com/campudus/tableaux/router/MediaRouter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ class MediaRouter(override val config: TableauxConfig, val controller: MediaCont
router.get(folders).handler(retrieveRootFolder)
router.getWithRegex(folder).handler(retrieveFolder)
router.getWithRegex(file).handler(retrieveFile)
router.getWithRegex(fileLangStatic).handler(serveFile)
if (!config.isPublicFileServer) {
router.getWithRegex(fileLangStatic).handler(serveFile)
}

router.deleteWithRegex(folder).handler(deleteFolder)
router.deleteWithRegex(file).handler(deleteFile)
Expand All @@ -78,6 +80,16 @@ class MediaRouter(override val config: TableauxConfig, val controller: MediaCont
router
}

def publicRoute: Router = {
val router = Router.router(vertx)

// RETRIEVE
if (config.isPublicFileServer) {
router.getWithRegex(fileLangStatic).handler(serveFile)
}
router
}

private def getFolderId(context: RoutingContext): Option[Long] = {
implicit val user = TableauxUser(context)
getLongParam("folderId", context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ object RouterRegistry extends LazyLogging {
router.route().handler(systemRouter.noRouteMatched)
}

def registerPublicRoutes(router: Router) = {
logger.info("Registering public routes")
router.mountSubRouter("/", mediaRouter.publicRoute)
}

def initManualAuth() = {
val keycloakAuthProvider = KeycloakAuth.create(vertx, tableauxConfig.authConfig)
val keycloakAuthHandler = OAuth2AuthHandler.create(keycloakAuthProvider)
Expand All @@ -89,6 +94,8 @@ object RouterRegistry extends LazyLogging {
clientOptions,
handler => {
if (handler.succeeded()) {
registerPublicRoutes(mainRouter)

val keycloakAuthProvider = handler.result()
val keycloakAuthHandler = OAuth2AuthHandler.create(keycloakAuthProvider)
mainRouter.route().handler(keycloakAuthHandler)
Expand Down Expand Up @@ -122,6 +129,7 @@ object RouterRegistry extends LazyLogging {
"Started WITHOUT access token verification. The API is completely publicly available and NOT secured! " +
"This is for development and/or testing purposes ONLY."
)
registerPublicRoutes(mainRouter)
registerCommonRoutes(mainRouter)
}

Expand Down

0 comments on commit b978eef

Please sign in to comment.