Skip to content

Commit

Permalink
fixed par request and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lalwani committed Mar 22, 2024
1 parent 0a95b3c commit 92f3246
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ import com.uber.sdk2.auth.api.response.AuthResult
import com.uber.sdk2.auth.api.response.PARResponse
import com.uber.sdk2.auth.api.response.UberToken
import com.uber.sdk2.auth.internal.service.AuthService
import com.uber.sdk2.auth.internal.service.PrefillRequest
import com.uber.sdk2.auth.internal.sso.SsoLinkFactory
import com.uber.sdk2.auth.internal.sso.UniversalSsoLink.Companion.RESPONSE_TYPE
import com.uber.sdk2.auth.internal.utils.Base64Util

class AuthProvider(
private val activity: AppCompatActivity,
Expand All @@ -43,12 +44,18 @@ class AuthProvider(
val ssoConfig = SsoConfigProvider.getSsoConfig(activity)
val parResponse =
authContext.prefillInfo?.let {
PrefillRequest.pushedAuthorizationRequest(
authService,
ssoConfig.clientId,
it,
ssoConfig.scope ?: "profile",
)
val response =
authService.loginParRequest(
ssoConfig.clientId,
RESPONSE_TYPE,
Base64Util.encodePrefillInfoToString(prefillInfo = it),
ssoConfig.scope ?: "profile",
)
if (response.isSuccessful && response.body() != null) {
response.body() ?: throw AuthException.ServerError(AuthException.EMPTY_RESPONSE)
} else {
PARResponse("", "")
}
} ?: PARResponse("", "")

val queryParams: Map<String, String> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,25 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.uber.sdk2.auth.internal.service
package com.uber.sdk2.auth.internal.utils

import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import com.uber.sdk2.auth.api.exception.AuthException
import com.uber.sdk2.auth.api.exception.AuthException.ServerError
import com.uber.sdk2.auth.api.request.PrefillInfo
import com.uber.sdk2.auth.api.response.PARResponse
import com.uber.sdk2.auth.internal.sso.UniversalSsoLink
import java.nio.charset.StandardCharsets
import java.util.Base64

object PrefillRequest {

suspend fun pushedAuthorizationRequest(
authService: AuthService,
clientId: String,
prefillInfo: PrefillInfo,
scope: String,
): PARResponse {
object Base64Util {
fun encodePrefillInfoToString(prefillInfo: PrefillInfo): String {
val moshi =
Moshi.Builder()
.add(KotlinJsonAdapterFactory()) // Needed for Kotlin data classes
.build()
val profileHintJsonAdapter: JsonAdapter<PrefillInfo> = moshi.adapter(PrefillInfo::class.java)
val profileHintString =
String(
Base64.getEncoder()
.encode(profileHintJsonAdapter.toJson(prefillInfo).toByteArray(StandardCharsets.UTF_8))
)
val response =
authService.loginParRequest(
clientId,
UniversalSsoLink.RESPONSE_TYPE,
profileHintString,
scope,
return Base64.getEncoder()
.encodeToString(
profileHintJsonAdapter.toJson(prefillInfo).toByteArray(StandardCharsets.UTF_8)
)
if (response.isSuccessful && response.body() != null) {
return response.body() ?: throw ServerError(AuthException.EMPTY_RESPONSE)
} else {
throw ServerError("Bad response from server. code: ${response.code()}")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.uber.sdk2.auth.internal.service.AuthService
import com.uber.sdk2.auth.internal.shadow.ShadowSsoConfigProvider
import com.uber.sdk2.auth.internal.shadow.ShadowSsoLinkFactory
import com.uber.sdk2.auth.internal.sso.SsoLinkFactory
import com.uber.sdk2.auth.internal.utils.Base64Util
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
Expand Down Expand Up @@ -67,11 +68,11 @@ class AuthProviderTest : RobolectricTestBase() {
whenever(authService.token(any(), any(), any(), any(), any()))
.thenReturn(Response.success(UberToken(accessToken = "accessToken")))
val authContext =
AuthContext(AuthDestination.CrossAppSso(listOf(CrossApp.Rider)), AuthType.PKCE, null)
AuthContext(AuthDestination.CrossAppSso(listOf(CrossApp.Rider)), AuthType.PKCE(), null)
val authProvider = AuthProvider(activity, authContext, authService, codeVerifierGenerator)
val result = authProvider.authenticate()
verify(authService, never()).loginParRequest(any(), any(), any(), any())
verify(authService).token("clientId", "verifier", "code", "redirectUri", "code")
verify(authService).token("clientId", "verifier", "authorization_code", "redirectUri", "code")
assert(result is AuthResult.Success)
assert((result as AuthResult.Success).uberToken.accessToken == "accessToken")
}
Expand All @@ -87,11 +88,18 @@ class AuthProviderTest : RobolectricTestBase() {
.thenReturn(Response.success(UberToken(accessToken = "accessToken")))
val prefillInfo = PrefillInfo("email", "firstName", "lastName", "phoneNumber")
val authContext =
AuthContext(AuthDestination.CrossAppSso(listOf(CrossApp.Rider)), AuthType.PKCE, prefillInfo)
AuthContext(AuthDestination.CrossAppSso(listOf(CrossApp.Rider)), AuthType.PKCE(), prefillInfo)
val authProvider = AuthProvider(activity, authContext, authService, codeVerifierGenerator)
val result = authProvider.authenticate()
verify(authService).loginParRequest("clientId", "code", prefillInfo, "profile")
verify(authService).token("clientId", "verifier", "code", "redirectUri", "authCode")
verify(authService)
.loginParRequest(
"clientId",
"code",
Base64Util.encodePrefillInfoToString(prefillInfo),
"profile",
)
verify(authService)
.token("clientId", "verifier", "authorization_code", "redirectUri", "authCode")
assert(result is AuthResult.Success)
assert((result as AuthResult.Success).uberToken.accessToken == "accessToken")
}
Expand Down

This file was deleted.

0 comments on commit 92f3246

Please sign in to comment.