-
-
Notifications
You must be signed in to change notification settings - Fork 2
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
12 changed files
with
156 additions
and
31 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
32 changes: 26 additions & 6 deletions
32
launcher/fly/src/main/kotlin/me/madhead/tyzenhaus/launcher/fly/routes/MiniAppAPI.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 |
---|---|---|
@@ -1,20 +1,40 @@ | ||
package me.madhead.tyzenhaus.launcher.fly.routes | ||
|
||
import io.ktor.http.ContentType | ||
import io.ktor.http.HttpStatusCode | ||
import io.ktor.server.application.call | ||
import io.ktor.server.response.respondText | ||
import io.ktor.server.auth.authenticate | ||
import io.ktor.server.auth.principal | ||
import io.ktor.server.config.ApplicationConfig | ||
import io.ktor.server.response.respond | ||
import io.ktor.server.routing.Route | ||
import io.ktor.server.routing.get | ||
import io.ktor.server.routing.localPort | ||
import io.ktor.server.routing.route | ||
import java.time.LocalDateTime | ||
import me.madhead.tyzenhaus.core.service.GroupMembersService | ||
import me.madhead.tyzenhaus.launcher.fly.security.APITokenPrincipal | ||
import org.koin.ktor.ext.inject | ||
|
||
/** | ||
* Routes for [Telegram Mini App](https://core.telegram.org/bots/webapps) API. | ||
*/ | ||
fun Route.miniAppAPI() { | ||
route("/app/api") { | ||
get("test") { | ||
call.respondText("""{"result":"ok", "date":"${LocalDateTime.now()}"}""", ContentType.Application.Json) | ||
val config by inject<ApplicationConfig>() | ||
val groupMembersService by inject<GroupMembersService>() | ||
|
||
localPort(config.property("deployment.port").getString().toInt()) { | ||
route("/app/api") { | ||
authenticate("api") { | ||
get("/group/members") { | ||
val principal = call.principal<APITokenPrincipal>()!! | ||
val members = groupMembersService.groupMembers(principal.groupId) | ||
|
||
if (members != null) { | ||
call.respond(members) | ||
} else { | ||
call.respond(HttpStatusCode.NotFound) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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
12 changes: 12 additions & 0 deletions
12
launcher/fly/src/main/kotlin/me/madhead/tyzenhaus/launcher/fly/security/APITokenPrincipal.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,12 @@ | ||
package me.madhead.tyzenhaus.launcher.fly.security | ||
|
||
import io.ktor.server.auth.Principal | ||
import me.madhead.tyzenhaus.entity.api.token.Scope | ||
|
||
/** | ||
* Represents a principal (authentication result) used to access API for the Mini Apps. | ||
*/ | ||
data class APITokenPrincipal( | ||
val groupId: Long, | ||
val scope: Scope, | ||
) : Principal |
33 changes: 33 additions & 0 deletions
33
...her/fly/src/main/kotlin/me/madhead/tyzenhaus/launcher/fly/security/AuthorizationPlugin.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,33 @@ | ||
package me.madhead.tyzenhaus.launcher.fly.security | ||
|
||
import io.ktor.http.HttpStatusCode | ||
import io.ktor.server.application.createRouteScopedPlugin | ||
import io.ktor.server.auth.AuthenticationChecked | ||
import io.ktor.server.auth.principal | ||
import io.ktor.server.response.respond | ||
import me.madhead.tyzenhaus.entity.api.token.Scope | ||
|
||
/** | ||
* Route scoped plugin checking for the required permissions (scope). | ||
*/ | ||
val AuthorizationPlugin = createRouteScopedPlugin( | ||
name = "AuthorizationPlugin", | ||
createConfiguration = ::AuthorizationPluginConfiguration, | ||
) { | ||
pluginConfig.apply { | ||
on(AuthenticationChecked) { call -> | ||
val principal = call.principal<APITokenPrincipal>() | ||
|
||
if (principal?.scope != pluginConfig.scope) { | ||
call.respond(HttpStatusCode.Forbidden) | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Configuration for the [Authorization Plugin][AuthorizationPlugin]. | ||
*/ | ||
data class AuthorizationPluginConfiguration( | ||
var scope: Scope? = null | ||
) |