generated from orchestr7/react-kotlin-template
-
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
23 changed files
with
190 additions
and
261 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
13 changes: 0 additions & 13 deletions
13
backend/src/jvmMain/kotlin/ru/posidata/backend/BackendApplication.kt
This file was deleted.
Oops, something went wrong.
36 changes: 0 additions & 36 deletions
36
backend/src/jvmMain/kotlin/ru/posidata/backend/controller/TelegramAuthController.kt
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
backend/src/jvmMain/kotlin/ru/posidata/backend/entity/User.kt
This file was deleted.
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
10
backend/src/jvmMain/kotlin/ru/posidata/backend/repository/UserRepository.kt
This file was deleted.
Oops, something went wrong.
43 changes: 0 additions & 43 deletions
43
backend/src/jvmMain/kotlin/ru/posidata/backend/service/TelegramAuthService.kt
This file was deleted.
Oops, something went wrong.
23 changes: 0 additions & 23 deletions
23
backend/src/jvmMain/kotlin/ru/posidata/backend/service/UserService.kt
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
57 changes: 57 additions & 0 deletions
57
backend/src/main/kotlin/ru/posidata/backend/controller/UserController.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,57 @@ | ||
package ru.posidata.backend.controller | ||
|
||
import org.springframework.http.HttpStatus | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.* | ||
import ru.posidata.backend.service.TelegramAuthService | ||
import ru.posidata.backend.service.UserService | ||
import ru.posidata.common.ResourceType | ||
import ru.posidata.common.Resources | ||
|
||
|
||
@RestController | ||
@RequestMapping("api") | ||
class UserController( | ||
private val telegramAuthService: TelegramAuthService, | ||
private val userService: UserService | ||
) { | ||
@GetMapping("/get") | ||
fun getResults( | ||
@RequestParam username: String?, | ||
@RequestParam telegramId: Long, | ||
@RequestParam hash: String, | ||
@RequestParam firstName: String?, | ||
@RequestParam lastName: String?, | ||
): ResponseEntity<Any> { | ||
println("Received a request to get results from $username, $telegramId, $hash, $firstName, $lastName") | ||
userService.findOrCreateUser( | ||
username = username, | ||
telegramId = telegramId, | ||
firstName = firstName, | ||
lastName = lastName | ||
) | ||
return ResponseEntity.status(HttpStatus.OK).body("") | ||
} | ||
|
||
@GetMapping("/answer") | ||
fun submitAnswer( | ||
@RequestParam pokemonName: String, | ||
@RequestParam resourceType: ResourceType, | ||
// @RequestParam gameNumber: Int, | ||
@RequestParam username: String, | ||
@RequestParam hash: String, | ||
): ResponseEntity<Any> { | ||
// user | ||
println(pokemonName) | ||
var count: Int = 0 | ||
val pokemon = Resources.getByName(pokemonName) | ||
?: return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid pokemon name $pokemonName") | ||
if (pokemon.type == resourceType) { | ||
// correct answer | ||
++count | ||
} else { | ||
// incorrect answer | ||
} | ||
return ResponseEntity.status(HttpStatus.OK).body(resourceType) | ||
} | ||
} |
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
4 changes: 2 additions & 2 deletions
4
backend/src/main/kotlin/ru/posidata/backend/repository/UserRepository.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,10 +1,10 @@ | ||
package ru.posidata.backend.repository | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository | ||
import org.springframework.data.repository.CrudRepository | ||
import org.springframework.stereotype.Repository | ||
import ru.posidata.backend.entity.User | ||
|
||
@Repository | ||
interface UserRepository : JpaRepository<User, Long> { | ||
interface UserRepository : CrudRepository<User, Long> { | ||
fun findByTelegramId(telegramId: Long): User? | ||
} |
77 changes: 46 additions & 31 deletions
77
backend/src/main/kotlin/ru/posidata/backend/service/TelegramAuthService.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,43 +1,58 @@ | ||
package ru.posidata.backend.service | ||
|
||
import org.apache.commons.codec.digest.HmacUtils | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.stereotype.Service | ||
import java.security.MessageDigest | ||
import java.util.* | ||
import javax.crypto.Mac | ||
import javax.crypto.spec.SecretKeySpec | ||
|
||
|
||
@Service | ||
class TelegramAuthService( | ||
@Value("\${telegram.bot.token}") private val telegramToken: String, | ||
private val userService: UserService | ||
) { | ||
fun authenticate(request: Map<String, Any>): AuthResult { | ||
val hash = request["hash"] as String | ||
val sortedRequest = request.toMutableMap().apply { remove("hash") } | ||
|
||
val dataCheckString = sortedRequest.entries | ||
.sortedBy { it.key.lowercase(Locale.getDefault()) } | ||
.joinToString("\n") { "${it.key}=${it.value}" } | ||
|
||
val secretKey = SecretKeySpec( | ||
MessageDigest.getInstance("SHA-256").digest(telegramToken.toByteArray(Charsets.UTF_8)), | ||
"HmacSHA256" | ||
) | ||
val mac = Mac.getInstance("HmacSHA256") | ||
mac.init(secretKey) | ||
|
||
val result = mac.doFinal(dataCheckString.toByteArray(Charsets.UTF_8)) | ||
val calculatedHash = result.joinToString("") { "%02x".format(it) } | ||
|
||
return if (hash.equals(calculatedHash, ignoreCase = true)) { | ||
val user = userService.findOrCreateUser(request) | ||
println(user) | ||
AuthResult(true, "token") | ||
} else { | ||
AuthResult(false, null) | ||
} | ||
fun isValidHash(hash: String): Boolean { | ||
val secreteKey = "1289756607:AAFYuBeHguJKYhDVzwSfYFLX4tF5YbkSU7M" | ||
return true | ||
} | ||
|
||
} | ||
|
||
|
||
fun main() { | ||
println(isValidHash()) | ||
} | ||
|
||
// Bot token | ||
const val BOT_TOKEN = "7518590686:AAFCT7m70_ANfOZfIACr2C7bOy0igdLNOpg" | ||
|
||
// Function to validate hash | ||
fun isValidHash(): Boolean { | ||
val hash = "742dda3a019e57821e1fb7acf9918aeb6f0d734cc5c8612913b6696aeab2c745" | ||
|
||
val parsedData: Map<String, String> = mapOf( | ||
"id" to "221298772", | ||
"first_name" to "Андрей", | ||
"last_name" to "Кулешов", | ||
"username" to "akuleshov7", | ||
"photo_url" to "https%3A%2F%2Ft.me%2Fi%2Fuserpic%2F320%2Fd3fKyG306aXHDBCxZXfWTpGlii6fZqZMo1tBmMPEl_E.jpg", | ||
"auth_date" to "1725656645", | ||
"hash" to "742dda3a019e57821e1fb7acf9918aeb6f0d734cc5c8612913b6696aeab2c745" | ||
) | ||
|
||
// Remove 'hash' value & sort alphabetically | ||
val dataKeys = parsedData.keys.filter { it != "hash" }.sorted() | ||
|
||
// Create line format key=<value> | ||
val items = dataKeys.map { key -> "$key=${parsedData[key]}" } | ||
|
||
// Create check string with '\n' as separator | ||
val dataCheckString = items.joinToString("\n") | ||
|
||
val secretKey: ByteArray = HmacUtils("HmacSHA256", "WebAppData").hmac(BOT_TOKEN) | ||
val initDataHash: String = HmacUtils("HmacSHA256", secretKey).hmacHex(dataCheckString) | ||
|
||
println(initDataHash) | ||
println(hash) | ||
// Return whether the generated hash matches the provided hash | ||
return initDataHash == hash | ||
} | ||
|
||
data class AuthResult(val isAuthenticated: Boolean, val token: String?) |
Oops, something went wrong.