Skip to content

Commit

Permalink
BackendApi: add oAuthManager to constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroleak committed Jan 16, 2020
1 parent 1ba1d88 commit 3b464cf
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 47 deletions.
13 changes: 7 additions & 6 deletions java/com/samourai/wallet/api/backend/BackendApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.samourai.wallet.api.backend.beans.UnspentResponse;
import com.samourai.wallet.util.oauth.OAuthApi;
import com.samourai.wallet.util.oauth.OAuthManager;
import java8.util.Optional;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -25,14 +26,14 @@ public class BackendApi implements OAuthApi {

private IBackendClient httpClient;
private String urlBackend;
private OAuthManager oAuthManager;
private Optional<OAuthManager> oAuthManager;

public BackendApi(IBackendClient httpClient, String urlBackend, String apiKey) {
public BackendApi(IBackendClient httpClient, String urlBackend, Optional<OAuthManager> oAuthManager) {
this.httpClient = httpClient;
this.urlBackend = urlBackend;
this.oAuthManager = (apiKey != null ? new OAuthManager(apiKey) : null);
this.oAuthManager = oAuthManager;
if (log.isDebugEnabled()) {
String oAuthStr = oAuthManager != null ? "yes" : "no";
String oAuthStr = oAuthManager.isPresent() ? "yes" : "no";
log.debug("urlBackend=" + urlBackend + ", oAuth=" + oAuthStr);
}
}
Expand Down Expand Up @@ -148,9 +149,9 @@ public boolean testConnectivity() {

protected Map<String,String> computeHeaders() throws Exception {
Map<String,String> headers = new HashMap<String, String>();
if (oAuthManager != null) {
if (oAuthManager.isPresent()) {
// add auth token
headers.put("Authorization", "Bearer " + oAuthManager.computeAccessToken(this));
headers.put("Authorization", "Bearer " + oAuthManager.get().getOAuthAccessToken(this));
}
return headers;
}
Expand Down
43 changes: 2 additions & 41 deletions java/com/samourai/wallet/util/oauth/OAuthManager.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,7 @@
package com.samourai.wallet.util.oauth;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.samourai.wallet.api.backend.beans.RefreshTokenResponse;
public interface OAuthManager {

public class OAuthManager {
private OAuthImpl oAuthImpl;
private String apiKey;
String getOAuthAccessToken(OAuthApi oAuthApi) throws Exception;

private DecodedJWT accessToken;
private DecodedJWT refreshToken;

public OAuthManager(String apiKey) {
this.oAuthImpl = new OAuthImpl();
this.apiKey = apiKey;
}

public String computeAccessToken(OAuthApi oAuthApi) throws Exception {
if (accessToken != null) {
boolean valid = oAuthImpl.validate(accessToken);
if (valid) {
// accessToken is valid
return oAuthImpl.tokenToString(accessToken);
}
}
return newAccessToken(oAuthApi);
}

public String newAccessToken(OAuthApi oAuthApi) throws Exception {
if (refreshToken != null) {
boolean valid = oAuthImpl.validate(refreshToken);
if (valid) {
// refreshToken is valid => refresh
String accessTokenStr = oAuthApi.oAuthRefresh(oAuthImpl.tokenToString(refreshToken));
this.accessToken = oAuthImpl.decode(accessTokenStr);
return oAuthImpl.tokenToString(accessToken);
}
}

// no refreshToken => authenticate
RefreshTokenResponse.Authorization auth = oAuthApi.oAuthAuthenticate(apiKey);
this.accessToken = oAuthImpl.decode(auth.access_token);
this.refreshToken = oAuthImpl.decode(auth.refresh_token);
return oAuthImpl.tokenToString(accessToken);
}
}
46 changes: 46 additions & 0 deletions java/com/samourai/wallet/util/oauth/OAuthManagerJava.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.samourai.wallet.util.oauth;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.samourai.wallet.api.backend.beans.RefreshTokenResponse;

public class OAuthManagerJava implements OAuthManager {
private OAuthImpl oAuthImpl;
private String apiKey;

private DecodedJWT accessToken;
private DecodedJWT refreshToken;

public OAuthManagerJava(String apiKey) {
this.oAuthImpl = new OAuthImpl();
this.apiKey = apiKey;
}

public String getOAuthAccessToken(OAuthApi oAuthApi) throws Exception {
if (accessToken != null) {
boolean valid = oAuthImpl.validate(accessToken);
if (valid) {
// accessToken is valid
return oAuthImpl.tokenToString(accessToken);
}
}
return newAccessToken(oAuthApi);
}

protected String newAccessToken(OAuthApi oAuthApi) throws Exception {
if (refreshToken != null) {
boolean valid = oAuthImpl.validate(refreshToken);
if (valid) {
// refreshToken is valid => refresh
String accessTokenStr = oAuthApi.oAuthRefresh(oAuthImpl.tokenToString(refreshToken));
this.accessToken = oAuthImpl.decode(accessTokenStr);
return oAuthImpl.tokenToString(accessToken);
}
}

// no refreshToken => authenticate
RefreshTokenResponse.Authorization auth = oAuthApi.oAuthAuthenticate(apiKey);
this.accessToken = oAuthImpl.decode(auth.access_token);
this.refreshToken = oAuthImpl.decode(auth.refresh_token);
return oAuthImpl.tokenToString(accessToken);
}
}
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
<artifactId>jackson-databind</artifactId>
<version>2.9.10.1</version>
</dependency>
<dependency>
<groupId>net.sourceforge.streamsupport</groupId>
<artifactId>streamsupport</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
Expand Down

0 comments on commit 3b464cf

Please sign in to comment.