-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
111 changed files
with
8,457 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
Pano/src/main/kotlin/com/panomc/platform/route/api/GetFaviconAPI.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.panomc.platform.route.api | ||
|
||
import com.panomc.platform.annotation.Endpoint | ||
import com.panomc.platform.config.ConfigManager | ||
import com.panomc.platform.model.Api | ||
import com.panomc.platform.model.Path | ||
import com.panomc.platform.model.Result | ||
import com.panomc.platform.model.RouteType | ||
import io.vertx.ext.web.RoutingContext | ||
import io.vertx.json.schema.SchemaParser | ||
import java.io.File | ||
|
||
@Endpoint | ||
class GetFaviconAPI(private val configManager: ConfigManager) : Api() { | ||
override val paths = listOf(Path("/api/favicon", RouteType.GET)) | ||
|
||
override fun getValidationHandler(schemaParser: SchemaParser) = null | ||
|
||
override suspend fun handle(context: RoutingContext): Result? { | ||
val faviconPath = configManager.getConfig().getJsonObject("file-paths").getString("favicon") | ||
|
||
if (faviconPath == null) { | ||
sendDefault(context) | ||
|
||
return null | ||
} | ||
|
||
val path = configManager.getConfig().getString("file-uploads-folder") + File.separator + | ||
faviconPath | ||
|
||
val file = File(path) | ||
|
||
if (!file.exists()) { | ||
sendDefault(context) | ||
|
||
return null | ||
} | ||
|
||
context.response().sendFile(path) | ||
|
||
return null | ||
} | ||
|
||
private fun sendDefault(context: RoutingContext) { | ||
context.response().sendFile("assets/img/minecraft-icon.png") | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
Pano/src/main/kotlin/com/panomc/platform/route/api/GetPostThumbnailAPI.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.panomc.platform.route.api | ||
|
||
import com.panomc.platform.AppConstants | ||
import com.panomc.platform.annotation.Endpoint | ||
import com.panomc.platform.config.ConfigManager | ||
import com.panomc.platform.model.Api | ||
import com.panomc.platform.model.Path | ||
import com.panomc.platform.model.Result | ||
import com.panomc.platform.model.RouteType | ||
import io.vertx.ext.web.RoutingContext | ||
import io.vertx.ext.web.validation.ValidationHandler | ||
import io.vertx.ext.web.validation.builder.Parameters.param | ||
import io.vertx.ext.web.validation.builder.ValidationHandlerBuilder | ||
import io.vertx.json.schema.SchemaParser | ||
import io.vertx.json.schema.common.dsl.Schemas.stringSchema | ||
import java.io.File | ||
|
||
@Endpoint | ||
class GetPostThumbnailAPI(private val configManager: ConfigManager) : Api() { | ||
override val paths = listOf(Path("/api/post/thumbnail/:filename", RouteType.GET)) | ||
|
||
override fun getValidationHandler(schemaParser: SchemaParser): ValidationHandler = | ||
ValidationHandlerBuilder.create(schemaParser) | ||
.pathParameter(param("filename", stringSchema())) | ||
.build() | ||
|
||
override suspend fun handle(context: RoutingContext): Result? { | ||
val parameters = getParameters(context) | ||
|
||
val filename = parameters.pathParameter("filename").string | ||
|
||
val path = configManager.getConfig() | ||
.getString("file-uploads-folder") + "/${AppConstants.DEFAULT_POST_THUMBNAIL_UPLOAD_PATH}/" + | ||
filename | ||
|
||
val file = File(path) | ||
|
||
if (!file.exists()) { | ||
context.response().setStatusCode(404).end() | ||
|
||
return null | ||
} | ||
|
||
context.response().sendFile(path) | ||
|
||
return null | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
Pano/src/main/kotlin/com/panomc/platform/route/api/GetSiteInfoAPI.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.panomc.platform.route.api | ||
|
||
import com.panomc.platform.Main.Companion.VERSION | ||
import com.panomc.platform.annotation.Endpoint | ||
import com.panomc.platform.config.ConfigManager | ||
import com.panomc.platform.model.* | ||
import io.vertx.ext.web.RoutingContext | ||
import io.vertx.json.schema.SchemaParser | ||
|
||
@Endpoint | ||
class GetSiteInfoAPI(private val configManager: ConfigManager) : Api() { | ||
override val paths = listOf(Path("/api/siteInfo", RouteType.GET)) | ||
|
||
override fun getValidationHandler(schemaParser: SchemaParser) = null | ||
|
||
override suspend fun handle(context: RoutingContext): Result { | ||
val response = mutableMapOf<String, Any>() | ||
val config = configManager.getConfig() | ||
|
||
response["locale"] = config.getString("locale") | ||
response["websiteName"] = config.getString("website-name") | ||
response["websiteDescription"] = config.getString("website-description") | ||
response["keywords"] = config.getJsonArray("keywords") | ||
response["panoVersion"] = VERSION | ||
|
||
return Successful(response) | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
Pano/src/main/kotlin/com/panomc/platform/route/api/GetWebsiteLogoAPI.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.panomc.platform.route.api | ||
|
||
import com.panomc.platform.annotation.Endpoint | ||
import com.panomc.platform.config.ConfigManager | ||
import com.panomc.platform.model.Api | ||
import com.panomc.platform.model.Path | ||
import com.panomc.platform.model.Result | ||
import com.panomc.platform.model.RouteType | ||
import io.vertx.ext.web.RoutingContext | ||
import io.vertx.json.schema.SchemaParser | ||
import java.io.File | ||
|
||
@Endpoint | ||
class GetWebsiteLogoAPI(private val configManager: ConfigManager) : Api() { | ||
override val paths = listOf(Path("/api/websiteLogo", RouteType.GET)) | ||
|
||
override fun getValidationHandler(schemaParser: SchemaParser) = null | ||
|
||
override suspend fun handle(context: RoutingContext): Result? { | ||
val websiteLogoPath = configManager.getConfig().getJsonObject("file-paths").getString("websiteLogo") | ||
|
||
if (websiteLogoPath == null) { | ||
sendDefault(context) | ||
|
||
return null | ||
} | ||
|
||
val path = configManager.getConfig().getString("file-uploads-folder") + File.separator + | ||
websiteLogoPath | ||
|
||
val file = File(path) | ||
|
||
if (!file.exists()) { | ||
sendDefault(context) | ||
|
||
return null | ||
} | ||
|
||
context.response().sendFile(path) | ||
|
||
return null | ||
} | ||
|
||
private fun sendDefault(context: RoutingContext) { | ||
context.response().sendFile("assets/img/minecraft-logo.png") | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
Pano/src/main/kotlin/com/panomc/platform/route/api/TestAPI.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.panomc.platform.route.api | ||
|
||
import com.panomc.platform.annotation.Endpoint | ||
import com.panomc.platform.model.* | ||
import io.vertx.ext.web.RoutingContext | ||
import io.vertx.json.schema.SchemaParser | ||
|
||
@Endpoint | ||
class TestAPI : Api() { | ||
override val paths = listOf(Path("/test", RouteType.GET)) | ||
|
||
override fun getValidationHandler(schemaParser: SchemaParser) = null | ||
|
||
override suspend fun handle(context: RoutingContext): Result? { | ||
return Successful() | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
Pano/src/main/kotlin/com/panomc/platform/route/api/VisitorVisitAPI.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.panomc.platform.route.api | ||
|
||
|
||
import com.panomc.platform.annotation.Endpoint | ||
import com.panomc.platform.db.DatabaseManager | ||
import com.panomc.platform.db.model.WebsiteView | ||
import com.panomc.platform.error.InvalidIpAddress | ||
import com.panomc.platform.model.* | ||
import io.vertx.ext.web.RoutingContext | ||
import io.vertx.json.schema.SchemaParser | ||
|
||
@Endpoint | ||
class VisitorVisitAPI(private val databaseManager: DatabaseManager) : Api() { | ||
override val paths = listOf(Path("/api/visitorVisit", RouteType.POST)) | ||
|
||
override fun getValidationHandler(schemaParser: SchemaParser) = null | ||
|
||
override suspend fun handle(context: RoutingContext): Result { | ||
val ipAddress = context.request().remoteAddress().host() | ||
|
||
validateIpAddress(ipAddress) | ||
|
||
val sqlClient = getSqlClient() | ||
|
||
val exists = databaseManager.websiteViewDao.isIpAddressExistsByToday(ipAddress, sqlClient) | ||
|
||
if (exists) { | ||
databaseManager.websiteViewDao.increaseTimesByOne(ipAddress, sqlClient) | ||
} else { | ||
databaseManager.websiteViewDao.add(WebsiteView(ipAddress = ipAddress), sqlClient) | ||
} | ||
|
||
return Successful() | ||
} | ||
|
||
private fun validateIpAddress(ipAddress: String) { | ||
if (!ipAddress.matches(Regex("^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\\.(?!\$)|\$)){4}\$"))) { | ||
throw InvalidIpAddress() | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
Pano/src/main/kotlin/com/panomc/platform/route/api/auth/GetCredentialsAPI.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.panomc.platform.route.api.auth | ||
|
||
import com.panomc.platform.annotation.Endpoint | ||
import com.panomc.platform.auth.AuthProvider | ||
import com.panomc.platform.db.DatabaseManager | ||
import com.panomc.platform.model.* | ||
import io.vertx.ext.web.RoutingContext | ||
import io.vertx.json.schema.SchemaParser | ||
|
||
@Endpoint | ||
class GetCredentialsAPI( | ||
private val authProvider: AuthProvider, | ||
private val databaseManager: DatabaseManager | ||
) : LoggedInApi() { | ||
override val paths = listOf(Path("/api/auth/credentials", RouteType.GET)) | ||
|
||
override fun getValidationHandler(schemaParser: SchemaParser) = null | ||
|
||
override suspend fun handle(context: RoutingContext): Result { | ||
val userId = authProvider.getUserIdFromRoutingContext(context) | ||
|
||
val sqlClient = getSqlClient() | ||
|
||
val user = databaseManager.userDao.getById(userId, sqlClient)!! | ||
|
||
return Successful( | ||
mapOf( | ||
"username" to user.username, | ||
"email" to user.email, | ||
"panelAccess" to (user.permissionGroupId != -1L) | ||
) | ||
) | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
Pano/src/main/kotlin/com/panomc/platform/route/api/auth/LoginAPI.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.panomc.platform.route.api.auth | ||
|
||
import com.panomc.platform.AppConstants | ||
import com.panomc.platform.annotation.Endpoint | ||
import com.panomc.platform.auth.AuthProvider | ||
import com.panomc.platform.db.DatabaseManager | ||
import com.panomc.platform.model.* | ||
import com.panomc.platform.util.CSRFTokenGenerator | ||
import io.vertx.core.http.Cookie | ||
import io.vertx.ext.web.RoutingContext | ||
import io.vertx.ext.web.validation.RequestPredicate | ||
import io.vertx.ext.web.validation.ValidationHandler | ||
import io.vertx.ext.web.validation.builder.Bodies | ||
import io.vertx.ext.web.validation.builder.ValidationHandlerBuilder | ||
import io.vertx.json.schema.SchemaParser | ||
import io.vertx.json.schema.common.dsl.Schemas | ||
|
||
@Endpoint | ||
class LoginAPI( | ||
private val authProvider: AuthProvider, | ||
private val databaseManager: DatabaseManager | ||
) : Api() { | ||
override val paths = listOf(Path("/api/auth/login", RouteType.POST)) | ||
|
||
override fun getValidationHandler(schemaParser: SchemaParser): ValidationHandler = | ||
ValidationHandlerBuilder.create(schemaParser) | ||
.body( | ||
Bodies.json( | ||
Schemas.objectSchema() | ||
.property("usernameOrEmail", Schemas.stringSchema()) | ||
.property("password", Schemas.stringSchema()) | ||
.property("rememberMe", Schemas.booleanSchema()) | ||
.property("recaptcha", Schemas.stringSchema()) | ||
) | ||
) | ||
.predicate(RequestPredicate.BODY_REQUIRED) | ||
.build() | ||
|
||
override suspend fun handle(context: RoutingContext): Result { | ||
val parameters = getParameters(context) | ||
val data = parameters.body().jsonObject | ||
|
||
val usernameOrEmail = data.getString("usernameOrEmail") | ||
val password = data.getString("password") | ||
val rememberMe = data.getBoolean("rememberMe") | ||
val recaptcha = data.getString("recaptcha") | ||
|
||
authProvider.validateInput(usernameOrEmail, password, recaptcha) | ||
|
||
val sqlClient = getSqlClient() | ||
|
||
authProvider.authenticate(usernameOrEmail, password, sqlClient) | ||
|
||
val token = authProvider.login(usernameOrEmail, sqlClient) | ||
|
||
val userId = databaseManager.userDao.getUserIdFromUsernameOrEmail(usernameOrEmail, sqlClient)!! | ||
|
||
databaseManager.userDao.updateLastLoginDate(userId, sqlClient) | ||
|
||
val csrfToken = CSRFTokenGenerator.nextToken() | ||
|
||
val response = context.response() | ||
|
||
val jwtCookie = Cookie.cookie(AppConstants.COOKIE_PREFIX + AppConstants.JWT_COOKIE_NAME, token) | ||
val csrfTokenCookie = Cookie.cookie(AppConstants.COOKIE_PREFIX + AppConstants.CSRF_TOKEN_COOKIE_NAME, csrfToken) | ||
|
||
jwtCookie.path = "/" | ||
jwtCookie.isHttpOnly = true | ||
|
||
csrfTokenCookie.path = "/" | ||
csrfTokenCookie.isHttpOnly = true | ||
|
||
response.addCookie(jwtCookie) | ||
response.addCookie(csrfTokenCookie) | ||
|
||
return Successful( | ||
mapOf( | ||
"CSRFToken" to csrfToken | ||
) | ||
) | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
Pano/src/main/kotlin/com/panomc/platform/route/api/auth/LogoutAPI.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.panomc.platform.route.api.auth | ||
|
||
import com.panomc.platform.AppConstants | ||
import com.panomc.platform.annotation.Endpoint | ||
import com.panomc.platform.auth.AuthProvider | ||
import com.panomc.platform.model.* | ||
import io.vertx.ext.web.RoutingContext | ||
import io.vertx.json.schema.SchemaParser | ||
|
||
@Endpoint | ||
class LogoutAPI( | ||
private val authProvider: AuthProvider | ||
) : LoggedInApi() { | ||
override val paths = listOf(Path("/api/auth/logout", RouteType.POST)) | ||
|
||
override fun getValidationHandler(schemaParser: SchemaParser) = null | ||
|
||
override suspend fun handle(context: RoutingContext): Result { | ||
val sqlClient = getSqlClient() | ||
|
||
authProvider.logout(context, sqlClient) | ||
|
||
val response = context.response() | ||
|
||
response.putHeader( | ||
"Set-Cookie", | ||
"${AppConstants.COOKIE_PREFIX + AppConstants.JWT_COOKIE_NAME}=deleted; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT" | ||
) | ||
response.putHeader( | ||
"Set-Cookie", | ||
"${AppConstants.COOKIE_PREFIX + AppConstants.CSRF_TOKEN_COOKIE_NAME}=deleted; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT" | ||
) | ||
|
||
return Successful() | ||
} | ||
} |
Oops, something went wrong.