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

Allow to set headers and parameters in all requests #318

Merged
merged 2 commits into from
Jul 16, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
import com.auth0.android.authentication.request.ProfileRequest;
import com.auth0.android.authentication.request.SignUpRequest;
import com.auth0.android.authentication.request.TokenRequest;
import com.auth0.android.request.AuthRequest;
import com.auth0.android.request.AuthenticationRequest;
import com.auth0.android.request.ErrorBuilder;
import com.auth0.android.request.ParameterizableRequest;
import com.auth0.android.request.Request;
import com.auth0.android.request.internal.AuthenticationErrorBuilder;
import com.auth0.android.request.internal.GsonProvider;
import com.auth0.android.request.internal.OkHttpClientFactory;
Expand Down Expand Up @@ -197,7 +197,7 @@ public void setUserAgent(String userAgent) {
* @return a request to configure and start that will yield {@link Credentials}
*/
@SuppressWarnings("WeakerAccess")
public AuthenticationRequest login(@NonNull String usernameOrEmail, @NonNull String password, @NonNull String realmOrConnection) {
public AuthRequest login(@NonNull String usernameOrEmail, @NonNull String password, @NonNull String realmOrConnection) {

ParameterBuilder builder = ParameterBuilder.newBuilder()
.set(USERNAME_KEY, usernameOrEmail)
Expand Down Expand Up @@ -240,7 +240,7 @@ public AuthenticationRequest login(@NonNull String usernameOrEmail, @NonNull Str
* @return a request to configure and start that will yield {@link Credentials}
*/
@SuppressWarnings("WeakerAccess")
public AuthenticationRequest login(@NonNull String usernameOrEmail, @NonNull String password) {
public AuthRequest login(@NonNull String usernameOrEmail, @NonNull String password) {
Map<String, Object> requestParameters = ParameterBuilder.newBuilder()
.set(USERNAME_KEY, usernameOrEmail)
.set(PASSWORD_KEY, password)
Expand Down Expand Up @@ -273,7 +273,7 @@ public AuthenticationRequest login(@NonNull String usernameOrEmail, @NonNull Str
* @return a request to configure and start that will yield {@link Credentials}
*/
@SuppressWarnings("WeakerAccess")
public AuthenticationRequest loginWithOTP(@NonNull String mfaToken, @NonNull String otp) {
public AuthRequest loginWithOTP(@NonNull String mfaToken, @NonNull String otp) {
Map<String, Object> parameters = ParameterBuilder.newBuilder()
.setGrantType(GRANT_TYPE_MFA_OTP)
.set(MFA_TOKEN_KEY, mfaToken)
Expand Down Expand Up @@ -305,7 +305,7 @@ public AuthenticationRequest loginWithOTP(@NonNull String mfaToken, @NonNull Str
* @return a request to configure and start that will yield {@link Credentials}
*/
@SuppressWarnings("WeakerAccess")
public AuthenticationRequest loginWithOAuthAccessToken(@NonNull String token, @NonNull String connection) {
public AuthRequest loginWithOAuthAccessToken(@NonNull String token, @NonNull String connection) {
HttpUrl url = HttpUrl.parse(auth0.getDomainUrl()).newBuilder()
.addPathSegment(OAUTH_PATH)
.addPathSegment(ACCESS_TOKEN_PATH)
Expand All @@ -317,8 +317,9 @@ public AuthenticationRequest loginWithOAuthAccessToken(@NonNull String token, @N
.setAccessToken(token)
.asDictionary();

return factory.authenticationPOST(url, client, gson)
.addAuthenticationParameters(parameters);
AuthRequest authRequest = factory.authenticationPOST(url, client, gson);
authRequest.addAuthenticationParameters(parameters);
return authRequest;
}

/**
Expand All @@ -343,7 +344,7 @@ public AuthenticationRequest loginWithOAuthAccessToken(@NonNull String token, @N
* @return a request to configure and start that will yield {@link Credentials}
*/
@SuppressWarnings("WeakerAccess")
public AuthenticationRequest loginWithNativeSocialToken(@NonNull String token, @NonNull String tokenType) {
public AuthRequest loginWithNativeSocialToken(@NonNull String token, @NonNull String tokenType) {
HttpUrl url = HttpUrl.parse(auth0.getDomainUrl()).newBuilder()
.addPathSegment(OAUTH_PATH)
.addPathSegment(TOKEN_PATH)
Expand All @@ -356,8 +357,9 @@ public AuthenticationRequest loginWithNativeSocialToken(@NonNull String token, @
.set(SUBJECT_TOKEN_TYPE_KEY, tokenType)
.asDictionary();

return factory.authenticationPOST(url, client, gson)
.addAuthenticationParameters(parameters);
AuthRequest authRequest = factory.authenticationPOST(url, client, gson);
authRequest.addAuthenticationParameters(parameters);
return authRequest;
}

/**
Expand Down Expand Up @@ -385,7 +387,7 @@ public AuthenticationRequest loginWithNativeSocialToken(@NonNull String token, @
* @return a request to configure and start that will yield {@link Credentials}
*/
@SuppressWarnings("WeakerAccess")
public AuthenticationRequest loginWithPhoneNumber(@NonNull String phoneNumber, @NonNull String verificationCode, @NonNull String realmOrConnection) {
public AuthRequest loginWithPhoneNumber(@NonNull String phoneNumber, @NonNull String verificationCode, @NonNull String realmOrConnection) {
ParameterBuilder builder = ParameterBuilder.newAuthenticationBuilder()
.setClientId(getClientId())
.set(USERNAME_KEY, phoneNumber);
Expand Down Expand Up @@ -431,7 +433,7 @@ public AuthenticationRequest loginWithPhoneNumber(@NonNull String phoneNumber, @
* @return a request to configure and start that will yield {@link Credentials}
*/
@SuppressWarnings("WeakerAccess")
public AuthenticationRequest loginWithPhoneNumber(@NonNull String phoneNumber, @NonNull String verificationCode) {
public AuthRequest loginWithPhoneNumber(@NonNull String phoneNumber, @NonNull String verificationCode) {
return loginWithPhoneNumber(phoneNumber, verificationCode, SMS_CONNECTION);
}

Expand Down Expand Up @@ -460,7 +462,7 @@ public AuthenticationRequest loginWithPhoneNumber(@NonNull String phoneNumber, @
* @return a request to configure and start that will yield {@link Credentials}
*/
@SuppressWarnings("WeakerAccess")
public AuthenticationRequest loginWithEmail(@NonNull String email, @NonNull String verificationCode, @NonNull String realmOrConnection) {
public AuthRequest loginWithEmail(@NonNull String email, @NonNull String verificationCode, @NonNull String realmOrConnection) {
ParameterBuilder builder = ParameterBuilder.newAuthenticationBuilder()
.setClientId(getClientId())
.set(USERNAME_KEY, email);
Expand Down Expand Up @@ -506,7 +508,7 @@ public AuthenticationRequest loginWithEmail(@NonNull String email, @NonNull Stri
* @return a request to configure and start that will yield {@link Credentials}
*/
@SuppressWarnings("WeakerAccess")
public AuthenticationRequest loginWithEmail(@NonNull String email, @NonNull String verificationCode) {
public AuthRequest loginWithEmail(@NonNull String email, @NonNull String verificationCode) {
return loginWithEmail(email, verificationCode, EMAIL_CONNECTION);
}

Expand All @@ -530,7 +532,7 @@ public AuthenticationRequest loginWithEmail(@NonNull String email, @NonNull Stri
* @return a request to start
*/
@SuppressWarnings("WeakerAccess")
public Request<UserProfile, AuthenticationException> userInfo(@NonNull String accessToken) {
public ParameterizableRequest<UserProfile, AuthenticationException> userInfo(@NonNull String accessToken) {
return profileRequest()
.addHeader(HEADER_AUTHORIZATION, "Bearer " + accessToken);
}
Expand All @@ -557,7 +559,7 @@ public Request<UserProfile, AuthenticationException> userInfo(@NonNull String ac
*/
@SuppressWarnings("WeakerAccess")
@Deprecated
public Request<UserProfile, AuthenticationException> tokenInfo(@NonNull String idToken) {
public ParameterizableRequest<UserProfile, AuthenticationException> tokenInfo(@NonNull String idToken) {
HttpUrl url = HttpUrl.parse(auth0.getDomainUrl()).newBuilder()
.addPathSegment(TOKEN_INFO_PATH)
.build();
Expand Down Expand Up @@ -661,7 +663,7 @@ public DatabaseConnectionRequest<DatabaseUser, AuthenticationException> createUs
@SuppressWarnings("WeakerAccess")
public SignUpRequest signUp(@NonNull String email, @NonNull String password, @NonNull String username, @NonNull String connection) {
final DatabaseConnectionRequest<DatabaseUser, AuthenticationException> createUserRequest = createUser(email, password, username, connection);
final AuthenticationRequest authenticationRequest = login(email, password, connection);
final AuthRequest authenticationRequest = login(email, password, connection);

return new SignUpRequest(createUserRequest, authenticationRequest);
}
Expand Down Expand Up @@ -691,7 +693,7 @@ public SignUpRequest signUp(@NonNull String email, @NonNull String password, @No
@SuppressWarnings("WeakerAccess")
public SignUpRequest signUp(@NonNull String email, @NonNull String password, @NonNull String connection) {
final DatabaseConnectionRequest<DatabaseUser, AuthenticationException> createUserRequest = createUser(email, password, connection);
final AuthenticationRequest authenticationRequest = login(email, password, connection);
final AuthRequest authenticationRequest = login(email, password, connection);
return new SignUpRequest(createUserRequest, authenticationRequest);
}

Expand Down Expand Up @@ -754,7 +756,7 @@ public DatabaseConnectionRequest<Void, AuthenticationException> resetPassword(@N
* @return a request to start
*/
@SuppressWarnings("WeakerAccess")
public Request<Void, AuthenticationException> revokeToken(@NonNull String refreshToken) {
public ParameterizableRequest<Void, AuthenticationException> revokeToken(@NonNull String refreshToken) {
final Map<String, Object> parameters = ParameterBuilder.newBuilder()
.setClientId(getClientId())
.set(TOKEN_KEY, refreshToken)
Expand Down Expand Up @@ -1161,7 +1163,7 @@ public TokenRequest token(@NonNull String authorizationCode, @NonNull String red
*
* @return a request to obtain the JSON Web Keys associated with this Auth0 account.
*/
public Request<Map<String, PublicKey>, AuthenticationException> fetchJsonWebKeys() {
public ParameterizableRequest<Map<String, PublicKey>, AuthenticationException> fetchJsonWebKeys() {
HttpUrl url = HttpUrl.parse(auth0.getDomainUrl()).newBuilder()
.addPathSegment(WELL_KNOWN_PATH)
.addPathSegment(JWKS_FILE_PATH)
Expand All @@ -1171,7 +1173,7 @@ public Request<Map<String, PublicKey>, AuthenticationException> fetchJsonWebKeys
return factory.GET(url, client, gson, jwksType, authErrorBuilder);
}

private AuthenticationRequest loginWithToken(Map<String, Object> parameters) {
private AuthRequest loginWithToken(Map<String, Object> parameters) {
HttpUrl url = HttpUrl.parse(auth0.getDomainUrl()).newBuilder()
.addPathSegment(OAUTH_PATH)
.addPathSegment(TOKEN_PATH)
Expand All @@ -1181,11 +1183,12 @@ private AuthenticationRequest loginWithToken(Map<String, Object> parameters) {
.setClientId(getClientId())
.addAll(parameters)
.asDictionary();
return factory.authenticationPOST(url, client, gson)
.addAuthenticationParameters(requestParameters);
AuthRequest authRequest = factory.authenticationPOST(url, client, gson);
authRequest.addAuthenticationParameters(requestParameters);
return authRequest;
}

private AuthenticationRequest loginWithResourceOwner(Map<String, Object> parameters) {
private AuthRequest loginWithResourceOwner(Map<String, Object> parameters) {
HttpUrl url = HttpUrl.parse(auth0.getDomainUrl()).newBuilder()
.addPathSegment(OAUTH_PATH)
.addPathSegment(RESOURCE_OWNER_PATH)
Expand All @@ -1195,8 +1198,9 @@ private AuthenticationRequest loginWithResourceOwner(Map<String, Object> paramet
.setClientId(getClientId())
.addAll(parameters)
.asDictionary();
return factory.authenticationPOST(url, client, gson)
.addAuthenticationParameters(requestParameters);
AuthRequest authRequest = factory.authenticationPOST(url, client, gson);
authRequest.addAuthenticationParameters(requestParameters);
return authRequest;
}

private ParameterizableRequest<UserProfile, AuthenticationException> profileRequest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public DatabaseConnectionRequest(ParameterizableRequest<T, U> request) {

/**
* Add the given parameters to the request
*
* @param parameters to be sent with the request
* @return itself
*/
Expand All @@ -31,7 +32,8 @@ public DatabaseConnectionRequest<T, U> addParameters(Map<String, Object> paramet

/**
* Add a parameter by name to the request
* @param name of the parameter
*
* @param name of the parameter
* @param value of the parameter
* @return itself
*/
Expand All @@ -41,8 +43,9 @@ public DatabaseConnectionRequest<T, U> addParameter(String name, Object value) {
}

/**
* Add a header for the request, e.g. "Authorization"
* @param name of the header
* Add a header to the request, e.g. "Authorization"
*
* @param name of the header
* @param value of the header
* @return itself
*/
Expand All @@ -53,6 +56,7 @@ public DatabaseConnectionRequest<T, U> addHeader(String name, String value) {

/**
* Set the Auth0 Database Connection used for this request using its name.
*
* @param connection name
* @return itself
*/
Expand All @@ -63,6 +67,7 @@ public DatabaseConnectionRequest<T, U> setConnection(String connection) {

/**
* Executes the request async and returns its results via callback
*
* @param callback called on success or failure of the request
*/
public void start(BaseCallback<T, U> callback) {
Expand All @@ -71,6 +76,7 @@ public void start(BaseCallback<T, U> callback) {

/**
* Executes the request synchronously
*
* @return the request result
* @throws Auth0Exception if the request failed
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ public DelegationRequest<T> addParameters(Map<String, Object> parameters) {
return this;
}

/**
* Add a header to the request, e.g. "Authorization"
*
* @param name of the header
* @param value of the header
* @return itself
*/
public DelegationRequest<T> addHeader(String name, String value) {
request.addHeader(name, value);
return this;
}

/**
* Set the 'api_type' parameter to be sent in the request
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* AuthenticationRequest.java
* ProfileRequest.java
*
* Copyright (c) 2015 Auth0 (https://auth0.com)
*
Expand All @@ -24,9 +24,12 @@

package com.auth0.android.authentication.request;

import android.support.annotation.NonNull;

import com.auth0.android.Auth0Exception;
import com.auth0.android.authentication.AuthenticationException;
import com.auth0.android.callback.BaseCallback;
import com.auth0.android.request.AuthRequest;
import com.auth0.android.request.AuthenticationRequest;
import com.auth0.android.request.ParameterizableRequest;
import com.auth0.android.request.Request;
Expand All @@ -43,12 +46,32 @@ public class ProfileRequest implements Request<Authentication, AuthenticationExc

private static final String HEADER_AUTHORIZATION = "Authorization";

private final AuthenticationRequest credentialsRequest;
private final AuthenticationRequest authenticationRequest;
private final AuthRequest authRequest;
private final ParameterizableRequest<UserProfile, AuthenticationException> userInfoRequest;

public ProfileRequest(AuthenticationRequest credentialsRequest, ParameterizableRequest<UserProfile, AuthenticationException> userInfoRequest) {
this.credentialsRequest = credentialsRequest;
/**
* @param authenticationRequest the request that will output a pair of credentials
* @param userInfoRequest the /userinfo request that will be wrapped
* @deprecated using this constructor prevents from updating the request headers. See {@link #ProfileRequest(AuthRequest, ParameterizableRequest)}
*/
@Deprecated
public ProfileRequest(@NonNull AuthenticationRequest authenticationRequest, @NonNull ParameterizableRequest<UserProfile, AuthenticationException> userInfoRequest) {
this.userInfoRequest = userInfoRequest;
this.authenticationRequest = authenticationRequest;
this.authRequest = null;
}

public ProfileRequest(@NonNull AuthRequest authRequest, @NonNull ParameterizableRequest<UserProfile, AuthenticationException> userInfoRequest) {
this.userInfoRequest = userInfoRequest;
this.authRequest = authRequest;
this.authenticationRequest = null;
}

@NonNull
private AuthenticationRequest getAuthRequest() {
//noinspection ConstantConditions
return authenticationRequest == null ? authRequest : authenticationRequest;
}

/**
Expand All @@ -58,7 +81,23 @@ public ProfileRequest(AuthenticationRequest credentialsRequest, ParameterizableR
* @return itself
*/
public ProfileRequest addParameters(Map<String, Object> parameters) {
credentialsRequest.addAuthenticationParameters(parameters);
getAuthRequest().addAuthenticationParameters(parameters);
return this;
}

/**
* Adds a header to the request, e.g. "Authorization"
* Only available when the underlying authentication request is of type {@link AuthRequest}.
*
* @param name of the header
* @param value of the header
* @return itself
* @see ProfileRequest(AuthRequest, ParameterizableRequest<UserProfile, AuthenticationException>)
*/
public ProfileRequest addHeader(String name, String value) {
if (authRequest != null) {
authRequest.addHeader(name, value);
}
return this;
}

Expand All @@ -69,7 +108,7 @@ public ProfileRequest addParameters(Map<String, Object> parameters) {
* @return itself
*/
public ProfileRequest setScope(String scope) {
credentialsRequest.setScope(scope);
getAuthRequest().setScope(scope);
return this;
}

Expand All @@ -80,7 +119,7 @@ public ProfileRequest setScope(String scope) {
* @return itself
*/
public ProfileRequest setConnection(String connection) {
credentialsRequest.setConnection(connection);
getAuthRequest().setConnection(connection);
return this;
}

Expand All @@ -91,7 +130,7 @@ public ProfileRequest setConnection(String connection) {
*/
@Override
public void start(final BaseCallback<Authentication, AuthenticationException> callback) {
credentialsRequest.start(new BaseCallback<Credentials, AuthenticationException>() {
getAuthRequest().start(new BaseCallback<Credentials, AuthenticationException>() {
@Override
public void onSuccess(final Credentials credentials) {
userInfoRequest
Expand Down Expand Up @@ -124,7 +163,7 @@ public void onFailure(AuthenticationException error) {
*/
@Override
public Authentication execute() throws Auth0Exception {
Credentials credentials = credentialsRequest.execute();
Credentials credentials = getAuthRequest().execute();
UserProfile profile = userInfoRequest
.addHeader(HEADER_AUTHORIZATION, "Bearer " + credentials.getAccessToken())
.execute();
Expand Down
Loading