Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
[feature/update-new-auth-host] APP 팀 서버에서 수정된 부분 반영 (#2)
Browse files Browse the repository at this point in the history
* API 업데이트 된 부분 수정

* Version bump to 0.1.3
  • Loading branch information
l2hyunwoo authored Apr 17, 2023
1 parent 16b78c5 commit 27f9d59
Show file tree
Hide file tree
Showing 17 changed files with 49 additions and 35 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Add in in your root build.gradle at the end of repositories:
** Step2. Add the dependency**
```groovy
dependencies {
implementation 'com.github.sopt-makers:sopt-android-pgauth:0.1.2'
implementation 'com.github.sopt-makers:sopt-android-pgauth:0.1.3'
}
```

Expand Down
4 changes: 2 additions & 2 deletions auth/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ android {
}

dependencies {
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.core:core-ktx:1.10.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
Expand All @@ -65,7 +65,7 @@ publishing {
maven(MavenPublication) {
groupId = 'com.github.sopt-makers'
artifactId = 'auth'
version = '0.1.2'
version = '0.1.3'

afterEvaluate {
from components.release
Expand Down
6 changes: 3 additions & 3 deletions auth/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application>
<application android:usesCleartextTraffic="true">
<activity
android:name=".AuthCodeHandlerActivity"
android:exported="true"
android:launchMode="singleTask"
android:theme="@style/TransparentTheme"
android:exported="true">
android:theme="@style/TransparentTheme">
<intent-filter>
<action android:name="android.intent.action.VIEW" />

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.sopt.official.playground.auth

internal object Constants {
val NOT_SECURE_SCHEME: String = "http"
val SCHEME: String = "https"
val REDIRECT_URI: String = "redirect_uri"
val CODE: String = "code"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,27 @@ import kotlinx.coroutines.launch
import org.sopt.official.playground.auth.data.PlaygroundApiManager
import org.sopt.official.playground.auth.data.PlaygroundAuthDatasource
import org.sopt.official.playground.auth.data.RemotePlaygroundAuthDatasource
import org.sopt.official.playground.auth.data.remote.model.response.OauthToken
import org.sopt.official.playground.auth.data.remote.model.response.OAuthToken
import org.sopt.official.playground.auth.utils.PlaygroundUriProvider

object PlaygroundAuth {
fun authorizeWithWebTab(
context: Context,
authStateGenerator: AuthStateGenerator = DefaultAuthStateGenerator(),
uri: Uri? = null,
isDebug: Boolean = false,
authDataSource: PlaygroundAuthDatasource = RemotePlaygroundAuthDatasource(
PlaygroundApiManager.getInstance().provideAuthService()
PlaygroundApiManager.getInstance(isDebug).provideAuthService()
),
callback: (Result<OauthToken>) -> Unit
callback: (Result<OAuthToken>) -> Unit,
) {
val stateToken = authStateGenerator.generate()
val resultReceiver = resultReceiver(authDataSource) {
callback(it)
}
startAuthTab(
context = context,
uri = uri ?: PlaygroundUriProvider().authorize(state = stateToken),
uri = uri ?: PlaygroundUriProvider().authorize(state = stateToken, isDebug = isDebug),
state = stateToken,
resultReceiver = resultReceiver
)
Expand All @@ -50,7 +51,7 @@ object PlaygroundAuth {

private fun resultReceiver(
authDataSource: PlaygroundAuthDatasource,
callback: (Result<OauthToken>) -> Unit
callback: (Result<OAuthToken>) -> Unit
) = PlaygroundAuthResultReceiver(
callback = { code, error ->
if (error != null) {
Expand All @@ -65,7 +66,7 @@ object PlaygroundAuth {
private fun requestOauthToken(
authDataSource: PlaygroundAuthDatasource,
code: String,
callback: (Result<OauthToken>) -> Unit
callback: (Result<OAuthToken>) -> Unit
) = CoroutineScope(Dispatchers.Default).launch {
callback(authDataSource.oauth(code))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.sopt.official.playground.auth

internal object PlaygroundInfo {
val HOST: String = "sopt-internal-dev.pages.dev"
val DEBUG_HOST: String = "sopt-internal-dev.pages.dev"
val HOST: String = "playground.sopt.org"
val AUTH_PATH: String = "auth/oauth"
val REDIRECT_URI = "app://org.sopt.makers.android/oauth2redirect"
val authHost: String = "mock-makers-app-server.tekiter.workers.dev"
val DEBUG_AUTH_HOST: String = "ec2-43-200-170-159.ap-northeast-2.compute.amazonaws.com:8080/api/v2/"
val RELEASE_AUTH_HOST: String = "app.sopt.org/api/v2/"
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ internal class PlaygroundApiManager private constructor(
@Volatile
private var instance: PlaygroundApiManager? = null

fun getInstance() = instance ?: synchronized(this) {
instance ?: PlaygroundApiManager(createRetrofit()).also {
fun getInstance(isDebug: Boolean) = instance ?: synchronized(this) {
instance ?: PlaygroundApiManager(createRetrofit(isDebug)).also {
instance = it
}
}

private fun createRetrofit() = ServiceFactory.withClient(
url = "${Constants.SCHEME}://${PlaygroundInfo.authHost}",
private fun createRetrofit(isDebug: Boolean) = ServiceFactory.withClient(
url = "${if (isDebug) Constants.NOT_SECURE_SCHEME else Constants.SCHEME}://${if (isDebug) PlaygroundInfo.DEBUG_AUTH_HOST else PlaygroundInfo.RELEASE_AUTH_HOST}",
client = OkHttpClient.Builder()
.addInterceptor(ServiceFactory.loggingInterceptor)
.build()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.sopt.official.playground.auth.data

import org.sopt.official.playground.auth.data.remote.model.response.OauthToken
import org.sopt.official.playground.auth.data.remote.model.response.OAuthToken

interface PlaygroundAuthDatasource {
suspend fun oauth(code: String): Result<OauthToken>
suspend fun oauth(code: String): Result<OAuthToken>
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package org.sopt.official.playground.auth.data
import org.sopt.official.playground.auth.PlaygroundError
import org.sopt.official.playground.auth.data.remote.AuthService
import org.sopt.official.playground.auth.data.remote.model.request.RequestToken
import org.sopt.official.playground.auth.data.remote.model.response.OauthToken
import org.sopt.official.playground.auth.data.remote.model.response.OAuthToken
import java.net.UnknownHostException

internal class RemotePlaygroundAuthDatasource(
private val authService: AuthService
) : PlaygroundAuthDatasource {

override suspend fun oauth(code: String): Result<OauthToken> {
override suspend fun oauth(code: String): Result<OAuthToken> {
val result = kotlin.runCatching { authService.oauth(RequestToken(code)) }
return when (val exception = result.exceptionOrNull()) {
null -> result
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.sopt.official.playground.auth.data.remote

import org.sopt.official.playground.auth.data.remote.model.request.RequestToken
import org.sopt.official.playground.auth.data.remote.model.response.OauthToken
import org.sopt.official.playground.auth.data.remote.model.response.OAuthToken
import retrofit2.http.Body
import retrofit2.http.POST

internal interface AuthService {
@POST("oauth")
@POST("auth/playground")
suspend fun oauth(
@Body request: RequestToken
): OauthToken
): OAuthToken
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.sopt.official.playground.auth.data.remote.model.request

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
internal data class RequestToken(
@SerialName("code")
val code: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class OauthToken(
@SerialName("access_token")
val accessToken: String
)
data class OAuthToken(
@SerialName("accessToken") val accessToken: String,
@SerialName("refreshToken") val refreshToken: String,
@SerialName("playgroundToken") val playgroundToken: String,
@SerialName("status") val status: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import org.sopt.official.playground.auth.PlaygroundInfo

internal class PlaygroundUriProvider {
fun authorize(
isDebug: Boolean = false,
redirectUri: String = PlaygroundInfo.REDIRECT_URI,
state: String
): Uri = Uri.Builder()
.scheme(Constants.SCHEME)
.authority(PlaygroundInfo.HOST)
.authority(if (isDebug) PlaygroundInfo.DEBUG_HOST else PlaygroundInfo.HOST)
.path(PlaygroundInfo.AUTH_PATH)
.appendQueryParameter(Constants.REDIRECT_URI, redirectUri)
.appendQueryParameter(Constants.STATE, state)
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '8.0.0-beta04' apply false
id 'com.android.library' version '8.0.0-beta04' apply false
id 'com.android.application' version '7.4.1' apply false
id 'com.android.library' version '7.4.1' apply false
id 'org.jetbrains.kotlin.android' version '1.8.0' apply false
id 'org.jetbrains.kotlin.plugin.serialization' version '1.8.0'
id 'com.google.dagger.hilt.android' version '2.44' apply false
Expand Down
2 changes: 1 addition & 1 deletion example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ android {
}

dependencies {
implementation 'com.github.sopt-makers:sopt-android-pgauth:0.1.2'
implementation project(':auth')


implementation 'androidx.core:core-ktx:1.9.0'
Expand Down
1 change: 1 addition & 0 deletions example/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AuthExample"
android:usesCleartextTraffic="true"
tools:targetApi="31">
<activity
android:name=".auth.MainActivity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import org.sopt.official.example.BuildConfig
import org.sopt.official.example.databinding.ActivityMainBinding
import org.sopt.official.playground.auth.PlaygroundAuth

Expand All @@ -26,8 +27,11 @@ class MainActivity : AppCompatActivity() {
}

private fun collectLoginEvent() = lifecycleScope.launch {
authViewModel.loginEventStream.collect() {
PlaygroundAuth.authorizeWithWebTab(this@MainActivity) { result ->
authViewModel.loginEventStream.collect {
PlaygroundAuth.authorizeWithWebTab(
this@MainActivity,
isDebug = false
) { result ->
result.onSuccess {
authViewModel.completeLogin(it.accessToken)
}.onFailure { exception ->
Expand Down

0 comments on commit 27f9d59

Please sign in to comment.