Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding parcelize #224

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion authentication/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
alias(libs.plugins.android.library)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.kotlin.parcelize)
alias(libs.plugins.spotless)
}

Expand All @@ -60,9 +61,9 @@ android {
}

dependencies {
implementation(libs.core.ktx)
implementation(libs.appCompat)
implementation(libs.material)
implementation(project(":core"))
testImplementation(libs.junit.junit)
androidTestImplementation(libs.androidx.test.ext.junit)
androidTestImplementation(libs.androidx.test.espresso.espresso.core)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package com.uber.sdk2.auth.api.request

import android.os.Parcelable
import kotlinx.parcelize.Parcelize

/**
* Represents the context of the authentication request needed for Uber to authenticate the user.
*
Expand All @@ -23,9 +26,9 @@ package com.uber.sdk2.auth.api.request
* @param prefillInfo The prefill information to be used for the authentication.
* @param scopes The scopes to request for the authentication.
*/
@Parcelize
data class AuthContext(
val authDestination: AuthDestination,
val authType: AuthType,
val prefillInfo: PrefillInfo?,
val scopes: String?,
)
) : Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
*/
package com.uber.sdk2.auth.api.request

import android.os.Parcelable
import kotlinx.parcelize.Parcelize

/** Represents the destination app to authenticate the user. */
sealed class AuthDestination {
@Parcelize
sealed class AuthDestination : Parcelable {
/**
* Authenticating within the same app by using a system webview, a.k.a Custom Tabs. If custom tabs
* are unavailable the authentication flow will be launched in the system browser app.
Expand All @@ -25,7 +29,8 @@ sealed class AuthDestination {

/**
* Authenticating via one of the family of Uber apps using the Single Sign-On (SSO) flow in the
* order of priority mentioned.
* order of priority mentioned. If none of the apps are available we will fall back to the [InApp]
* flow
*
* @param appPriority The order of the apps to use for the SSO flow.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@
*/
package com.uber.sdk2.auth.api.request

import android.os.Parcelable
import kotlinx.parcelize.Parcelize

/**
* Represents the type of authentication to perform.
*
* @see AuthContext
*/
sealed class AuthType {
@Parcelize
sealed class AuthType(val grantType: String) : Parcelable {
/** The authorization code flow. */
data object AuthCode : AuthType()
data object AuthCode : AuthType("code")

/** The proof key for code exchange (PKCE) flow. This is the recommended flow for mobile apps. */
data object PKCE : AuthType()
data object PKCE : AuthType("code")
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,25 @@
*/
package com.uber.sdk2.auth.api.request

/** Provides different apps that could be used for authentication using SSO flow. */
sealed class CrossApp {
import android.os.Parcelable
import kotlinx.parcelize.Parcelize

/**
* Provides different apps that could be used for authentication using SSO flow.
*
* @param packages The list of packages that could be used for authentication.
*/
@Parcelize
sealed class CrossApp(val packages: List<String>) : Parcelable {
/** The Eats app. */
data object Eats : CrossApp()
data object Eats :
CrossApp(listOf("com.ubercab.eats", "com.ubercab.eats.exo", "com.ubercab.eats.internal"))

/** The Rider app. */
data object Rider : CrossApp()
data object Rider :
CrossApp(listOf("com.ubercab", "com.ubercab.presidio.exo", "com.ubercab.rider.internal"))

/** The Driver app. */
data object Driver : CrossApp()
data object Driver :
CrossApp(listOf("com.ubercab.driver", "com.ubercab.driver.exo", "com.ubercab.driver.internal"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package com.uber.sdk2.auth.api.request

import android.os.Parcelable
import kotlinx.parcelize.Parcelize

/**
* Provides a way to prefill the user's information in the authentication flow.
*
Expand All @@ -23,9 +26,10 @@ package com.uber.sdk2.auth.api.request
* @param lastName The last name to prefill.
* @param phoneNumber The phone number to prefill.
*/
@Parcelize
data class PrefillInfo(
val email: String,
val firstName: String,
val lastName: String,
val phoneNumber: String,
)
) : Parcelable
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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.api.request

import android.content.Context
import android.content.res.Resources
import android.text.TextUtils
import com.uber.sdk2.auth.api.exception.AuthException
import com.uber.sdk2.core.config.UriConfig.CLIENT_ID_PARAM
import com.uber.sdk2.core.config.UriConfig.REDIRECT_PARAM
import com.uber.sdk2.core.config.UriConfig.SCOPE_PARAM
import java.io.IOException
import java.nio.charset.Charset
import okio.Buffer
import okio.BufferedSource
import okio.Okio
import org.json.JSONException
import org.json.JSONObject

data class SsoConfig(val clientId: String, val redirectUri: String, val scope: String? = null)

object SsoConfigProvider {
fun getSsoConfig(context: Context): SsoConfig {
val resources: Resources = context.resources
val resourceId = resources.getIdentifier(SSO_CONFIG_FILE, "raw", context.packageName)
val configSource: BufferedSource =
Okio.buffer(Okio.source(resources.openRawResource(resourceId)))
val configData = Buffer()
try {
configSource.readAll(configData)
val configJson = JSONObject(configData.readString(Charset.forName("UTF-8")))
val clientId = getRequiredConfigString(configJson, CLIENT_ID_PARAM)
val scope = getConfigString(configJson, SCOPE_PARAM)
val redirectUri = getRequiredConfigString(configJson, REDIRECT_PARAM)
return SsoConfig(clientId, redirectUri, scope)
} catch (ex: IOException) {
throw AuthException.ClientError("Failed to read configuration: " + ex.message)
} catch (ex: JSONException) {
throw AuthException.ClientError("Failed to read configuration: " + ex.message)
}
}

@Throws(AuthException::class)
private fun getRequiredConfigString(configJson: JSONObject, propName: String): String {
return getConfigString(configJson, propName)
?: throw AuthException.ClientError(
"$propName is required but not specified in the configuration"
)
}

private fun getConfigString(configJson: JSONObject, propName: String): String? {
var value: String = configJson.optString(propName) ?: return null
value = value.trim { it <= ' ' }
return if (TextUtils.isEmpty(value)) {
null
} else value
}

private const val SSO_CONFIG_FILE = "sso_config"
}
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ plugins {
alias(libs.plugins.android.application) apply false
alias(libs.plugins.android.library) apply false
alias(libs.plugins.mavenPublish) apply false
alias(libs.plugins.kotlin.parcelize) apply false
alias(libs.plugins.dokka)
alias(libs.plugins.spotless)
}
Expand Down
1 change: 1 addition & 0 deletions core/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
45 changes: 45 additions & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.
*/
plugins {
alias(libs.plugins.android.library)
alias(libs.plugins.kotlin.android)
}

android {
namespace = "com.uber.sdk2.core"
buildFeatures.buildConfig = true
defaultConfig {
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
buildConfigField("String", "VERSION_NAME", "\"${project.property("VERSION_NAME").toString()}\"")
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
}

dependencies {
implementation(libs.chrometabs)
implementation(libs.core.ktx)
implementation(libs.appCompat)
implementation(libs.material)
testImplementation(libs.junit.junit)
androidTestImplementation(libs.androidx.test.ext.junit)
androidTestImplementation(libs.androidx.test.espresso.espresso.core)
}
21 changes: 21 additions & 0 deletions core/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
4 changes: 4 additions & 0 deletions core/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

</manifest>
83 changes: 83 additions & 0 deletions core/src/main/kotlin/com/uber/sdk2/core/config/UriConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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.core.config

import android.net.Uri
import com.uber.sdk2.core.BuildConfig
import com.uber.sdk2.core.config.UriConfig.EndpointRegion.DEFAULT
import com.uber.sdk2.core.config.UriConfig.Environment.API
import com.uber.sdk2.core.config.UriConfig.Environment.AUTH
import com.uber.sdk2.core.config.UriConfig.Scheme.HTTPS
import java.util.Locale

object UriConfig {

enum class Scheme(val scheme: String) {
HTTP("http"),
HTTPS("https")
}

/**
* An Uber API Environment. See [Sandbox](https://developer.uber.com/v1/sandbox) for more
* information.
*/
enum class Environment(val subDomain: String) {
API("api"),
AUTH("auth"),
}

enum class EndpointRegion(
/** @return domain to use. */
val domain: String
) {
DEFAULT("uber.com")
}

fun assembleUri(
clientId: String,
responseType: String,
redirectUri: String,
scopes: String? = null,
): Uri {
val builder = Uri.Builder()
builder
.scheme(HTTPS.scheme)
.authority(AUTH.subDomain + "." + DEFAULT.domain)
.appendEncodedPath(PATH)
.appendQueryParameter(CLIENT_ID_PARAM, clientId)
.appendQueryParameter(RESPONSE_TYPE_PARAM, responseType.lowercase(Locale.US))
.appendQueryParameter(REDIRECT_PARAM, redirectUri)
.appendQueryParameter(SCOPE_PARAM, scopes)
.appendQueryParameter(SDK_VERSION_PARAM, BuildConfig.VERSION_NAME)
return builder.build()
}

/** Gets the endpoint host used to hit the Uber API. */
fun getEndpointHost(): String = "${HTTPS.scheme}://$API.${DEFAULT.domain}"

/** Gets the login host used to sign in to the Uber API. */
fun getAuthHost(): String = "${HTTPS.scheme}://$AUTH.${DEFAULT.domain}"

const val CLIENT_ID_PARAM = "client_id"
const val PATH = "oauth/v2/universal/authorize"
const val REDIRECT_PARAM = "redirect_uri"
const val RESPONSE_TYPE_PARAM = "response_type"
const val SCOPE_PARAM = "scope"
const val PLATFORM_PARAM = "sdk"
const val SDK_VERSION_PARAM = "sdk_version"
const val CODE_CHALLENGE_PARAM = "code_challenge"
const val REQUEST_URI = "request_uri"
}
Loading
Loading