Skip to content

Commit

Permalink
initData validation API
Browse files Browse the repository at this point in the history
  • Loading branch information
madhead committed Oct 21, 2023
1 parent b663b09 commit c3cf7f2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
11 changes: 10 additions & 1 deletion launcher/fly/requests.http
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ Content-Type: application/json
"url": "https://{{ngrok}}/{{telegram_token}}"
}

### Validate initData
POST https://{{ngrok}}/app/api/auth/validation
Authorization: Bearer {{api_token}}


### Get group members
GET http://{{local}}/app/api/group/members
GET https://{{ngrok}}/app/api/group/members
Authorization: Bearer {{api_token}}

### Get group currencies
GET https://{{ngrok}}/app/api/group/currencies
Authorization: Bearer {{api_token}}
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package me.madhead.tyzenhaus.launcher.fly.routes

import com.soywiz.krypto.HMAC
import io.ktor.http.HttpStatusCode
import io.ktor.http.decodeURLQueryComponent
import io.ktor.server.application.call
import io.ktor.server.auth.authenticate
import io.ktor.server.auth.principal
import io.ktor.server.config.ApplicationConfig
import io.ktor.server.request.receiveText
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.post
import io.ktor.server.routing.route
import io.ktor.utils.io.core.toByteArray
import me.madhead.tyzenhaus.core.service.GroupCurrenciesService
import me.madhead.tyzenhaus.core.service.GroupMembersService
import me.madhead.tyzenhaus.launcher.fly.security.APITokenPrincipal
import org.koin.ktor.ext.get
import org.koin.ktor.ext.inject

/**
Expand All @@ -22,10 +28,37 @@ fun Route.miniAppAPI() {
val config by inject<ApplicationConfig>()
val groupMembersService by inject<GroupMembersService>()
val groupCurrenciesService by inject<GroupCurrenciesService>()
val webAppDataSecretKeyHash by lazy {
HMAC.hmacSHA256(
"WebAppData".toByteArray(),
this@miniAppAPI.get<ApplicationConfig>().property("telegram.token").getString().toByteArray()
)
}

localPort(config.property("deployment.port").getString().toInt()) {
authenticate("api") {
route("/app/api") {
route("auth") {
post("validation") {
val initData = call.receiveText()
val fields = initData
.decodeURLQueryComponent()
.split("&")
val preparedData = fields
.filterNot { it.startsWith("hash=") }
.sorted()
.joinToString("\n")
val computedHash = HMAC.hmacSHA256(webAppDataSecretKeyHash.bytes, preparedData.toByteArray()).hexLower
val hash = fields.find { it.startsWith("hash=") }?.removePrefix("hash=")?.lowercase()

if (computedHash == hash) {
call.respond(HttpStatusCode.NoContent)
} else {
call.respond(HttpStatusCode.Unauthorized)
}
}
}

route("group") {
get("members") {
val principal = call.principal<APITokenPrincipal>()!!
Expand Down
2 changes: 1 addition & 1 deletion mini-app/src/history/HistoryApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function HistoryApp() {
return (
<div>
<h1>History</h1>
Data: {JSON.stringify(data)}
Init Data: {WebApp.initData}
</div>
);
}
Expand Down

0 comments on commit c3cf7f2

Please sign in to comment.