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.
### What's done: - new backend project - script for deployment - small changes to frontend
- Loading branch information
Showing
21 changed files
with
278 additions
and
208 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
plugins { | ||
kotlin("multiplatform") | ||
kotlin("plugin.spring") version "1.9.25" | ||
id("org.springframework.boot") version "3.3.3" | ||
id("io.spring.dependency-management") version "1.1.6" | ||
kotlin("plugin.jpa") version "1.9.25" | ||
} | ||
|
||
kotlin { | ||
jvm() | ||
sourceSets { | ||
jvmMain.dependencies { | ||
implementation("org.springframework.boot:spring-boot-starter-data-jpa") | ||
implementation("org.springframework.boot:spring-boot-starter-oauth2-client") | ||
// implementation("org.springframework.boot:spring-boot-starter-security") | ||
implementation("org.springframework.boot:spring-boot-starter-validation") | ||
implementation("org.springframework.boot:spring-boot-starter-web") | ||
implementation("com.fasterxml.jackson.module:jackson-module-kotlin") | ||
implementation("org.jetbrains.kotlin:kotlin-reflect") | ||
implementation("io.jsonwebtoken:jjwt-api:0.11.5") | ||
implementation("com.h2database:h2:2.3.232") | ||
runtimeOnly("io.jsonwebtoken:jjwt-impl:0.11.5") | ||
runtimeOnly("io.jsonwebtoken:jjwt-jackson:0.11.5") | ||
} | ||
|
||
jvmToolchain { | ||
languageVersion.set(JavaLanguageVersion.of(21)) | ||
} | ||
} | ||
|
||
tasks.withType<Test> { | ||
useJUnitPlatform() | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
backend/src/jvmMain/kotlin/ru/posidata/backend/BackendApplication.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,13 @@ | ||
package ru.posidata.backend | ||
|
||
import org.springframework.boot.autoconfigure.SpringBootApplication | ||
import org.springframework.boot.runApplication | ||
import org.springframework.context.annotation.ComponentScan | ||
|
||
@SpringBootApplication | ||
@ComponentScan(basePackages = ["ru.posidata"]) | ||
class BackendApplication | ||
|
||
fun main(args: Array<String>) { | ||
runApplication<BackendApplication>(*args) | ||
} |
36 changes: 36 additions & 0 deletions
36
backend/src/jvmMain/kotlin/ru/posidata/backend/controller/TelegramAuthController.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,36 @@ | ||
package ru.posidata.backend.controller | ||
|
||
import org.springframework.http.HttpStatus | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.security.core.Authentication | ||
import org.springframework.web.bind.annotation.* | ||
import ru.posidata.backend.service.TelegramAuthService | ||
|
||
|
||
@RestController | ||
@RequestMapping(path = ["/api/v1/"]) | ||
class TelegramAuthController(private val telegramAuthService: TelegramAuthService) { | ||
@GetMapping("/user-info") | ||
fun getSelfUserInfo(authentication: Authentication?): String { | ||
return authentication?.principal.toString() | ||
} | ||
|
||
@PostMapping("/test") | ||
fun test(@RequestBody request: Map<String, Any>): String { | ||
return "$request" | ||
} | ||
|
||
@PostMapping("auth/telegram") | ||
fun telegramAuth(@RequestBody request: Map<String, Any>): ResponseEntity<Any> { | ||
return try { | ||
val authResult = telegramAuthService.authenticate(request) | ||
if (authResult.isAuthenticated) { | ||
ResponseEntity.ok(authResult.token) | ||
} else { | ||
ResponseEntity.status(HttpStatus.FORBIDDEN).body("Login info hash mismatch") | ||
} | ||
} catch (e: Exception) { | ||
ResponseEntity.status(HttpStatus.FORBIDDEN).body("Server error while authenticating") | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
backend/src/jvmMain/kotlin/ru/posidata/backend/entity/User.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,15 @@ | ||
package ru.posidata.backend.entity | ||
|
||
import jakarta.persistence.* | ||
|
||
@Entity | ||
@Table(name = "users") | ||
data class User( | ||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
val id: Long = 0, | ||
@Column(unique = true) | ||
val telegramId: Long, | ||
val firstName: String, | ||
val lastName: String?, | ||
val username: String? | ||
) |
10 changes: 10 additions & 0 deletions
10
backend/src/jvmMain/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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package ru.posidata.backend.repository | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository | ||
import org.springframework.stereotype.Repository | ||
import ru.posidata.backend.entity.User | ||
|
||
@Repository | ||
interface UserRepository : JpaRepository<User, Long> { | ||
fun findByTelegramId(telegramId: Long): User? | ||
} |
43 changes: 43 additions & 0 deletions
43
backend/src/jvmMain/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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package ru.posidata.backend.service | ||
|
||
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) | ||
} | ||
} | ||
} | ||
|
||
data class AuthResult(val isAuthenticated: Boolean, val token: String?) |
23 changes: 23 additions & 0 deletions
23
backend/src/jvmMain/kotlin/ru/posidata/backend/service/UserService.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,23 @@ | ||
package ru.posidata.backend.service | ||
|
||
import org.springframework.stereotype.Service | ||
import ru.posidata.backend.entity.User | ||
import ru.posidata.backend.repository.UserRepository | ||
|
||
@Service | ||
class UserService(private val userRepository: UserRepository) { | ||
fun findOrCreateUser(telegramData: Map<String, Any>): User { | ||
val telegramId = telegramData["id"] as Long | ||
return userRepository.findByTelegramId(telegramId) ?: createUser(telegramData) | ||
} | ||
|
||
private fun createUser(telegramData: Map<String, Any>): User { | ||
val user = User( | ||
telegramId = telegramData["id"] as Long, | ||
firstName = telegramData["first_name"] as String, | ||
lastName = telegramData["last_name"] as? String, | ||
username = telegramData["username"] as? String | ||
) | ||
return userRepository.save(user) | ||
} | ||
} |
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,8 @@ | ||
spring: | ||
application: | ||
name: | ||
backend | ||
|
||
telegram: | ||
bot: | ||
token: |
13 changes: 13 additions & 0 deletions
13
backend/src/test/kotlin/ru/posidata/backend/BackendApplicationTests.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,13 @@ | ||
package ru.posidata.backend | ||
|
||
import org.junit.jupiter.api.Test | ||
import org.springframework.boot.test.context.SpringBootTest | ||
|
||
@SpringBootTest | ||
class BackendApplicationTests { | ||
|
||
@Test | ||
fun contextLoads() { | ||
} | ||
|
||
} |
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,5 +1,5 @@ | ||
plugins { | ||
java | ||
kotlin("multiplatform") version("2.0.0") apply(false) | ||
} | ||
|
||
group = "ru.posidata" | ||
|
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,5 +1,5 @@ | ||
plugins { | ||
kotlin("multiplatform") version("2.0.0") | ||
kotlin("multiplatform") | ||
} | ||
|
||
kotlin { | ||
|
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
Oops, something went wrong.