Skip to content

Commit 1a292ef

Browse files
author
andreals
committed
Automated commit message
1 parent 6567269 commit 1a292ef

21 files changed

+684
-49
lines changed

README.md

Lines changed: 56 additions & 44 deletions
Large diffs are not rendered by default.

doc/auth/basic-authentication.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
# Basic Authentication
3+
4+
5+
6+
Documentation for accessing and setting credentials for httpBasic.
7+
8+
## Auth Credentials
9+
10+
| Name | Type | Description | Setter | Getter |
11+
| --- | --- | --- | --- | --- |
12+
| BasicAuthUserName | `String` | The username to use with basic authentication | `username` | `getBasicAuthUserName()` |
13+
| BasicAuthPassword | `String` | The password to use with basic authentication | `password` | `getBasicAuthPassword()` |
14+
15+
16+
17+
**Note:** Auth credentials can be set using `basicAuthCredentials` in the client builder and accessed through `getBasicAuthCredentials` method in the client instance.
18+
19+
## Usage Example
20+
21+
### Client Initialization
22+
23+
You must provide credentials in the client as shown in the following code snippet.
24+
25+
```java
26+
PagarmeApiSDKClient client = new PagarmeApiSDKClient.Builder()
27+
.basicAuthCredentials(new BasicAuthModel.Builder(
28+
"BasicAuthUserName",
29+
"BasicAuthPassword"
30+
)
31+
.build())
32+
.build();
33+
```
34+
35+

doc/client.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The following parameters are configurable for the API Client:
77
| --- | --- | --- |
88
| `serviceRefererName` | `String` | |
99
| `httpClientConfig` | [`ReadonlyHttpClientConfiguration`](http-client-configuration.md) | Http Client Configuration instance. |
10+
| `basicAuthCredentials` | [`BasicAuthCredentials`]($a/basic-authentication.md) | The Credentials Setter for Basic Authentication |
1011

1112
The API client can be initialized as follows:
1213

@@ -15,6 +16,11 @@ PagarmeApiSDKClient client = new PagarmeApiSDKClient.Builder()
1516
.httpClientConfig(configBuilder -> configBuilder
1617
.timeout(0))
1718
.serviceRefererName("ServiceRefererName")
19+
.basicAuthCredentials(new BasicAuthModel.Builder(
20+
"BasicAuthUserName",
21+
"BasicAuthPassword"
22+
)
23+
.build())
1824
.build();
1925
```
2026

@@ -48,6 +54,7 @@ The gateway for the SDK. This class acts as a factory for the Controllers and al
4854
| `getServiceRefererName()` | . | `String` |
4955
| `getHttpClient()` | The HTTP Client instance to use for making HTTP requests. | `HttpClient` |
5056
| `getHttpClientConfig()` | Http Client Configuration instance. | [`ReadonlyHttpClientConfiguration`](http-client-configuration.md) |
57+
| `getBasicAuthCredentials()` | The credentials to use with BasicAuth. | [`BasicAuthCredentials`]($a/basic-authentication.md) |
5158
| `getBaseUri(Server server)` | Get base URI by current environment | `String` |
5259
| `getBaseUri()` | Get base URI by current environment | `String` |
5360

doc/controllers/tokens.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ TokensController tokensController = client.getTokensController();
1616

1717
# Create Token
1818

19+
:information_source: **Note** This endpoint does not require authentication.
20+
1921
```java
2022
GetTokenResponse createToken(
2123
final String publicKey,
@@ -70,6 +72,8 @@ try {
7072

7173
Gets a token from its id
7274

75+
:information_source: **Note** This endpoint does not require authentication.
76+
7377
```java
7478
GetTokenResponse getToken(
7579
final String id,

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<modelVersion>4.0.0</modelVersion>
99
<groupId>PagarmeApiSDKLib</groupId>
1010
<artifactId>pagarme-api-sdklib</artifactId>
11-
<version>6.8.5</version>
11+
<version>6.8.6</version>
1212
<packaging>jar</packaging>
1313
<name>PagarmeApiSDKLib</name>
1414
<properties>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* PagarmeApiSDKLib
3+
*
4+
* This file was automatically generated by APIMATIC v3.0 ( https://www.apimatic.io ).
5+
*/
6+
7+
package me.pagar.api;
8+
9+
/**
10+
* Interface for defining the behavior of Basic Authentication.
11+
*/
12+
public interface BasicAuthCredentials {
13+
14+
/**
15+
* String value for basicAuthUserName.
16+
* @return basicAuthUserName
17+
*/
18+
String getBasicAuthUserName();
19+
20+
/**
21+
* String value for basicAuthPassword.
22+
* @return basicAuthPassword
23+
*/
24+
String getBasicAuthPassword();
25+
26+
/**
27+
* Checks if provided credentials matched with existing ones.
28+
* @param basicAuthUserName String value for credentials.
29+
* @param basicAuthPassword String value for credentials.
30+
* @return true if credentials matched.
31+
*/
32+
boolean equals(String basicAuthUserName, String basicAuthPassword);
33+
}

src/main/java/me/pagar/api/Configuration.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
package me.pagar.api;
88

9+
import me.pagar.api.authentication.BasicAuthModel;
910
import me.pagar.api.http.client.ReadonlyHttpClientConfiguration;
1011

1112
/**
@@ -37,6 +38,18 @@ public interface Configuration {
3738
*/
3839
long timeout();
3940

41+
/**
42+
* The credentials to use with BasicAuth.
43+
* @return basicAuthCredentials
44+
*/
45+
BasicAuthCredentials getBasicAuthCredentials();
46+
47+
/**
48+
* The auth credential model for BasicAuth.
49+
* @return the instance of BasicAuthModel
50+
*/
51+
BasicAuthModel getBasicAuthModel();
52+
4053
/**
4154
* Get base URI by current environment.
4255
* @param server Server for which to get the base URI

src/main/java/me/pagar/api/PagarmeApiSDKClient.java

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77
package me.pagar.api;
88

99
import io.apimatic.core.GlobalConfiguration;
10+
import io.apimatic.coreinterfaces.authentication.Authentication;
1011
import io.apimatic.coreinterfaces.compatibility.CompatibilityFactory;
1112
import io.apimatic.coreinterfaces.http.HttpClient;
1213
import io.apimatic.okhttpclient.adapter.OkClient;
1314
import java.util.HashMap;
1415
import java.util.Map;
1516
import java.util.function.Consumer;
17+
import me.pagar.api.authentication.BasicAuthManager;
18+
import me.pagar.api.authentication.BasicAuthModel;
1619
import me.pagar.api.controllers.BalanceOperationsController;
1720
import me.pagar.api.controllers.ChargesController;
1821
import me.pagar.api.controllers.CustomersController;
@@ -65,7 +68,7 @@ public final class PagarmeApiSDKClient implements PagarmeApiSDKClientInterface {
6568

6669
private static final CompatibilityFactory compatibilityFactory = new CompatibilityFactoryImpl();
6770

68-
private static String userAgent = "PagarmeApiSDK - Java 6.8.5";
71+
private static String userAgent = "PagarmeApiSDK - Java 6.8.6";
6972

7073
/**
7174
* Current API environment.
@@ -87,15 +90,47 @@ public final class PagarmeApiSDKClient implements PagarmeApiSDKClientInterface {
8790
*/
8891
private final ReadonlyHttpClientConfiguration httpClientConfig;
8992

93+
/**
94+
* BasicAuthManager.
95+
*/
96+
private BasicAuthManager basicAuthManager;
97+
98+
/**
99+
* The instance of BasicAuthModel.
100+
*/
101+
private BasicAuthModel basicAuthModel;
102+
103+
/**
104+
* Map of authentication Managers.
105+
*/
106+
private Map<String, Authentication> authentications;
107+
90108
private PagarmeApiSDKClient(Environment environment, String serviceRefererName,
91-
HttpClient httpClient, ReadonlyHttpClientConfiguration httpClientConfig) {
109+
HttpClient httpClient, ReadonlyHttpClientConfiguration httpClientConfig,
110+
BasicAuthModel basicAuthModel, Map<String, Authentication> authentications) {
92111
this.environment = environment;
93112
this.serviceRefererName = serviceRefererName;
94113
this.httpClient = httpClient;
95114
this.httpClientConfig = httpClientConfig;
115+
this.authentications =
116+
(authentications == null) ? new HashMap<>() : new HashMap<>(authentications);
117+
this.basicAuthModel = basicAuthModel;
118+
119+
if (this.authentications.containsKey("httpBasic")) {
120+
this.basicAuthManager = (BasicAuthManager) this.authentications.get("httpBasic");
121+
}
122+
123+
if (!this.authentications.containsKey("httpBasic")
124+
|| !getBasicAuthCredentials().equals(basicAuthModel.getUsername(),
125+
basicAuthModel.getPassword())) {
126+
this.basicAuthManager = new BasicAuthManager(basicAuthModel);
127+
this.authentications.put("httpBasic", basicAuthManager);
128+
}
129+
96130
GlobalConfiguration globalConfig = new GlobalConfiguration.Builder()
97131
.httpClient(httpClient).baseUri(server -> getBaseUri(server))
98132
.compatibilityFactory(compatibilityFactory)
133+
.authentication(this.authentications)
99134
.userAgent(userAgent)
100135
.globalHeader("ServiceRefererName", serviceRefererName)
101136
.build();
@@ -248,6 +283,21 @@ public ReadonlyHttpClientConfiguration getHttpClientConfig() {
248283
return httpClientConfig;
249284
}
250285

286+
/**
287+
* The credentials to use with BasicAuth.
288+
* @return basicAuthCredentials
289+
*/
290+
public BasicAuthCredentials getBasicAuthCredentials() {
291+
return basicAuthManager;
292+
}
293+
294+
/**
295+
* The auth credential model for BasicAuth.
296+
* @return the instance of BasicAuthModel
297+
*/
298+
public BasicAuthModel getBasicAuthModel() {
299+
return basicAuthModel;
300+
}
251301
/**
252302
* The timeout to use for making HTTP requests.
253303
* @deprecated This method will be removed in a future version. Use
@@ -311,7 +361,8 @@ private static String environmentMapper(Environment environment, Server server)
311361
@Override
312362
public String toString() {
313363
return "PagarmeApiSDKClient [" + "environment=" + environment + ", serviceRefererName="
314-
+ serviceRefererName + ", httpClientConfig=" + httpClientConfig + "]";
364+
+ serviceRefererName + ", httpClientConfig=" + httpClientConfig
365+
+ ", authentications=" + authentications + "]";
315366
}
316367

317368
/**
@@ -324,6 +375,9 @@ public Builder newBuilder() {
324375
builder.environment = getEnvironment();
325376
builder.serviceRefererName = getServiceRefererName();
326377
builder.httpClient = getHttpClient();
378+
builder.basicAuthCredentials(getBasicAuthModel()
379+
.toBuilder().build());
380+
builder.authentications = authentications;
327381
builder.httpClientConfig(configBldr -> configBldr =
328382
((HttpClientConfiguration) httpClientConfig).newBuilder());
329383
return builder;
@@ -337,10 +391,39 @@ public static class Builder {
337391
private Environment environment = Environment.PRODUCTION;
338392
private String serviceRefererName = "";
339393
private HttpClient httpClient;
394+
private BasicAuthModel basicAuthModel = new BasicAuthModel.Builder("", "").build();
395+
private Map<String, Authentication> authentications = null;
340396
private HttpClientConfiguration.Builder httpClientConfigBuilder =
341397
new HttpClientConfiguration.Builder();
342398

343399

400+
/**
401+
* Credentials setter for BasicAuth.
402+
* @param basicAuthUserName String value for basicAuthUserName.
403+
* @param basicAuthPassword String value for basicAuthPassword.
404+
* @deprecated This builder method is deprecated.
405+
* Use {@link #basicAuthCredentials(BasicAuthModel) basicAuthCredentials} instead.
406+
* @return The current instance of builder.
407+
*/
408+
@Deprecated
409+
public Builder basicAuthCredentials(String basicAuthUserName, String basicAuthPassword) {
410+
basicAuthModel = basicAuthModel.toBuilder()
411+
.username(basicAuthUserName)
412+
.password(basicAuthPassword)
413+
.build();
414+
return this;
415+
}
416+
417+
/**
418+
* Credentials setter for BasicAuthCredentials.
419+
* @param basicAuthModel The instance of BasicAuthModel.
420+
* @return The current instance of builder.
421+
*/
422+
public Builder basicAuthCredentials(BasicAuthModel basicAuthModel) {
423+
this.basicAuthModel = basicAuthModel;
424+
return this;
425+
}
426+
344427
/**
345428
* Current API environment.
346429
* @param environment The environment for client.
@@ -398,7 +481,7 @@ public PagarmeApiSDKClient build() {
398481
httpClient = new OkClient(httpClientConfig.getConfiguration(), compatibilityFactory);
399482

400483
return new PagarmeApiSDKClient(environment, serviceRefererName, httpClient,
401-
httpClientConfig);
484+
httpClientConfig, basicAuthModel, authentications);
402485
}
403486
}
404487
}

0 commit comments

Comments
 (0)