-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added PrefillRequest and some other changes
- Loading branch information
Showing
6 changed files
with
132 additions
and
14 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
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
60 changes: 60 additions & 0 deletions
60
authentication/src/main/kotlin/com/uber/sdk2/auth/internal/service/PrefillRequest.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,60 @@ | ||
/* | ||
* Copyright (C) 2024. Uber Technologies | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.uber.sdk2.auth.internal.service | ||
|
||
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 { | ||
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, | ||
) | ||
if (response.isSuccessful && response.body() != null) { | ||
return response.body() ?: throw ServerError(AuthException.EMPTY_RESPONSE) | ||
} else { | ||
throw ServerError("Bad response from server. code: ${response.code()}") | ||
} | ||
} | ||
} |
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
44 changes: 44 additions & 0 deletions
44
authentication/src/test/kotlin/com/uber/sdk2/auth/internal/service/PrefillRequestTest.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,44 @@ | ||
/* | ||
* Copyright (C) 2024. Uber Technologies | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.uber.sdk2.auth.internal.service | ||
|
||
import com.uber.sdk2.auth.RobolectricTestBase | ||
import com.uber.sdk2.auth.api.request.PrefillInfo | ||
import com.uber.sdk2.auth.api.response.PARResponse | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertNotNull | ||
import org.junit.Test | ||
import org.mockito.kotlin.any | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.whenever | ||
import retrofit2.Response | ||
|
||
class PrefillRequestTest : RobolectricTestBase() { | ||
private val authService: AuthService = mock() | ||
private val prefillInfo = PrefillInfo("email", "firstName", "lastName", "phoneNumber") | ||
|
||
@Test | ||
fun `test pushedAuthorizationRequest when successfulResponse should receiveRequestUri`() = | ||
runTest { | ||
whenever(authService.loginParRequest(any(), any(), any(), any())) | ||
.thenReturn(Response.success(PARResponse("requestUri", "expiresIn"))) | ||
val response = | ||
PrefillRequest.pushedAuthorizationRequest(authService, "clientId", prefillInfo, "scope") | ||
assertNotNull(response) | ||
assertEquals("requestUri", response.requestUri) | ||
} | ||
} |