-
-
Notifications
You must be signed in to change notification settings - Fork 253
handle an OAUTH2 offline token refresh #241
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d9b276f
handle an OAUTH2 offline token refresh
lpardoRH 40f52e4
Apply suggestions from code review
lpardoRH 60d7c66
Merge branch 'main' of https://github.com/zaproxy/community-scripts
lpardoRH f94a356
CHANGES REQUESTED: move scripts in CHANGELOG to Unreleased section
lpardoRH d560aeb
fixed missing header in CHANGELOG
lpardoRH 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
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
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,72 @@ | ||
| /* | ||
| * This script is intended to be used along with httpsender/AddBearerTokenHeader.js to | ||
| * handle an OAUTH2 offline token refresh workflow. | ||
| * | ||
| * authentication/OfflineTokenRefresher.js will automatically fetch the new access token for every unauthorized | ||
| * request determined by the "Logged Out" or "Logged In" indicator previously set in Context -> Authentication. | ||
| * | ||
| * httpsender/AddBearerTokenHeader.js will add the new access token to all requests in scope | ||
| * made by ZAP (except the authentication ones) as an "Authorization: Bearer [access_token]" HTTP Header. | ||
| * | ||
| * @author Laura Pardo <lpardo at redhat.com> | ||
| */ | ||
|
|
||
| var HttpRequestHeader = Java.type("org.parosproxy.paros.network.HttpRequestHeader"); | ||
| var HttpHeader = Java.type("org.parosproxy.paros.network.HttpHeader"); | ||
| var URI = Java.type("org.apache.commons.httpclient.URI"); | ||
| var ScriptVars = Java.type('org.zaproxy.zap.extension.script.ScriptVars'); | ||
|
|
||
|
|
||
| function authenticate(helper, paramsValues, credentials) { | ||
|
|
||
| var token_endpoint = paramsValues.get("token_endpoint"); | ||
| var client_id = paramsValues.get("client_id"); | ||
| var refresh_token = credentials.getParam("refresh_token"); | ||
|
|
||
| // Build body | ||
| var refreshTokenBody = "client_id=" + client_id; | ||
| refreshTokenBody+= "&grant_type=refresh_token"; | ||
| refreshTokenBody+= "&refresh_token=" + refresh_token; | ||
|
|
||
| // Build header | ||
| var tokenRequestURI = new URI(token_endpoint, false); | ||
| var tokenRequestMethod = HttpRequestHeader.POST; | ||
| var tokenRequestMainHeader = new HttpRequestHeader(tokenRequestMethod, tokenRequestURI, HttpHeader.HTTP11); | ||
|
|
||
| // Build message | ||
| var tokenMsg = helper.prepareMessage(); | ||
| tokenMsg.setRequestBody(refreshTokenBody); | ||
| tokenMsg.setRequestHeader(tokenRequestMainHeader); | ||
| tokenMsg.getRequestHeader().setContentLength(tokenMsg.getRequestBody().length()); | ||
|
|
||
| // Make the request and receive the response | ||
| helper.sendAndReceive(tokenMsg, false); | ||
|
|
||
| // Parse the JSON response and save the new access_token in a global var | ||
| // we will replace the Authentication header in AddBearerTokenHeader.js | ||
| var json = JSON.parse(tokenMsg.getResponseBody().toString()); | ||
| var access_token = json['access_token']; | ||
|
|
||
| if (access_token){ | ||
| ScriptVars.setGlobalVar("access_token", access_token); | ||
| }else{ | ||
| print("Error getting access token") | ||
| } | ||
|
|
||
| return tokenMsg; | ||
| } | ||
|
|
||
|
|
||
| function getRequiredParamsNames(){ | ||
| return ["token_endpoint", "client_id"]; | ||
| } | ||
|
|
||
|
|
||
| function getOptionalParamsNames(){ | ||
| return []; | ||
| } | ||
|
|
||
|
|
||
| function getCredentialsParamsNames(){ | ||
| return ["access_token", "refresh_token"]; | ||
| } | ||
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,27 @@ | ||
| /* | ||
| * This script is intended to be used along with authentication/OfflineTokenRefresher.js to | ||
| * handle an OAUTH2 offline token refresh workflow. | ||
| * | ||
| * authentication/OfflineTokenRefresher.js will automatically fetch the new access token for every unauthorized | ||
| * request determined by the "Logged Out" or "Logged In" indicator previously set in Context -> Authentication. | ||
| * | ||
| * httpsender/AddBearerTokenHeader.js will add the new access token to all requests in scope | ||
| * made by ZAP (except the authentication ones) as an "Authorization: Bearer [access_token]" HTTP Header. | ||
| * | ||
| * @author Laura Pardo <lpardo at redhat.com> | ||
| */ | ||
|
|
||
| var HttpSender = Java.type('org.parosproxy.paros.network.HttpSender'); | ||
| var ScriptVars = Java.type('org.zaproxy.zap.extension.script.ScriptVars'); | ||
|
|
||
| function sendingRequest(msg, initiator, helper) { | ||
|
|
||
| // add Authorization header to all request in scope except the authorization request itself | ||
| if (initiator !== HttpSender.AUTHENTICATION_INITIATOR && msg.isInScope()) { | ||
| msg.getRequestHeader().setHeader("Authorization", "Bearer " + ScriptVars.getGlobalVar("access_token")); | ||
| } | ||
|
|
||
| return msg; | ||
| } | ||
|
|
||
| function responseReceived(msg, initiator, helper) {} |
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.