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

Added support for pushed authorization request #49

Merged
merged 1 commit into from
May 2, 2023
Merged
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
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.8-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.3.1-all.zip
29 changes: 16 additions & 13 deletions uber-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ apply plugin: 'java'
apply plugin: 'de.fuerstenau.buildconfig'
apply plugin: 'idea'

targetCompatibility = JavaVersion.VERSION_1_7
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_1_8

buildConfig {
appName = POM_ARTIFACT_ID
Expand All @@ -43,18 +43,21 @@ buildConfig {
}

dependencies {
compile deps.network.retrofit
compile deps.network.retrofitMoshiConverter
compile deps.network.moshi
compile deps.network.okhttp
compile deps.network.okhttpLoggingInterceptor
compile deps.misc.jsr305

testCompile deps.test.junit
testCompile deps.test.assertj
testCompile deps.test.mockito
testCompile deps.test.hamcrest
testCompile deps.test.wiremock
implementation deps.network.retrofit
implementation deps.network.retrofitMoshiConverter
implementation deps.network.moshi
implementation deps.network.okhttp
implementation deps.network.okhttpLoggingInterceptor
implementation deps.misc.jsr305

testImplementation deps.test.junit
testImplementation deps.test.assertj
testImplementation deps.test.mockito
testImplementation deps.test.hamcrest
testImplementation deps.test.wiremock
testImplementation deps.network.retrofit
testImplementation deps.network.okhttp
}

apply from: rootProject.file('gradle/gradle-mvn-push.gradle')
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.uber.sdk.core.auth.internal;

import com.squareup.moshi.Json;

public class LoginPARResponse {
@Json(name = "request_uri")
public String requestUri;
@Json(name = "expires_in")
public String expiresIn;
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,11 @@ public interface OAuth2Service {
@POST("token")
Call<AccessToken> refresh(@Field("refresh_token") String refreshToken,
@Field("client_id") String clientId);
@FormUrlEncoded
@POST("/oauth/v2/par")
Call<LoginPARResponse> loginParRequest(
@Field("client_id") String clientId,
@Field("response_type") String responseType,
@Field("login_hint") String loginHint
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.uber.sdk.core.auth.internal;

import com.squareup.moshi.Json;

import java.io.Serializable;

import javax.annotation.Nonnull;

public class ProfileHint implements Serializable {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't be in internal, it's in the public facing API

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call. will raise a separate pr for it


@Json(name = "first_name")
private final String firstName;
@Json(name = "last_name")
private final String lastName;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should explicitly set @JSON field name on all these.

private final String email;
private final String phone;

private ProfileHint(
String firstName,
String lastName,
String email,
String phone) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.phone = phone;
}

public static class Builder {
private String firstName;
private String lastName;
private String email;
private String phone;
public Builder firstName(@Nonnull String firstName) {
this.firstName = firstName;
return this;
}

public Builder lastName(@Nonnull String lastName) {
this.lastName = lastName;
return this;
}

public Builder email(@Nonnull String email) {
this.email = email;
return this;
}

public Builder phone(@Nonnull String phone) {
this.phone = phone;
return this;
}

public ProfileHint build() {
return new ProfileHint(
firstName,
lastName,
email,
phone
);
}
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public String getEmail() {
return email;
}

public String getPhone() {
return phone;
}

public Builder newBuilder() {
return new Builder()
.firstName(firstName)
.lastName(lastName)
.email(email)
.phone(phone);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package com.uber.sdk.core.client;

import com.uber.sdk.core.auth.Scope;
import com.uber.sdk.core.auth.internal.ProfileHint;

import java.io.Serializable;
import java.util.Collection;
Expand Down Expand Up @@ -83,6 +84,7 @@ public static class Builder {
private Collection<Scope> scopes;
private Collection<String> customScopes;
private Locale locale;
private ProfileHint profileHint;

/**
* The Uber API requires a registered clientId to be sent along with API requests and Deeplinks.
Expand Down Expand Up @@ -172,6 +174,15 @@ public Builder setLocale(@Nonnull Locale locale) {
return this;
}

/**
* Sets the {@link ProfileHint} for prefilling some user personal information during onboarding
* for possible locales.
*/
public Builder setProfileHint(@Nonnull ProfileHint profileHint) {
this.profileHint = profileHint;
return this;
}

/**
* Constructs {@link SessionConfiguration} from set Builder parameters.
*
Expand Down Expand Up @@ -210,7 +221,8 @@ public SessionConfiguration build() {
environment,
scopes,
customScopes,
locale);
locale,
profileHint);
}
}

Expand All @@ -223,6 +235,7 @@ public SessionConfiguration build() {
private final Collection<Scope> scopes;
private final Collection<String> customScopes;
private final Locale locale;
private ProfileHint profileHint;

protected SessionConfiguration(@Nonnull String clientId,
@Nonnull String clientSecret,
Expand All @@ -232,7 +245,8 @@ protected SessionConfiguration(@Nonnull String clientId,
@Nonnull Environment environment,
@Nonnull Collection<Scope> scopes,
@Nonnull Collection<String> customScopes,
@Nonnull Locale locale) {
@Nonnull Locale locale,
ProfileHint profileHint) {
this.clientId = clientId;
this.clientSecret = clientSecret;
this.serverToken = serverToken;
Expand All @@ -242,6 +256,7 @@ protected SessionConfiguration(@Nonnull String clientId,
this.scopes = scopes;
this.customScopes = customScopes;
this.locale = locale;
this.profileHint = profileHint;
}

/**
Expand Down Expand Up @@ -312,7 +327,7 @@ public String getEndpointHost() {
*/
@Nonnull
public String getLoginHost() {
return String.format("https://login.%s", DEFAULT.getDomain());
return String.format("https://auth.%s", DEFAULT.getDomain());
}

/**
Expand Down Expand Up @@ -341,11 +356,20 @@ public Locale getLocale() {
return locale;
}

/**
* Gets the {@link ProfileHint} used to prefill user's profile information
* @return
*/
public ProfileHint getProfileHint() {
return profileHint;
}

public Builder newBuilder() {
return new Builder()
.setClientId(clientId)
.setRedirectUri(redirectUri)
.setEnvironment(environment)
.setScopes(scopes);
.setScopes(scopes)
.setProfileHint(profileHint);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.uber.sdk.core.client.internal;

public class LoginPARRequestException extends RuntimeException {
LoginPARRequestException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.uber.sdk.core.client.internal;

import static java.nio.charset.StandardCharsets.UTF_8;

import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
import com.uber.sdk.core.auth.internal.LoginPARResponse;
import com.uber.sdk.core.auth.internal.OAuth2Service;
import com.uber.sdk.core.auth.internal.ProfileHint;
import com.uber.sdk.core.client.SessionConfiguration;

import java.util.Base64;
import java.util.Locale;

import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.moshi.MoshiConverterFactory;

public class LoginPushedAuthorizationRequest {

private final OAuth2Service oAuth2Service;

private final Callback callback;
private final ProfileHint profileHint;
private final String clientId;
private final String responseType;
private final Moshi moshi;

public LoginPushedAuthorizationRequest(
SessionConfiguration sessionConfiguration,
String responseType,
Callback callback
) {
this(
createOAuthService(sessionConfiguration.getLoginHost()),
sessionConfiguration.getProfileHint(),
sessionConfiguration.getClientId(),
responseType,
callback
);
}

public LoginPushedAuthorizationRequest(
OAuth2Service oAuth2Service,
ProfileHint profileHint,
String clientId,
String responseType,
Callback callback
) {
this.oAuth2Service = oAuth2Service;
this.profileHint = profileHint;
this.clientId = clientId;
this.responseType = responseType.toLowerCase(Locale.US);
this.callback = callback;
this.moshi = new Moshi.Builder().build();
}

public void execute() {
if (profileHint == null) {
callback.onSuccess("");
return;
}
JsonAdapter<ProfileHint> profileHintJsonAdapter = moshi.adapter(ProfileHint.class);
String profileHintString = new String(
Base64.getEncoder().encode(
profileHintJsonAdapter.toJson(profileHint).getBytes(UTF_8)
)
);
oAuth2Service
.loginParRequest(clientId, responseType, profileHintString)
.enqueue(new retrofit2.Callback<LoginPARResponse>() {
@Override
public void onResponse(Call<LoginPARResponse> call, Response<LoginPARResponse> response) {
callback.onSuccess(response.body().requestUri);
}

@Override
public void onFailure(Call<LoginPARResponse> call, Throwable t) {
callback.onError(new LoginPARRequestException(t));
}
});
}

static OAuth2Service createOAuthService(String baseUrl) {
return new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(MoshiConverterFactory.create())
.build()
.create(OAuth2Service.class);
}

public static interface Callback {
void onSuccess(String requestUri);

void onError(LoginPARRequestException e);
}
}
Loading