-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BackendApi: add oAuthManager to constructor
- Loading branch information
Showing
4 changed files
with
60 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters