fixed
This commit is contained in:
parent
949c96b7ca
commit
87d06133d9
@ -1,8 +1,8 @@
|
|||||||
package ru.myitschool.work.core
|
package ru.myitschool.work.core
|
||||||
|
|
||||||
object Constants {
|
object Constants {
|
||||||
const val HOST = "http://10.0.0.12:49165"
|
const val HOST = "http://10.0.0.103:49165"
|
||||||
const val AUTH_URL = "/auth"
|
const val AUTH_URL = "/login"
|
||||||
const val INFO_URL = "/info"
|
const val INFO_URL = "/info"
|
||||||
const val BOOKING_URL = "/booking"
|
const val BOOKING_URL = "/booking"
|
||||||
const val BOOK_URL = "/book"
|
const val BOOK_URL = "/book"
|
||||||
|
|||||||
@ -1,58 +0,0 @@
|
|||||||
package ru.myitschool.work.data
|
|
||||||
|
|
||||||
import android.util.Base64
|
|
||||||
import javax.crypto.Cipher
|
|
||||||
import javax.crypto.SecretKeyFactory
|
|
||||||
import javax.crypto.spec.IvParameterSpec
|
|
||||||
import javax.crypto.spec.PBEKeySpec
|
|
||||||
import javax.crypto.spec.SecretKeySpec
|
|
||||||
|
|
||||||
|
|
||||||
object AESEncyption {
|
|
||||||
|
|
||||||
const val secretKey = "tK5Ugskdkipokuodvknfdk3434weofnf="
|
|
||||||
const val salt = "QLlGNHNhYTJTQWZ2bGhpV3U="
|
|
||||||
const val iv = "bVQqNFNhRkQ1Njc4UUFaPA=="
|
|
||||||
|
|
||||||
fun encrypt(strToEncrypt: String): String? {
|
|
||||||
try {
|
|
||||||
val ivParameterSpec = IvParameterSpec(Base64.decode(iv, Base64.DEFAULT))
|
|
||||||
|
|
||||||
val factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1")
|
|
||||||
val spec =
|
|
||||||
PBEKeySpec(secretKey.toCharArray(), Base64.decode(salt, Base64.DEFAULT), 10000, 256)
|
|
||||||
val tmp = factory.generateSecret(spec)
|
|
||||||
val secretKey = SecretKeySpec(tmp.encoded, "AES")
|
|
||||||
|
|
||||||
val cipher = Cipher.getInstance("AES/CBC/PKCS7Padding")
|
|
||||||
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec)
|
|
||||||
return Base64.encodeToString(
|
|
||||||
cipher.doFinal(strToEncrypt.toByteArray(Charsets.UTF_8)),
|
|
||||||
Base64.DEFAULT
|
|
||||||
)
|
|
||||||
} catch (e: Exception) {
|
|
||||||
println("Error while encrypting: $e")
|
|
||||||
}
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
fun decrypt(strToDecrypt: String?): String? {
|
|
||||||
try {
|
|
||||||
|
|
||||||
val ivParameterSpec = IvParameterSpec(Base64.decode(iv, Base64.DEFAULT))
|
|
||||||
|
|
||||||
val factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1")
|
|
||||||
val spec =
|
|
||||||
PBEKeySpec(secretKey.toCharArray(), Base64.decode(salt, Base64.DEFAULT), 10000, 256)
|
|
||||||
val tmp = factory.generateSecret(spec)
|
|
||||||
val secretKey = SecretKeySpec(tmp.encoded, "AES")
|
|
||||||
|
|
||||||
val cipher = Cipher.getInstance("AES/CBC/PKCS7Padding")
|
|
||||||
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec)
|
|
||||||
return String(cipher.doFinal(Base64.decode(strToDecrypt, Base64.DEFAULT)))
|
|
||||||
} catch (e: Exception) {
|
|
||||||
println("Error while decrypting: $e")
|
|
||||||
}
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -0,0 +1,14 @@
|
|||||||
|
package ru.myitschool.work.data
|
||||||
|
|
||||||
|
import java.util.Base64
|
||||||
|
|
||||||
|
|
||||||
|
object Base64Encoder {
|
||||||
|
fun encrypt(strToEncrypt: String): String? {
|
||||||
|
return Base64.getEncoder().encodeToString(strToEncrypt.toByteArray())
|
||||||
|
}
|
||||||
|
|
||||||
|
fun decrypt(strToDecrypt: String): String {
|
||||||
|
return String(Base64.getDecoder().decode(strToDecrypt))
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -8,7 +8,7 @@ import androidx.datastore.preferences.core.stringPreferencesKey
|
|||||||
import androidx.datastore.preferences.preferencesDataStore
|
import androidx.datastore.preferences.preferencesDataStore
|
||||||
import kotlinx.coroutines.flow.firstOrNull
|
import kotlinx.coroutines.flow.firstOrNull
|
||||||
import ru.myitschool.work.App
|
import ru.myitschool.work.App
|
||||||
import ru.myitschool.work.data.AESEncyption
|
import ru.myitschool.work.data.Base64Encoder
|
||||||
import ru.myitschool.work.data.dto.AuthRequestDto
|
import ru.myitschool.work.data.dto.AuthRequestDto
|
||||||
import ru.myitschool.work.data.dto.AuthResponseDto
|
import ru.myitschool.work.data.dto.AuthResponseDto
|
||||||
import ru.myitschool.work.data.source.NetworkDataSource
|
import ru.myitschool.work.data.source.NetworkDataSource
|
||||||
@ -22,7 +22,7 @@ object AuthRepository {
|
|||||||
suspend fun checkAndSave(login: String, password: String): Result<AuthResponseDto> {
|
suspend fun checkAndSave(login: String, password: String): Result<AuthResponseDto> {
|
||||||
val data = AuthRequestDto(login=login, password=password)
|
val data = AuthRequestDto(login=login, password=password)
|
||||||
return NetworkDataSource.checkAuth(data).onSuccess { success ->
|
return NetworkDataSource.checkAuth(data).onSuccess { success ->
|
||||||
val encryptedTokenCache = AESEncyption.encrypt(success.token)
|
val encryptedTokenCache = Base64Encoder.encrypt(success.token)
|
||||||
tokenCache = encryptedTokenCache
|
tokenCache = encryptedTokenCache
|
||||||
if (encryptedTokenCache != null) {
|
if (encryptedTokenCache != null) {
|
||||||
App.context.userDataStore.edit { preferences ->
|
App.context.userDataStore.edit { preferences ->
|
||||||
@ -41,9 +41,7 @@ object AuthRepository {
|
|||||||
preferences[stringPreferencesKey(TOKEN_KEY)]
|
preferences[stringPreferencesKey(TOKEN_KEY)]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (tokenCache != null) {
|
tokenCache?.let { return Base64Encoder.decrypt(it) }
|
||||||
return AESEncyption.decrypt(tokenCache)
|
|
||||||
}
|
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
package ru.myitschool.work.data.source
|
package ru.myitschool.work.data.source
|
||||||
|
|
||||||
|
import android.util.Log
|
||||||
import io.ktor.client.HttpClient
|
import io.ktor.client.HttpClient
|
||||||
import io.ktor.client.call.body
|
import io.ktor.client.call.body
|
||||||
import io.ktor.client.engine.cio.CIO
|
import io.ktor.client.engine.cio.CIO
|
||||||
@ -46,6 +47,7 @@ object NetworkDataSource {
|
|||||||
contentType(ContentType.Application.Json)
|
contentType(ContentType.Application.Json)
|
||||||
setBody(data)
|
setBody(data)
|
||||||
}
|
}
|
||||||
|
Log.d("testlog", response.status.toString())
|
||||||
when (response.status) {
|
when (response.status) {
|
||||||
HttpStatusCode.OK -> response.body<AuthResponseDto>()
|
HttpStatusCode.OK -> response.body<AuthResponseDto>()
|
||||||
else -> error(response.bodyAsText())
|
else -> error(response.bodyAsText())
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user