Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
env:
isCI: "true"
IN_MEMORY_DATABASE: 'true'
DB_USER: 'user'
DB_USER: 'postgres'
WRITEOPIA_FIREBASE_ID: 'id'
WRITEOPIA_CLIENT_BASE_URL: "baseurl"
JWT_SECRET: "testingsecret"
Expand Down Expand Up @@ -77,7 +77,7 @@ jobs:
env:
isCI: "true"
IN_MEMORY_DATABASE: 'true'
DB_USER: 'user'
DB_USER: 'postgres'
WRITEOPIA_FIREBASE_ID: 'id'
WRITEOPIA_CLIENT_BASE_URL: "baseurl"
JWT_SECRET: "testingsecret"
Expand Down Expand Up @@ -138,7 +138,7 @@ jobs:
env:
isCI: "true"
IN_MEMORY_DATABASE: 'true'
DB_USER: 'user'
DB_USER: 'postgres'
WRITEOPIA_FIREBASE_ID: 'id'
WRITEOPIA_CLIENT_BASE_URL: "baseurl"
JWT_SECRET: "testingsecret"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,11 @@ fun WriteopiaDbBackend.deleteUserById(id: String) {
fun WriteopiaDbBackend.deleteUserByEmail(email: String) {
this.userEntityQueries.deleteUserByEmail(email)
}

fun WriteopiaDbBackend.enableUserByEmail(email: String) {
this.userEntityQueries.enableUserByEmail(email)
}

fun WriteopiaDbBackend.disableUserByEmail(email: String) {
this.userEntityQueries.disableUserByEmail(email)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.writeopia.api.core.auth.routing

import io.ktor.http.HttpStatusCode
import io.ktor.server.request.header
import io.ktor.server.response.respond
import io.ktor.server.routing.Route
import io.ktor.server.routing.RoutingContext
import io.ktor.server.routing.post
import io.ktor.server.routing.route
import io.writeopia.api.core.auth.repository.disableUserByEmail
import io.writeopia.api.core.auth.repository.enableUserByEmail
import io.writeopia.sql.WriteopiaDbBackend

fun Route.adminProtectedRoute(apiKey: String, writeopiaDb: WriteopiaDbBackend) {
route("/admin") {
post("/enable-user/{userEmail}") {
val providedKey = call.request.header("X-Admin-Key")
adminUserFn(apiKey, providedKey, writeopiaDb::enableUserByEmail)
}

post("/disable-user/{userEmail}") {
val providedKey = call.request.header("X-Admin-Key")
adminUserFn(apiKey, providedKey, writeopiaDb::disableUserByEmail)
}
}
}

suspend fun RoutingContext.adminUserFn(
apiKey: String,
providedKey: String?,
func: suspend (String) -> Unit
) {
if (providedKey != apiKey) {
call.respond(HttpStatusCode.Unauthorized, "Invalid admin key")
} else {
val userEmail = call.parameters["userEmail"]
?: return call.respond(HttpStatusCode.BadRequest)

func(userEmail)

call.respond(HttpStatusCode.OK, "User $userEmail enabled")
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.writeopia.api.core.auth
package io.writeopia.api.core.auth.routing

import io.ktor.http.HttpStatusCode
import io.ktor.server.auth.authenticate
Expand All @@ -13,6 +13,8 @@ import io.ktor.server.routing.delete
import io.ktor.server.routing.get
import io.ktor.server.routing.post
import io.ktor.server.routing.put
import io.writeopia.api.core.auth.AuthService
import io.writeopia.api.core.auth.JwtConfig
import io.writeopia.api.core.auth.hash.HashUtils
import io.writeopia.api.core.auth.models.toApi
import io.writeopia.api.core.auth.repository.deleteUserById
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ CREATE TABLE user_entity (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
created_at BIGINT NOT NULL,
email TEXT NOT NULL,
email TEXT NOT NULL UNIQUE,
password TEXT NOT NULL,
salt TEXT NOT NULL,
enabled BOOLEAN NOT NULL,
Expand Down Expand Up @@ -42,3 +42,14 @@ deleteUserByEmail:
DELETE
FROM user_entity
WHERE email = ?;

enableUserByEmail:
UPDATE user_entity
SET enabled = TRUE
WHERE email = ?;

disableUserByEmail:
UPDATE user_entity
SET enabled = FALSE
WHERE email = ?;

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import io.ktor.server.routing.Routing
import io.ktor.server.routing.get
import io.ktor.server.routing.post
import io.ktor.server.routing.route
import io.writeopia.api.core.auth.getUserId
import io.writeopia.api.core.auth.routing.getUserId
import io.writeopia.api.documents.documents.DocumentsService
import io.writeopia.sdk.serialization.json.SendDocumentsRequest
import io.writeopia.api.documents.documents.repository.folderDiff
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ fun main() {
fun Application.module(
writeopiaDb: WriteopiaDbBackend? = configurePersistence(),
useAi: Boolean = System.getenv("WRITEOPIA_USE_AI")?.toBoolean() ?: false,
debugMode: Boolean = System.getenv("WRITEOPIA_DEBUG_MODE")?.toBoolean() ?: false
debugMode: Boolean = System.getenv("WRITEOPIA_DEBUG_MODE")?.toBoolean() ?: false,
adminKey: String? = System.getenv("WRITEOPIA_ADMIN_KEY")
) {
installAuth()
configureRouting(writeopiaDb, useAi, debugMode = debugMode)
configureRouting(writeopiaDb, useAi, debugMode = debugMode, adminKey = adminKey)
configureSerialization()
configureEditorSockets()
configureHTTP()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,30 @@ package io.writeopia.api.geteway
import io.ktor.server.application.Application
import io.ktor.server.response.respondText
import io.ktor.server.routing.get
import io.ktor.server.routing.route
import io.ktor.server.routing.routing
import io.writeopia.api.core.auth.authRoute
import io.writeopia.api.core.auth.routing.adminProtectedRoute
import io.writeopia.api.core.auth.routing.authRoute
import io.writeopia.api.documents.routing.documentsRoute
import io.writeopia.sql.WriteopiaDbBackend

fun Application.configureRouting(
writeopiaDb: WriteopiaDbBackend?,
useAi: Boolean,
debugMode: Boolean = false
debugMode: Boolean = false,
adminKey: String?
) {
routing {
if (writeopiaDb != null) {
documentsRoute(writeopiaDb, useAi, debugMode)
authRoute(writeopiaDb, debugMode)

if (adminKey != null) {
adminProtectedRoute(adminKey, writeopiaDb)
}
}

get {
call.respondText("Hi")
}

if (writeopiaDb != null) {
authRoute(writeopiaDb, debugMode)
}
}
}
2 changes: 1 addition & 1 deletion docker/postgres/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ CREATE TABLE user_entity (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
created_at BIGINT NOT NULL,
email TEXT NOT NULL,
email TEXT NOT NULL UNIQUE,
password TEXT NOT NULL,
salt TEXT NOT NULL,
enabled BOOLEAN NOT NULL,
Expand Down