Skip to content

Commit b1f3726

Browse files
authored
handle an OAUTH2 offline token refresh (#241)
* handle an OAUTH2 offline token refresh Signed-off-by: Laura Pardo <[email protected]>
1 parent e63da66 commit b1f3726

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ All notable changes to this add-on will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
55

66
## [Unreleased]
7-
7+
### Added
8+
- authentication/OfflineTokenRefresh.js - refresh oauth2 offline tokens
9+
- httpsender/AddBearerTokenHeader.js - refresh oauth2 offline tokens
810

911
## [11] - 2021-09-07
1012
### Added
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* This script is intended to be used along with httpsender/AddBearerTokenHeader.js to
3+
* handle an OAUTH2 offline token refresh workflow.
4+
*
5+
* authentication/OfflineTokenRefresher.js will automatically fetch the new access token for every unauthorized
6+
* request determined by the "Logged Out" or "Logged In" indicator previously set in Context -> Authentication.
7+
*
8+
* httpsender/AddBearerTokenHeader.js will add the new access token to all requests in scope
9+
* made by ZAP (except the authentication ones) as an "Authorization: Bearer [access_token]" HTTP Header.
10+
*
11+
* @author Laura Pardo <lpardo at redhat.com>
12+
*/
13+
14+
var HttpRequestHeader = Java.type("org.parosproxy.paros.network.HttpRequestHeader");
15+
var HttpHeader = Java.type("org.parosproxy.paros.network.HttpHeader");
16+
var URI = Java.type("org.apache.commons.httpclient.URI");
17+
var ScriptVars = Java.type('org.zaproxy.zap.extension.script.ScriptVars');
18+
19+
20+
function authenticate(helper, paramsValues, credentials) {
21+
22+
var token_endpoint = paramsValues.get("token_endpoint");
23+
var client_id = paramsValues.get("client_id");
24+
var refresh_token = credentials.getParam("refresh_token");
25+
26+
// Build body
27+
var refreshTokenBody = "client_id=" + client_id;
28+
refreshTokenBody+= "&grant_type=refresh_token";
29+
refreshTokenBody+= "&refresh_token=" + refresh_token;
30+
31+
// Build header
32+
var tokenRequestURI = new URI(token_endpoint, false);
33+
var tokenRequestMethod = HttpRequestHeader.POST;
34+
var tokenRequestMainHeader = new HttpRequestHeader(tokenRequestMethod, tokenRequestURI, HttpHeader.HTTP11);
35+
36+
// Build message
37+
var tokenMsg = helper.prepareMessage();
38+
tokenMsg.setRequestBody(refreshTokenBody);
39+
tokenMsg.setRequestHeader(tokenRequestMainHeader);
40+
tokenMsg.getRequestHeader().setContentLength(tokenMsg.getRequestBody().length());
41+
42+
// Make the request and receive the response
43+
helper.sendAndReceive(tokenMsg, false);
44+
45+
// Parse the JSON response and save the new access_token in a global var
46+
// we will replace the Authentication header in AddBearerTokenHeader.js
47+
var json = JSON.parse(tokenMsg.getResponseBody().toString());
48+
var access_token = json['access_token'];
49+
50+
if (access_token){
51+
ScriptVars.setGlobalVar("access_token", access_token);
52+
}else{
53+
print("Error getting access token")
54+
}
55+
56+
return tokenMsg;
57+
}
58+
59+
60+
function getRequiredParamsNames(){
61+
return ["token_endpoint", "client_id"];
62+
}
63+
64+
65+
function getOptionalParamsNames(){
66+
return [];
67+
}
68+
69+
70+
function getCredentialsParamsNames(){
71+
return ["access_token", "refresh_token"];
72+
}

httpsender/AddBearerTokenHeader.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* This script is intended to be used along with authentication/OfflineTokenRefresher.js to
3+
* handle an OAUTH2 offline token refresh workflow.
4+
*
5+
* authentication/OfflineTokenRefresher.js will automatically fetch the new access token for every unauthorized
6+
* request determined by the "Logged Out" or "Logged In" indicator previously set in Context -> Authentication.
7+
*
8+
* httpsender/AddBearerTokenHeader.js will add the new access token to all requests in scope
9+
* made by ZAP (except the authentication ones) as an "Authorization: Bearer [access_token]" HTTP Header.
10+
*
11+
* @author Laura Pardo <lpardo at redhat.com>
12+
*/
13+
14+
var HttpSender = Java.type('org.parosproxy.paros.network.HttpSender');
15+
var ScriptVars = Java.type('org.zaproxy.zap.extension.script.ScriptVars');
16+
17+
function sendingRequest(msg, initiator, helper) {
18+
19+
// add Authorization header to all request in scope except the authorization request itself
20+
if (initiator !== HttpSender.AUTHENTICATION_INITIATOR && msg.isInScope()) {
21+
msg.getRequestHeader().setHeader("Authorization", "Bearer " + ScriptVars.getGlobalVar("access_token"));
22+
}
23+
24+
return msg;
25+
}
26+
27+
function responseReceived(msg, initiator, helper) {}

0 commit comments

Comments
 (0)