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
22 changed files
with
123 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
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? | ||
} |
67 changes: 36 additions & 31 deletions
67
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,48 @@ | ||
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()) | ||
} | ||
data class AuthResult(val isAuthenticated: Boolean, val token: String?) | ||
// Bot token | ||
const val BOT_TOKEN = "" | ||
// Function to validate hash | ||
fun isValidHash(): Boolean { | ||
val hash = "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 | ||
} | ||
*/ |
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
17 changes: 17 additions & 0 deletions
17
backend/src/test/kotlin/ru/posidata/backend/UserControllerTest.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 ru.posidata.backend | ||
|
||
import ru.posidata.backend.controller.UserController | ||
|
||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.mockito.Mock | ||
import org.mockito.MockitoAnnotations | ||
import org.springframework.http.HttpStatus | ||
import ru.posidata.backend.service.TelegramAuthService | ||
import ru.posidata.common.ResourceType | ||
import org.junit.jupiter.api.Assertions.* | ||
import org.mockito.Mockito.verifyNoInteractions | ||
|
||
class UserControllerTest { | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...u/posidata/views/utils/internals/Enums.kt → ...onMain/kotlin/ru/posidata/common/Enums.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,4 +1,4 @@ | ||
package ru.posidata.views.utils.internals | ||
package ru.posidata.common | ||
|
||
enum class Selection { | ||
ANSWER, | ||
|
Oops, something went wrong.