-
-
Notifications
You must be signed in to change notification settings - Fork 246
feat: add Ory Kratos authentication scripts #447
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
thc202
merged 7 commits into
zaproxy:main
from
getlarge:feat-add-kratos-authentication-scripts
Jun 13, 2024
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e15f887
feat: add script to authenticate via Ory Kratos native flow
getlarge 10fa644
feat: add script to authenticate via Ory Kratos browser flow
getlarge a004bb7
chore: lint files
getlarge 07b1895
chore: update changelog
getlarge 23bb972
docs: suggest Authentication Verification for Kratos authentication s…
getlarge ad55761
fix: apply review suggestions
getlarge 8db8b93
fix: format code
getlarge 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,135 @@ | ||
/* | ||
* This script is used to authenticate a user using Ory Kratos self-service API for clients without browsers. | ||
* | ||
* Authentication Verification can be configured with the following rule: | ||
* - Verification Strategy: Poll the Specified URL | ||
* - Regex Pattern used to identify Logged In message: \Qactive.* | ||
* - URL to poll: <Kratos Base URL>/sessions/whoami | ||
* | ||
* Zap must be configured to use HTTP header-based session management with the value: {%json:session_token%} | ||
thc202 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @author Edouard Maleix <ed@getlarge.eu> | ||
* @see https://www.ory.sh/docs/kratos/self-service/flows/user-login#login-for-api-clients-and-clients-without-browsers | ||
*/ | ||
|
||
const HttpRequestHeader = Java.type( | ||
"org.parosproxy.paros.network.HttpRequestHeader" | ||
); | ||
const HttpHeader = Java.type("org.parosproxy.paros.network.HttpHeader"); | ||
const URI = Java.type("org.apache.commons.httpclient.URI"); | ||
const AuthenticationHelper = Java.type( | ||
"org.zaproxy.zap.authentication.AuthenticationHelper" | ||
); | ||
|
||
/** | ||
* @typedef {Object} AuthHelper | ||
* @property {function(): Object} prepareMessage - Prepares an HTTP message. | ||
* @property {function(Object, boolean=): void} sendAndReceive - Sends the HTTP message and receives the response. | ||
*/ | ||
|
||
/** | ||
* @typedef {Object} ParamsValues | ||
* @property {function(string): string} get - Gets the value of a parameter. | ||
* @property {function(string): void} set - Sets the value of a parameter. | ||
*/ | ||
|
||
/** | ||
* @typedef {Object} Credentials | ||
* @property {function(string): string} getParam - Gets the value of a parameter. The param names are the ones returned by the getCredentialsParamsNames() below | ||
*/ | ||
|
||
/** | ||
* @param {AuthHelper} helper - The authentication helper object provided by ZAP. | ||
* @param {ParamsValues} paramsValues - The map of parameter values configured in the Session Properties - Authentication panel. | ||
* @param {Credentials} credentials - an object containing the credentials values, as configured in the Session Properties - Users panel. | ||
* @returns {Object} The HTTP message used to perform the authentication. | ||
*/ | ||
function authenticate(helper, paramsValues, credentials) { | ||
print("Authenticating via Ory Kratos..."); | ||
|
||
// Step 1: Initialize the login flow | ||
const kratosBaseUri = paramsValues.get("Kratos Base URL"); | ||
const initLoginUri = new URI( | ||
kratosBaseUri + "/self-service/login/api", | ||
false | ||
thc202 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
); | ||
const initLoginMsg = helper.prepareMessage(); | ||
initLoginMsg.setRequestHeader( | ||
new HttpRequestHeader( | ||
HttpRequestHeader.GET, | ||
initLoginUri, | ||
HttpHeader.HTTP11 | ||
) | ||
); | ||
print("Sending GET request to " + initLoginUri); | ||
helper.sendAndReceive(initLoginMsg); | ||
print( | ||
"Received response status code: " + | ||
initLoginMsg.getResponseHeader().getStatusCode() | ||
); | ||
AuthenticationHelper.addAuthMessageToHistory(initLoginMsg); | ||
|
||
// Step 2: Submit login credentials | ||
const actionUrl = JSON.parse(initLoginMsg.getResponseBody().toString()).ui | ||
.action; | ||
const loginUri = new URI(actionUrl, false); | ||
const loginMsg = helper.prepareMessage(); | ||
const requestBody = JSON.stringify({ | ||
method: "password", | ||
identifier: credentials.getParam("username"), | ||
password: credentials.getParam("password"), | ||
}); | ||
loginMsg.setRequestBody(requestBody); | ||
|
||
const requestHeader = new HttpRequestHeader( | ||
HttpRequestHeader.POST, | ||
loginUri, | ||
HttpHeader.HTTP11 | ||
); | ||
loginMsg.setRequestHeader(requestHeader); | ||
|
||
// Build the POST request header | ||
loginMsg | ||
.getRequestHeader() | ||
.setHeader(HttpHeader.CONTENT_TYPE, "application/json"); | ||
loginMsg | ||
.getRequestHeader() | ||
.setContentLength(loginMsg.getRequestBody().length()); | ||
|
||
print("Sending POST request to " + loginUri); | ||
helper.sendAndReceive(loginMsg, false); | ||
print( | ||
"Received response status code: " + | ||
loginMsg.getResponseHeader().getStatusCode() | ||
); | ||
AuthenticationHelper.addAuthMessageToHistory(loginMsg); | ||
|
||
return loginMsg; | ||
} | ||
|
||
/** | ||
* Returns the required parameter names. | ||
* | ||
* @returns {Array<string>} An array of required parameter names. | ||
*/ | ||
function getRequiredParamsNames() { | ||
return ["Kratos Base URL"]; | ||
} | ||
|
||
/** | ||
* Returns the optional parameter names. | ||
* | ||
* @returns {Array<string>} An array of optional parameter names. | ||
*/ | ||
function getOptionalParamsNames() { | ||
return []; | ||
} | ||
|
||
/** | ||
* Returns the credentials parameter names. | ||
* | ||
* @returns {Array<string>} An array of credentials parameter names. | ||
*/ | ||
function getCredentialsParamsNames() { | ||
return ["username", "password"]; | ||
} |
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,182 @@ | ||
/* | ||
* @description This script is used to authenticate a user using Ory Kratos self-service API for browser. | ||
* | ||
* Authentication Verification can be configured with the following rule: | ||
* - Verification Strategy: Poll the Specified URL | ||
* - Regex Pattern used to identify Logged In message: \Qactive.* | ||
* - URL to poll: <Kratos Base URL>/sessions/whoami | ||
* | ||
* Zap must be configured to use cookie-based session management. | ||
* | ||
* @author Edouard Maleix <ed@getlarge.eu> | ||
* @see https://www.ory.sh/docs/kratos/self-service/flows/user-login#login-for-server-side-browser-clients | ||
*/ | ||
|
||
const HttpRequestHeader = Java.type( | ||
"org.parosproxy.paros.network.HttpRequestHeader" | ||
); | ||
const HttpHeader = Java.type("org.parosproxy.paros.network.HttpHeader"); | ||
const URI = Java.type("org.apache.commons.httpclient.URI"); | ||
const AuthenticationHelper = Java.type( | ||
"org.zaproxy.zap.authentication.AuthenticationHelper" | ||
); | ||
const Source = Java.type("net.htmlparser.jericho.Source"); | ||
|
||
/** | ||
* @typedef {Object} AuthHelper | ||
* @property {function(): Object} prepareMessage - Prepares an HTTP message. | ||
* @property {function(Object, boolean=): void} sendAndReceive - Sends the HTTP message and receives the response. | ||
*/ | ||
|
||
/** | ||
* @typedef {Object} ParamsValues | ||
* @property {function(string): string} get - Gets the value of a parameter. | ||
* @property {function(string): void} set - Sets the value of a parameter. | ||
*/ | ||
|
||
/** | ||
* @typedef {Object} Credentials | ||
* @property {function(string): string} getParam - Gets the value of a parameter. The param names are the ones returned by the getCredentialsParamsNames() below | ||
*/ | ||
|
||
/** | ||
* @param {AuthHelper} helper - The authentication helper object provided by ZAP. | ||
* @param {ParamsValues} paramsValues - The map of parameter values configured in the Session Properties - Authentication panel. | ||
* @param {Credentials} credentials - an object containing the credentials values, as configured in the Session Properties - Users panel. | ||
* @returns {Object} The HTTP message used to perform the authentication. | ||
*/ | ||
function authenticate(helper, paramsValues, credentials) { | ||
print("Authenticating via Ory Kratos..."); | ||
|
||
// Step 1: Initialize the login flow | ||
const kratosBaseUri = paramsValues.get("Kratos Base URL"); | ||
const initLoginUri = new URI( | ||
kratosBaseUri + "/self-service/login/browser", | ||
false | ||
); | ||
const initLoginMsg = helper.prepareMessage(); | ||
initLoginMsg.setRequestHeader( | ||
new HttpRequestHeader( | ||
HttpRequestHeader.GET, | ||
initLoginUri, | ||
HttpHeader.HTTP11 | ||
) | ||
); | ||
print("Sending GET request to " + initLoginUri); | ||
helper.sendAndReceive(initLoginMsg, true); | ||
print( | ||
"Received response status code: " + | ||
initLoginMsg.getResponseHeader().getStatusCode() | ||
); | ||
AuthenticationHelper.addAuthMessageToHistory(initLoginMsg); | ||
|
||
// Step 2: Submit login credentials | ||
const actionUrl = getActionurl(initLoginMsg); | ||
const loginUri = new URI(actionUrl, false); | ||
const loginMsg = helper.prepareMessage(); | ||
const csrf_token = getCsrfToken(initLoginMsg); | ||
const requestBody = | ||
"identifier=" + | ||
encodeURIComponent(credentials.getParam("username")) + | ||
"&password=" + | ||
encodeURIComponent(credentials.getParam("password")) + | ||
"&method=password" + | ||
"&csrf_token=" + | ||
encodeURIComponent(csrf_token); | ||
loginMsg.setRequestBody(requestBody); | ||
const requestHeader = new HttpRequestHeader( | ||
HttpRequestHeader.POST, | ||
loginUri, | ||
HttpHeader.HTTP11 | ||
); | ||
loginMsg.setRequestHeader(requestHeader); | ||
|
||
loginMsg | ||
.getRequestHeader() | ||
.setHeader(HttpHeader.CONTENT_TYPE, "application/x-www-form-urlencoded"); | ||
loginMsg | ||
.getRequestHeader() | ||
.setContentLength(loginMsg.getRequestBody().length()); | ||
|
||
print("Sending POST request to " + loginUri); | ||
//! disable redirect to get the cookies from Kratos | ||
helper.sendAndReceive(loginMsg, false); | ||
print( | ||
"Received response status code: " + | ||
loginMsg.getResponseHeader().getStatusCode() | ||
); | ||
AuthenticationHelper.addAuthMessageToHistory(loginMsg); | ||
|
||
return loginMsg; | ||
} | ||
|
||
/** | ||
* | ||
* @param {*} request | ||
* @returns | ||
*/ | ||
function getActionurl(request) { | ||
let iterator; | ||
let element; | ||
let actionUrl; | ||
const pageSource = | ||
request.getResponseHeader().toString() + | ||
request.getResponseBody().toString(); | ||
const src = new Source(pageSource); | ||
const elements = src.getAllElements("form"); | ||
|
||
for (iterator = elements.iterator(); iterator.hasNext(); ) { | ||
element = iterator.next(); | ||
actionUrl = element.getAttributeValue("action"); | ||
break; | ||
} | ||
|
||
return actionUrl; | ||
} | ||
|
||
function getCsrfToken(request) { | ||
let iterator; | ||
let element; | ||
let loginToken; | ||
const pageSource = | ||
request.getResponseHeader().toString() + | ||
request.getResponseBody().toString(); | ||
const src = new Source(pageSource); | ||
const elements = src.getAllElements("input"); | ||
|
||
for (iterator = elements.iterator(); iterator.hasNext(); ) { | ||
element = iterator.next(); | ||
if (element.getAttributeValue("name") == "csrf_token") { | ||
loginToken = element.getAttributeValue("value"); | ||
break; | ||
} | ||
} | ||
|
||
return loginToken; | ||
} | ||
/** | ||
* Returns the required parameter names. | ||
* | ||
* @returns {Array<string>} An array of required parameter names. | ||
*/ | ||
function getRequiredParamsNames() { | ||
return ["Kratos Base URL"]; | ||
} | ||
|
||
/** | ||
* Returns the optional parameter names. | ||
* | ||
* @returns {Array<string>} An array of optional parameter names. | ||
*/ | ||
function getOptionalParamsNames() { | ||
return []; | ||
} | ||
|
||
/** | ||
* Returns the credentials parameter names. | ||
* | ||
* @returns {Array<string>} An array of credentials parameter names. | ||
*/ | ||
function getCredentialsParamsNames() { | ||
return ["username", "password"]; | ||
} |
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.