Skip to content

Commit 9d56b38

Browse files
committed
migrate baseUrl to HttpUrl (internal)
1 parent e740d32 commit 9d56b38

23 files changed

Lines changed: 89 additions & 99 deletions

src/main/java/com/auth0/client/auth/AuthAPI.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ public class AuthAPI {
3535
private final OkHttpClient client;
3636
private final String clientId;
3737
private final String clientSecret;
38-
private final String baseUrl;
39-
private final HttpUrl httpUrl;
38+
private final HttpUrl baseUrl;
4039
private final TelemetryInterceptor telemetry;
4140
private final HttpLoggingInterceptor logging;
4241

@@ -52,11 +51,10 @@ public AuthAPI(String domain, String clientId, String clientSecret) {
5251
Asserts.assertNotNull(clientId, "client id");
5352
Asserts.assertNotNull(clientSecret, "client secret");
5453

55-
baseUrl = createBaseUrl(domain);
54+
this.baseUrl = createBaseUrl(domain);
5655
if (baseUrl == null) {
5756
throw new IllegalArgumentException("The domain had an invalid format and couldn't be parsed as an URL.");
5857
}
59-
this.httpUrl = HttpUrl.parse(baseUrl);
6058
this.clientId = clientId;
6159
this.clientSecret = clientSecret;
6260

@@ -91,17 +89,16 @@ OkHttpClient getClient() {
9189
}
9290

9391
//Visible for Testing
94-
String getBaseUrl() {
92+
HttpUrl getBaseUrl() {
9593
return baseUrl;
9694
}
9795

98-
private String createBaseUrl(String domain) {
96+
private HttpUrl createBaseUrl(String domain) {
9997
String url = domain;
10098
if (!domain.startsWith("https://") && !domain.startsWith("http://")) {
10199
url = "https://" + domain;
102100
}
103-
HttpUrl baseUrl = HttpUrl.parse(url);
104-
return baseUrl == null ? null : baseUrl.newBuilder().build().toString();
101+
return HttpUrl.parse(url);
105102
}
106103

107104
/**
@@ -123,7 +120,7 @@ private String createBaseUrl(String domain) {
123120
* @return a new instance of the {@link AuthorizeUrlBuilder} to configure.
124121
*/
125122
public AuthorizeUrlBuilder authorizeUrl(String redirectUri) {
126-
Asserts.assertNotNull(redirectUri, "redirect uri");
123+
Asserts.assertValidUrl(redirectUri, "redirect uri");
127124

128125
return AuthorizeUrlBuilder.newInstance(baseUrl, clientId, redirectUri);
129126
}
@@ -145,7 +142,7 @@ public AuthorizeUrlBuilder authorizeUrl(String redirectUri) {
145142
* @return a new instance of the {@link LogoutUrlBuilder} to configure.
146143
*/
147144
public LogoutUrlBuilder logoutUrl(String returnToUrl, boolean setClientId) {
148-
Asserts.assertNotNull(returnToUrl, "return to url");
145+
Asserts.assertValidUrl(returnToUrl, "return to url");
149146

150147
return LogoutUrlBuilder.newInstance(baseUrl, clientId, returnToUrl, setClientId);
151148
}
@@ -171,7 +168,7 @@ public LogoutUrlBuilder logoutUrl(String returnToUrl, boolean setClientId) {
171168
public Request<UserInfo> userInfo(String accessToken) {
172169
Asserts.assertNotNull(accessToken, "access token");
173170

174-
String url = httpUrl
171+
String url = baseUrl
175172
.newBuilder()
176173
.addPathSegment("userinfo")
177174
.build()
@@ -205,7 +202,7 @@ public Request resetPassword(String email, String connection) {
205202
Asserts.assertNotNull(email, "email");
206203
Asserts.assertNotNull(connection, "connection");
207204

208-
String url = httpUrl
205+
String url = baseUrl
209206
.newBuilder()
210207
.addPathSegment(PATH_DBCONNECTIONS)
211208
.addPathSegment("change_password")
@@ -281,7 +278,7 @@ public SignUpRequest signUp(String email, String password, String connection) {
281278
Asserts.assertNotNull(password, "password");
282279
Asserts.assertNotNull(connection, "connection");
283280

284-
String url = httpUrl
281+
String url = baseUrl
285282
.newBuilder()
286283
.addPathSegment(PATH_DBCONNECTIONS)
287284
.addPathSegment("signup")
@@ -319,7 +316,7 @@ public AuthRequest login(String emailOrUsername, String password) {
319316
Asserts.assertNotNull(emailOrUsername, "email or username");
320317
Asserts.assertNotNull(password, "password");
321318

322-
String url = httpUrl
319+
String url = baseUrl
323320
.newBuilder()
324321
.addPathSegment(PATH_OAUTH)
325322
.addPathSegment(PATH_TOKEN)
@@ -360,7 +357,7 @@ public AuthRequest login(String emailOrUsername, String password, String realm)
360357
Asserts.assertNotNull(password, "password");
361358
Asserts.assertNotNull(realm, "realm");
362359

363-
String url = httpUrl
360+
String url = baseUrl
364361
.newBuilder()
365362
.addPathSegment(PATH_OAUTH)
366363
.addPathSegment(PATH_TOKEN)
@@ -398,7 +395,7 @@ public AuthRequest login(String emailOrUsername, String password, String realm)
398395
public AuthRequest requestToken(String audience) {
399396
Asserts.assertNotNull(audience, "audience");
400397

401-
String url = httpUrl
398+
String url = baseUrl
402399
.newBuilder()
403400
.addPathSegment(PATH_OAUTH)
404401
.addPathSegment(PATH_TOKEN)
@@ -432,7 +429,7 @@ public AuthRequest requestToken(String audience) {
432429
public Request<Void> revokeToken(String refreshToken) {
433430
Asserts.assertNotNull(refreshToken, "refresh token");
434431

435-
String url = httpUrl
432+
String url = baseUrl
436433
.newBuilder()
437434
.addPathSegment(PATH_OAUTH)
438435
.addPathSegment(PATH_REVOKE)
@@ -466,7 +463,7 @@ public Request<Void> revokeToken(String refreshToken) {
466463
public AuthRequest renewAuth(String refreshToken) {
467464
Asserts.assertNotNull(refreshToken, "refresh token");
468465

469-
String url = httpUrl
466+
String url = baseUrl
470467
.newBuilder()
471468
.addPathSegment(PATH_OAUTH)
472469
.addPathSegment(PATH_TOKEN)
@@ -503,7 +500,7 @@ public AuthRequest exchangeCode(String code, String redirectUri) {
503500
Asserts.assertNotNull(code, "code");
504501
Asserts.assertNotNull(redirectUri, "redirect uri");
505502

506-
String url = httpUrl
503+
String url = baseUrl
507504
.newBuilder()
508505
.addPathSegment(PATH_OAUTH)
509506
.addPathSegment(PATH_TOKEN)

src/main/java/com/auth0/client/auth/AuthorizeUrlBuilder.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import java.util.Map;
77

88
import static com.auth0.utils.Asserts.assertNotNull;
9-
import static com.auth0.utils.Asserts.assertValidUrl;
109

1110
/**
1211
* Class that provides the methods to generate a valid Auth0 Authorize Url. It's based on the https://auth0.com/docs/api/authentication#social docs.
@@ -20,22 +19,22 @@ public class AuthorizeUrlBuilder {
2019
/**
2120
* Creates an instance of the {@link AuthorizeUrlBuilder} using the given domain and base parameters.
2221
*
23-
* @param domain the domain to use for this URL. Must be a valid URL.
22+
* @param baseUrl the base url constructed from a valid domain.
2423
* @param clientId the client_id value to set
2524
* @param redirectUri the redirect_uri value to set. Must be already URL Encoded and must be white-listed in your Auth0's dashboard.
2625
* @return a new instance of the {@link AuthorizeUrlBuilder} to configure.
2726
*/
28-
static AuthorizeUrlBuilder newInstance(String domain, String clientId, String redirectUri) {
29-
return new AuthorizeUrlBuilder(domain, clientId, redirectUri);
27+
static AuthorizeUrlBuilder newInstance(HttpUrl baseUrl, String clientId, String redirectUri) {
28+
return new AuthorizeUrlBuilder(baseUrl, clientId, redirectUri);
3029
}
3130

32-
private AuthorizeUrlBuilder(String domain, String clientId, String redirectUri) {
33-
assertValidUrl(domain, "domain");
31+
private AuthorizeUrlBuilder(HttpUrl url, String clientId, String redirectUri) {
32+
assertNotNull(url, "base url");
3433
assertNotNull(clientId, "client id");
3534
assertNotNull(redirectUri, "redirect uri");
3635

3736
parameters = new HashMap<>();
38-
builder = HttpUrl.parse(domain).newBuilder()
37+
builder = url.newBuilder()
3938
.addPathSegment("authorize")
4039
.addEncodedQueryParameter("redirect_uri", redirectUri)
4140
.addQueryParameter("client_id", clientId);

src/main/java/com/auth0/client/auth/LogoutUrlBuilder.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import java.util.Map;
77

88
import static com.auth0.utils.Asserts.assertNotNull;
9-
import static com.auth0.utils.Asserts.assertValidUrl;
109

1110
/**
1211
* Class that provides the methods to generate a valid Auth0 Logout Url. It's based on the https://auth0.com/docs/api/authentication#logout docs.
@@ -20,22 +19,22 @@ public class LogoutUrlBuilder {
2019
/**
2120
* Creates a instance of the {@link LogoutUrlBuilder} using the given domain and base parameters.
2221
*
23-
* @param domain the domain to use for this URL. Must be a valid URL.
22+
* @param baseUrl the base url constructed from a valid domain.
2423
* @param clientId the client_id value to set
2524
* @param returnToUrl the returnTo value to set. Must be already URL Encoded and must be white-listed in your Auth0's dashboard.
2625
* @param setClientId whether the client_id value must be set or not. This affects the white-list that the Auth0's Dashboard uses to validate the returnTo url.
2726
* @return a new instance of the {@link LogoutUrlBuilder} to configure.
2827
*/
29-
static LogoutUrlBuilder newInstance(String domain, String clientId, String returnToUrl, boolean setClientId) {
30-
return new LogoutUrlBuilder(domain, setClientId ? clientId : null, returnToUrl);
28+
static LogoutUrlBuilder newInstance(HttpUrl baseUrl, String clientId, String returnToUrl, boolean setClientId) {
29+
return new LogoutUrlBuilder(baseUrl, setClientId ? clientId : null, returnToUrl);
3130
}
3231

33-
private LogoutUrlBuilder(String domain, String clientId, String returnToUrl) {
34-
assertValidUrl(domain, "domain");
32+
private LogoutUrlBuilder(HttpUrl url, String clientId, String returnToUrl) {
33+
assertNotNull(url, "base url");
3534
assertNotNull(returnToUrl, "return to url");
3635

3736
parameters = new HashMap<>();
38-
builder = HttpUrl.parse(domain).newBuilder()
37+
builder = url.newBuilder()
3938
.addPathSegment("v2")
4039
.addPathSegment("logout")
4140
.addEncodedQueryParameter("returnTo", returnToUrl);

src/main/java/com/auth0/client/mgmt/BaseManagementEntity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ abstract class BaseManagementEntity {
88
protected final HttpUrl baseUrl;
99
protected final String apiToken;
1010

11-
BaseManagementEntity(OkHttpClient client, String baseUrl, String apiToken) {
11+
BaseManagementEntity(OkHttpClient client, HttpUrl baseUrl, String apiToken) {
1212
this.client = client;
13-
this.baseUrl = HttpUrl.parse(baseUrl);
13+
this.baseUrl = baseUrl;
1414
this.apiToken = apiToken;
1515
}
1616
}

src/main/java/com/auth0/client/mgmt/BlacklistsEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
@SuppressWarnings("WeakerAccess")
1818
public class BlacklistsEntity extends BaseManagementEntity {
1919

20-
BlacklistsEntity(OkHttpClient client, String baseUrl, String apiToken) {
20+
BlacklistsEntity(OkHttpClient client, HttpUrl baseUrl, String apiToken) {
2121
super(client, baseUrl, apiToken);
2222
}
2323

src/main/java/com/auth0/client/mgmt/ClientGrantsEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
@SuppressWarnings("WeakerAccess")
1818
public class ClientGrantsEntity extends BaseManagementEntity {
1919

20-
ClientGrantsEntity(OkHttpClient client, String baseUrl, String apiToken) {
20+
ClientGrantsEntity(OkHttpClient client, HttpUrl baseUrl, String apiToken) {
2121
super(client, baseUrl, apiToken);
2222
}
2323

src/main/java/com/auth0/client/mgmt/ClientsEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
@SuppressWarnings("WeakerAccess")
1919
public class ClientsEntity extends BaseManagementEntity {
2020

21-
ClientsEntity(OkHttpClient client, String baseUrl, String apiToken) {
21+
ClientsEntity(OkHttpClient client, HttpUrl baseUrl, String apiToken) {
2222
super(client, baseUrl, apiToken);
2323
}
2424

src/main/java/com/auth0/client/mgmt/ConnectionsEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
@SuppressWarnings("WeakerAccess")
2020
public class ConnectionsEntity extends BaseManagementEntity {
2121

22-
ConnectionsEntity(OkHttpClient client, String baseUrl, String apiToken) {
22+
ConnectionsEntity(OkHttpClient client, HttpUrl baseUrl, String apiToken) {
2323
super(client, baseUrl, apiToken);
2424
}
2525

src/main/java/com/auth0/client/mgmt/DeviceCredentialsEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
@SuppressWarnings("WeakerAccess")
2020
public class DeviceCredentialsEntity extends BaseManagementEntity {
2121

22-
DeviceCredentialsEntity(OkHttpClient client, String baseUrl, String apiToken) {
22+
DeviceCredentialsEntity(OkHttpClient client, HttpUrl baseUrl, String apiToken) {
2323
super(client, baseUrl, apiToken);
2424
}
2525

src/main/java/com/auth0/client/mgmt/EmailProviderEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818
@SuppressWarnings("WeakerAccess")
1919
public class EmailProviderEntity extends BaseManagementEntity {
20-
EmailProviderEntity(OkHttpClient client, String baseUrl, String apiToken) {
20+
EmailProviderEntity(OkHttpClient client, HttpUrl baseUrl, String apiToken) {
2121
super(client, baseUrl, apiToken);
2222
}
2323

0 commit comments

Comments
 (0)