-
-
Notifications
You must be signed in to change notification settings - Fork 434
[Issue #193] Integrate OAuth2 Authentication into Core Repository #676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
prayascoriolis
wants to merge
20
commits into
authorjapps:master
Choose a base branch
from
prayascoriolis:ISSUE-193-OAuth2-Support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
92003ee
Important files assembled for OAuth2 testing
prayascoriolis 1c7323b
resolved paths error
prayascoriolis 7d35f86
commenting plugin in pom.xml for ruuning kfka test
prayascoriolis 8b40305
Merge branch 'master' into ISSUE-193-OAuth2-Support
prayascoriolis aba383b
Description improved
authorjapps 0603dbf
Install docker compose manually and try
authorjapps 19690a1
Merge branch 'ISSUE-193-OAuth2-Support' of https://github.com/prayasc…
prayascoriolis d349285
uncommenting plugin in pom.xml for ruuning kfka test
prayascoriolis ef2b685
grant_type fetched from host.properties, name-value pair in post url
prayascoriolis 159be2b
separate assertions & unit test class used for refresh & acces token …
prayascoriolis ea564c5
Access Token workflow implemented
prayascoriolis 3f35c99
host.properties modified for access and refresh token
prayascoriolis aab323b
updated the comments for oauth2 code
prayascoriolis e529bae
fix: resolve issue with mock server not matching incoming requests
prayascoriolis adcb897
removed duplicate localhost_REST_fake_end_points_stubs.json from http…
prayascoriolis 14d65c9
end to end access token implemetation with fake REST end points
prayascoriolis 626f82e
Merge branch 'master' into ISSUE-193-OAuth2-Support
prayascoriolis 0cc5aef
accountsUrl changed to accessTokenUrl, class names changed to CamelCa…
prayascoriolis 2efa3c6
Merge branch 'ISSUE-193-OAuth2-Support' of https://github.com/prayasc…
prayascoriolis e7106cf
Merge branch 'master' into ISSUE-193-OAuth2-Support
prayascoriolis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
core/src/main/java/org/jsmart/zerocode/core/httpclient/oauth2/OAuth2HttpClient.java
This file contains hidden or 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,83 @@ | ||
package org.jsmart.zerocode.core.httpclient.oauth2; | ||
|
||
import java.util.Map; | ||
import java.util.Timer; | ||
|
||
import org.jsmart.zerocode.core.httpclient.BasicHttpClient; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.google.inject.Inject; | ||
import com.google.inject.name.Named; | ||
|
||
import org.apache.http.client.methods.RequestBuilder; | ||
|
||
/** | ||
* @author santhoshTpixler | ||
* | ||
*/ | ||
|
||
|
||
/* | ||
* Note: This implementation supports the OAuth2.0 with refresh_token | ||
* | ||
* Reference: https://tools.ietf.org/html/rfc6749#page-11 | ||
* | ||
* | ||
* 1. The refresh_token, access_token URL, client_id and client_secret | ||
* should be generated by the user and stored in the properties file | ||
* mentioned in the @TargetEnv("host.properties"). | ||
* 2. For generating the refresh token REST Client such as Insomnia (https://insomnia.rest/) can | ||
* be used. | ||
* | ||
* Note: Postman cannot be used as it does not show the refresh token. | ||
*/ | ||
public class OAuth2HttpClient extends BasicHttpClient { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(OAuth2HttpClient.class); | ||
|
||
/* | ||
* Properties to be fetched from the host.properties | ||
*/ | ||
private static final String CLIENT_ID = "client_id"; | ||
private static final String CLIENT_SECRET = "client_secret"; | ||
private static final String REFRESH_TOKEN = "refresh_token"; | ||
private static final String ACCOUNTS_URL = "accounts_url"; | ||
|
||
/* | ||
* If the Authorization header contains the replacement value as specified by the | ||
* below constant, then it is replaced with the valid access token | ||
*/ | ||
private static final String ACCESS_TOKEN_REPLACEMENT_VALUE = "DIY"; | ||
/* | ||
* Time interval in which the accessToken should be renewed | ||
*/ | ||
private static final long REFRESH_INTERVAL = 3540000; | ||
|
||
private OAuth2Impl oauth2 = null; | ||
|
||
@Inject | ||
public OAuth2HttpClient(@Named(CLIENT_ID) String clientId, @Named(CLIENT_SECRET) String clientSecret, | ||
@Named(REFRESH_TOKEN) String refreshToken, @Named(ACCOUNTS_URL) String accountsURL) { | ||
this.oauth2 = new OAuth2Impl(clientId, clientSecret, refreshToken, accountsURL); | ||
Timer timer = new Timer(); | ||
timer.schedule(oauth2, 0, REFRESH_INTERVAL); | ||
synchronized (oauth2) { | ||
try { | ||
oauth2.wait(); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public RequestBuilder handleHeaders(Map<String, Object> headers, RequestBuilder requestBuilder) { | ||
String authorization = (String) headers.get("Authorization"); | ||
if (authorization != null && authorization.equals(ACCESS_TOKEN_REPLACEMENT_VALUE)) { | ||
headers.put("Authorization", oauth2.getAccessToken()); | ||
LOGGER.info("Token injected into header."); | ||
} | ||
return super.handleHeaders(headers, requestBuilder); | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
core/src/main/java/org/jsmart/zerocode/core/httpclient/oauth2/OAuth2Impl.java
This file contains hidden or 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,108 @@ | ||
package org.jsmart.zerocode.core.httpclient.oauth2; | ||
|
||
import java.io.InputStream; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.TimerTask; | ||
|
||
import org.apache.http.NameValuePair; | ||
import org.apache.http.client.entity.UrlEncodedFormEntity; | ||
import org.apache.http.client.methods.CloseableHttpResponse; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClients; | ||
import org.apache.http.message.BasicNameValuePair; | ||
import org.json.JSONObject; | ||
import org.json.JSONTokener; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* @author santhoshTpixler | ||
* | ||
*/ | ||
|
||
/* | ||
* Note: This implementation supports the OAuth2.0 with refresh_token | ||
* | ||
* Reference: https://tools.ietf.org/html/rfc6749#page-11 | ||
*/ | ||
public class OAuth2Impl extends TimerTask { | ||
private String clienId; | ||
private String clientSecret; | ||
private String refreshToken; | ||
private String accessTokenURL; | ||
|
||
private String accessToken; | ||
private static final Logger LOGGER = LoggerFactory.getLogger(OAuth2Impl.class); | ||
|
||
public OAuth2Impl(String clientId, String clientSecret, String refreshToken, String accountsUrl) { | ||
this.clienId = clientId; | ||
this.clientSecret = clientSecret; | ||
this.refreshToken = refreshToken; | ||
this.accessTokenURL = accountsUrl; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
generateToken(); | ||
} | ||
|
||
public synchronized String getAccessToken() { | ||
return accessToken; | ||
|
||
} | ||
|
||
private synchronized void setAccessToken(String token) { | ||
this.accessToken = "Bearer " + token; | ||
} | ||
|
||
/** | ||
* Makes a POST request to the accessTokenURL to fetch the accesstoken | ||
*/ | ||
private synchronized void generateToken() { | ||
try (CloseableHttpClient client = HttpClients.createDefault()) { | ||
|
||
|
||
StringBuilder URL = new StringBuilder(accessTokenURL); | ||
URL.append('?'); | ||
URL.append("refresh_token=" + refreshToken); | ||
URL.append("&client_id=" + clienId);; | ||
URL.append("&client_secret=" + clientSecret); | ||
URL.append("&grant_type=refresh_token"); | ||
HttpPost post = new HttpPost(URL.toString()); | ||
|
||
|
||
/* | ||
* Below code was not compatible with simulator. In production kindly make | ||
* use of the below code. | ||
*/ | ||
|
||
/* | ||
* List<NameValuePair> nameValuePairs = new ArrayList<>(4); | ||
* nameValuePairs.add(new BasicNameValuePair("refresh_token", refreshToken)); | ||
* nameValuePairs.add(new BasicNameValuePair("client_secret", clientSecret)); | ||
* nameValuePairs.add(new BasicNameValuePair("client_id", clienId)); | ||
* nameValuePairs.add(new BasicNameValuePair("grant_type", "refresh_token")); | ||
* post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); | ||
*/ | ||
|
||
JSONObject jsonRespone = null; | ||
try (CloseableHttpResponse response = client.execute(post);) { | ||
try (InputStream stream = response.getEntity().getContent()) { | ||
jsonRespone = new JSONObject(new JSONTokener(stream)); | ||
} | ||
} | ||
if (accessToken == null) { | ||
setAccessToken(jsonRespone.getString("access_token")); | ||
this.notifyAll(); | ||
} else { | ||
setAccessToken(jsonRespone.getString("access_token")); | ||
} | ||
} catch (Exception e) { | ||
LOGGER.error("Cannot fetch access token from IAM", e); | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.