Skip to content

Commit 3b464cf

Browse files
committed
BackendApi: add oAuthManager to constructor
1 parent 1ba1d88 commit 3b464cf

File tree

4 files changed

+60
-47
lines changed

4 files changed

+60
-47
lines changed

java/com/samourai/wallet/api/backend/BackendApi.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.samourai.wallet.api.backend.beans.UnspentResponse;
77
import com.samourai.wallet.util.oauth.OAuthApi;
88
import com.samourai.wallet.util.oauth.OAuthManager;
9+
import java8.util.Optional;
910
import org.apache.commons.lang3.StringUtils;
1011
import org.slf4j.Logger;
1112
import org.slf4j.LoggerFactory;
@@ -25,14 +26,14 @@ public class BackendApi implements OAuthApi {
2526

2627
private IBackendClient httpClient;
2728
private String urlBackend;
28-
private OAuthManager oAuthManager;
29+
private Optional<OAuthManager> oAuthManager;
2930

30-
public BackendApi(IBackendClient httpClient, String urlBackend, String apiKey) {
31+
public BackendApi(IBackendClient httpClient, String urlBackend, Optional<OAuthManager> oAuthManager) {
3132
this.httpClient = httpClient;
3233
this.urlBackend = urlBackend;
33-
this.oAuthManager = (apiKey != null ? new OAuthManager(apiKey) : null);
34+
this.oAuthManager = oAuthManager;
3435
if (log.isDebugEnabled()) {
35-
String oAuthStr = oAuthManager != null ? "yes" : "no";
36+
String oAuthStr = oAuthManager.isPresent() ? "yes" : "no";
3637
log.debug("urlBackend=" + urlBackend + ", oAuth=" + oAuthStr);
3738
}
3839
}
@@ -148,9 +149,9 @@ public boolean testConnectivity() {
148149

149150
protected Map<String,String> computeHeaders() throws Exception {
150151
Map<String,String> headers = new HashMap<String, String>();
151-
if (oAuthManager != null) {
152+
if (oAuthManager.isPresent()) {
152153
// add auth token
153-
headers.put("Authorization", "Bearer " + oAuthManager.computeAccessToken(this));
154+
headers.put("Authorization", "Bearer " + oAuthManager.get().getOAuthAccessToken(this));
154155
}
155156
return headers;
156157
}
Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,7 @@
11
package com.samourai.wallet.util.oauth;
22

3-
import com.auth0.jwt.interfaces.DecodedJWT;
4-
import com.samourai.wallet.api.backend.beans.RefreshTokenResponse;
3+
public interface OAuthManager {
54

6-
public class OAuthManager {
7-
private OAuthImpl oAuthImpl;
8-
private String apiKey;
5+
String getOAuthAccessToken(OAuthApi oAuthApi) throws Exception;
96

10-
private DecodedJWT accessToken;
11-
private DecodedJWT refreshToken;
12-
13-
public OAuthManager(String apiKey) {
14-
this.oAuthImpl = new OAuthImpl();
15-
this.apiKey = apiKey;
16-
}
17-
18-
public String computeAccessToken(OAuthApi oAuthApi) throws Exception {
19-
if (accessToken != null) {
20-
boolean valid = oAuthImpl.validate(accessToken);
21-
if (valid) {
22-
// accessToken is valid
23-
return oAuthImpl.tokenToString(accessToken);
24-
}
25-
}
26-
return newAccessToken(oAuthApi);
27-
}
28-
29-
public String newAccessToken(OAuthApi oAuthApi) throws Exception {
30-
if (refreshToken != null) {
31-
boolean valid = oAuthImpl.validate(refreshToken);
32-
if (valid) {
33-
// refreshToken is valid => refresh
34-
String accessTokenStr = oAuthApi.oAuthRefresh(oAuthImpl.tokenToString(refreshToken));
35-
this.accessToken = oAuthImpl.decode(accessTokenStr);
36-
return oAuthImpl.tokenToString(accessToken);
37-
}
38-
}
39-
40-
// no refreshToken => authenticate
41-
RefreshTokenResponse.Authorization auth = oAuthApi.oAuthAuthenticate(apiKey);
42-
this.accessToken = oAuthImpl.decode(auth.access_token);
43-
this.refreshToken = oAuthImpl.decode(auth.refresh_token);
44-
return oAuthImpl.tokenToString(accessToken);
45-
}
467
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.samourai.wallet.util.oauth;
2+
3+
import com.auth0.jwt.interfaces.DecodedJWT;
4+
import com.samourai.wallet.api.backend.beans.RefreshTokenResponse;
5+
6+
public class OAuthManagerJava implements OAuthManager {
7+
private OAuthImpl oAuthImpl;
8+
private String apiKey;
9+
10+
private DecodedJWT accessToken;
11+
private DecodedJWT refreshToken;
12+
13+
public OAuthManagerJava(String apiKey) {
14+
this.oAuthImpl = new OAuthImpl();
15+
this.apiKey = apiKey;
16+
}
17+
18+
public String getOAuthAccessToken(OAuthApi oAuthApi) throws Exception {
19+
if (accessToken != null) {
20+
boolean valid = oAuthImpl.validate(accessToken);
21+
if (valid) {
22+
// accessToken is valid
23+
return oAuthImpl.tokenToString(accessToken);
24+
}
25+
}
26+
return newAccessToken(oAuthApi);
27+
}
28+
29+
protected String newAccessToken(OAuthApi oAuthApi) throws Exception {
30+
if (refreshToken != null) {
31+
boolean valid = oAuthImpl.validate(refreshToken);
32+
if (valid) {
33+
// refreshToken is valid => refresh
34+
String accessTokenStr = oAuthApi.oAuthRefresh(oAuthImpl.tokenToString(refreshToken));
35+
this.accessToken = oAuthImpl.decode(accessTokenStr);
36+
return oAuthImpl.tokenToString(accessToken);
37+
}
38+
}
39+
40+
// no refreshToken => authenticate
41+
RefreshTokenResponse.Authorization auth = oAuthApi.oAuthAuthenticate(apiKey);
42+
this.accessToken = oAuthImpl.decode(auth.access_token);
43+
this.refreshToken = oAuthImpl.decode(auth.refresh_token);
44+
return oAuthImpl.tokenToString(accessToken);
45+
}
46+
}

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@
4646
<artifactId>jackson-databind</artifactId>
4747
<version>2.9.10.1</version>
4848
</dependency>
49+
<dependency>
50+
<groupId>net.sourceforge.streamsupport</groupId>
51+
<artifactId>streamsupport</artifactId>
52+
<version>1.7.0</version>
53+
</dependency>
4954
<dependency>
5055
<groupId>org.junit.platform</groupId>
5156
<artifactId>junit-platform-launcher</artifactId>

0 commit comments

Comments
 (0)