-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Refactor/#40 source data store
- Loading branch information
Showing
14 changed files
with
262 additions
and
30 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
41 changes: 41 additions & 0 deletions
41
app/src/main/java/com/puzzling/puzzlingaos/data/datasource/local/TokenDataSource.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,41 @@ | ||
package com.puzzling.puzzlingaos.data.datasource.local | ||
|
||
import android.os.Build | ||
import androidx.annotation.RequiresApi | ||
import androidx.datastore.core.Serializer | ||
import com.puzzling.puzzlingaos.data.entity.Token | ||
import com.puzzling.puzzlingaos.data.service.CryptoService | ||
import kotlinx.serialization.json.Json | ||
import org.apache.commons.lang3.SerializationException | ||
import java.io.InputStream | ||
import java.io.OutputStream | ||
import javax.inject.Inject | ||
|
||
@RequiresApi(Build.VERSION_CODES.M) | ||
class TokenDataSource @Inject constructor(private val cryptoService: CryptoService) : Serializer<Token> { | ||
override val defaultValue: Token | ||
get() = Token() | ||
|
||
override suspend fun readFrom(input: InputStream): Token { | ||
val decryptedBytes = cryptoService.decrypt(input) | ||
return try { | ||
Json.decodeFromString( | ||
deserializer = Token.serializer(), | ||
string = decryptedBytes.decodeToString(), | ||
) | ||
} catch (e: SerializationException) { | ||
e.printStackTrace() | ||
defaultValue | ||
} | ||
} | ||
|
||
override suspend fun writeTo(t: Token, output: OutputStream) { | ||
cryptoService.encrypt( | ||
bytes = Json.encodeToString( | ||
serializer = Token.serializer(), | ||
value = t, | ||
).encodeToByteArray(), | ||
outputStream = output, | ||
) | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
app/src/main/java/com/puzzling/puzzlingaos/data/entity/Token.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,8 @@ | ||
package com.puzzling.puzzlingaos.data.entity | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class Token( | ||
val accessToken: String? = null, | ||
) |
16 changes: 16 additions & 0 deletions
16
app/src/main/java/com/puzzling/puzzlingaos/data/repository/TokenRepositoryImpl.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,16 @@ | ||
package com.puzzling.puzzlingaos.data.repository | ||
|
||
import androidx.datastore.core.DataStore | ||
import com.puzzling.puzzlingaos.data.entity.Token | ||
import com.puzzling.puzzlingaos.domain.repository.TokenRepository | ||
import kotlinx.coroutines.flow.first | ||
import javax.inject.Inject | ||
|
||
class TokenRepositoryImpl @Inject constructor(private val dataStore: DataStore<Token>) : | ||
TokenRepository { | ||
override suspend fun setToken(token: String) { | ||
dataStore.updateData { Token(token) } | ||
} | ||
|
||
override suspend fun getToken(): Token = dataStore.data.first() | ||
} |
84 changes: 84 additions & 0 deletions
84
app/src/main/java/com/puzzling/puzzlingaos/data/service/CryptoService.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,84 @@ | ||
package com.puzzling.puzzlingaos.data.service | ||
|
||
import android.os.Build | ||
import android.security.keystore.KeyGenParameterSpec | ||
import android.security.keystore.KeyProperties | ||
import androidx.annotation.RequiresApi | ||
import java.io.InputStream | ||
import java.io.OutputStream | ||
import java.security.KeyStore | ||
import javax.crypto.Cipher | ||
import javax.crypto.KeyGenerator | ||
import javax.crypto.SecretKey | ||
import javax.crypto.spec.IvParameterSpec | ||
import javax.inject.Inject | ||
|
||
@RequiresApi(Build.VERSION_CODES.M) | ||
class CryptoService @Inject constructor() { | ||
|
||
private val keyStore = KeyStore.getInstance("AndroidKeyStore").apply { | ||
load(null) | ||
} | ||
|
||
private val encryptCipher = Cipher.getInstance(TRANSFORMATION).apply { | ||
init(Cipher.ENCRYPT_MODE, getKey()) | ||
} | ||
|
||
private fun getDecryptCipher(iv: ByteArray): Cipher { | ||
return Cipher.getInstance(TRANSFORMATION).apply { | ||
init(Cipher.DECRYPT_MODE, getKey(), IvParameterSpec(iv)) | ||
} | ||
} | ||
|
||
private fun getKey(): SecretKey { | ||
val existingKey = keyStore.getEntry("secret", null) as? KeyStore.SecretKeyEntry | ||
return existingKey?.secretKey ?: createKey() | ||
} | ||
|
||
private fun createKey(): SecretKey { | ||
return KeyGenerator.getInstance(ALGORITHM).apply { | ||
init( | ||
KeyGenParameterSpec.Builder( | ||
"secret", | ||
KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT, | ||
).setBlockModes(BLOCK_MODE).setEncryptionPaddings( | ||
PADDING, | ||
).setUserAuthenticationRequired(false) // 지문인식 같은거 false | ||
.setRandomizedEncryptionRequired(true).build(), | ||
) | ||
}.generateKey() | ||
} | ||
|
||
fun encrypt(bytes: ByteArray, outputStream: OutputStream): ByteArray { | ||
val encryptedBytes = encryptCipher.doFinal(bytes) | ||
outputStream.use { | ||
it.write(encryptCipher.iv.size) | ||
it.write(encryptCipher.iv) | ||
it.write(encryptedBytes.size) | ||
it.write(encryptedBytes) | ||
} | ||
|
||
return encryptedBytes | ||
} | ||
|
||
fun decrypt(inputStream: InputStream): ByteArray { | ||
return inputStream.use { | ||
val ivSize = it.read() | ||
val iv = ByteArray(ivSize) | ||
it.read(iv) | ||
|
||
val encryptedBytesSize = it.read() | ||
val encryptedBytes = ByteArray(encryptedBytesSize) | ||
it.read(encryptedBytes) | ||
|
||
getDecryptCipher(iv).doFinal(encryptedBytes) | ||
} | ||
} | ||
|
||
companion object { | ||
private const val ALGORITHM = KeyProperties.KEY_ALGORITHM_AES | ||
private const val BLOCK_MODE = KeyProperties.BLOCK_MODE_CBC | ||
private const val PADDING = KeyProperties.ENCRYPTION_PADDING_PKCS7 | ||
private const val TRANSFORMATION = "$ALGORITHM/$BLOCK_MODE/$PADDING" | ||
} | ||
} |
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
35 changes: 35 additions & 0 deletions
35
app/src/main/java/com/puzzling/puzzlingaos/di/DataStoreModule.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,35 @@ | ||
package com.puzzling.puzzlingaos.di | ||
|
||
import android.content.Context | ||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.core.DataStoreFactory | ||
import androidx.datastore.dataStoreFile | ||
import com.puzzling.puzzlingaos.data.datasource.local.TokenDataSource | ||
import com.puzzling.puzzlingaos.data.entity.Token | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object DataStoreModule { | ||
private const val USER_PREFERENCES_NAME = "user_preferences" | ||
private const val DATA_STORE_FILE_NAME = "user_prefs.pb" | ||
|
||
@Provides | ||
@Singleton | ||
fun providePreferencesDataStore( | ||
@ApplicationContext appContext: Context, | ||
tokenDataSource: TokenDataSource, | ||
): DataStore<Token> { | ||
return DataStoreFactory.create( | ||
serializer = tokenDataSource, | ||
produceFile = { | ||
appContext.dataStoreFile(DATA_STORE_FILE_NAME) | ||
}, | ||
) | ||
} | ||
} |
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
9 changes: 9 additions & 0 deletions
9
app/src/main/java/com/puzzling/puzzlingaos/domain/repository/TokenRepository.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,9 @@ | ||
package com.puzzling.puzzlingaos.domain.repository | ||
|
||
import com.puzzling.puzzlingaos.data.entity.Token | ||
|
||
interface TokenRepository { | ||
suspend fun setToken(token: String) | ||
|
||
suspend fun getToken(): Token | ||
} |
13 changes: 13 additions & 0 deletions
13
app/src/main/java/com/puzzling/puzzlingaos/domain/usecase/onboarding/GetTokenUseCase.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 com.puzzling.puzzlingaos.domain.usecase.onboarding | ||
|
||
import com.puzzling.puzzlingaos.domain.repository.TokenRepository | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class GetTokenUseCase @Inject constructor( | ||
private val tokenRepository: TokenRepository, | ||
) { | ||
|
||
suspend operator fun invoke() = tokenRepository.getToken() | ||
} |
10 changes: 10 additions & 0 deletions
10
app/src/main/java/com/puzzling/puzzlingaos/domain/usecase/onboarding/PostTokenUseCase.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 com.puzzling.puzzlingaos.domain.usecase.onboarding | ||
|
||
import com.puzzling.puzzlingaos.domain.repository.TokenRepository | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class PostTokenUseCase @Inject constructor(private val tokenRepository: TokenRepository) { | ||
suspend operator fun invoke(token: String) = tokenRepository.setToken(token) | ||
} |
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
24 changes: 16 additions & 8 deletions
24
app/src/main/java/com/puzzling/puzzlingaos/presentation/onboarding/LoginViewModel.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,29 +1,37 @@ | ||
package com.puzzling.puzzlingaos.presentation.onboarding | ||
|
||
import android.util.Log | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.kakao.sdk.auth.model.OAuthToken | ||
import com.puzzling.puzzlingaos.data.datasource.local.LocalDataSource | ||
import com.puzzling.puzzlingaos.data.service.KakaoLoginService | ||
import com.puzzling.puzzlingaos.domain.usecase.onboarding.GetTokenUseCase | ||
import com.puzzling.puzzlingaos.domain.usecase.onboarding.PostTokenUseCase | ||
import com.puzzling.puzzlingaos.util.KakaoLoginCallback | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.launch | ||
import timber.log.Timber | ||
import javax.inject.Inject | ||
|
||
class LoginViewModel(private val kakaoLoginService: KakaoLoginService) : ViewModel() { | ||
@HiltViewModel | ||
class LoginViewModel @Inject constructor( | ||
private val postTokenUseCase: PostTokenUseCase, | ||
private val getTokenUseCase: GetTokenUseCase, | ||
) : | ||
ViewModel() { | ||
private val _isKakaoLogin = MutableStateFlow(false) | ||
val isKakaoLogin = _isKakaoLogin.asStateFlow() | ||
|
||
val kakaoLoginCallback: (OAuthToken?, Throwable?) -> Unit = { token, error -> | ||
KakaoLoginCallback { | ||
_isKakaoLogin.value = true | ||
Timber.d("토큰!!!! $token") | ||
Log.d("LoginViewModel", "토큰!! $token") | ||
LocalDataSource.setAccessToken("$token") | ||
viewModelScope.launch { | ||
postTokenUseCase.invoke(it) | ||
Log.d("LoginViewModel", "토큰!! usecase ${getTokenUseCase.invoke()}") | ||
} | ||
}.handleResult(token, error) | ||
} | ||
|
||
fun kakaoLogin() = viewModelScope.launch { | ||
kakaoLoginService.startKakaoLogin(kakaoLoginCallback) | ||
} | ||
} |
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