|
| 1 | +/* |
| 2 | + * @description This script is used to authenticate a user using Ory Kratos self-service API for browser. |
| 3 | + * |
| 4 | + * Authentication Verification can be configured with the following rule: |
| 5 | + * - Verification Strategy: Poll the Specified URL |
| 6 | + * - Regex Pattern used to identify Logged In message: active.* |
| 7 | + * - URL to poll: <Kratos Base URL>/sessions/whoami |
| 8 | + * |
| 9 | + * ZAP must be configured to use cookie-based session management. |
| 10 | + * |
| 11 | + * @author Edouard Maleix <ed@getlarge.eu> |
| 12 | + * @see https://www.ory.sh/docs/kratos/self-service/flows/user-login#login-for-server-side-browser-clients |
| 13 | + */ |
| 14 | + |
| 15 | +const HttpRequestHeader = Java.type( |
| 16 | + "org.parosproxy.paros.network.HttpRequestHeader" |
| 17 | +); |
| 18 | +const HttpHeader = Java.type("org.parosproxy.paros.network.HttpHeader"); |
| 19 | +const URI = Java.type("org.apache.commons.httpclient.URI"); |
| 20 | +const AuthenticationHelper = Java.type( |
| 21 | + "org.zaproxy.zap.authentication.AuthenticationHelper" |
| 22 | +); |
| 23 | +const Source = Java.type("net.htmlparser.jericho.Source"); |
| 24 | + |
| 25 | +/** |
| 26 | + * @typedef {Object} AuthHelper |
| 27 | + * @property {function(): Object} prepareMessage - Prepares an HTTP message. |
| 28 | + * @property {function(Object, boolean=): void} sendAndReceive - Sends the HTTP message and receives the response. |
| 29 | + */ |
| 30 | + |
| 31 | +/** |
| 32 | + * @typedef {Object} ParamsValues |
| 33 | + * @property {function(string): string} get - Gets the value of a parameter. |
| 34 | + * @property {function(string): void} set - Sets the value of a parameter. |
| 35 | + */ |
| 36 | + |
| 37 | +/** |
| 38 | + * @typedef {Object} Credentials |
| 39 | + * @property {function(string): string} getParam - Gets the value of a parameter. The param names are the ones returned by the getCredentialsParamsNames() below |
| 40 | + */ |
| 41 | + |
| 42 | +/** |
| 43 | + * @param {AuthHelper} helper - The authentication helper object provided by ZAP. |
| 44 | + * @param {ParamsValues} paramsValues - The map of parameter values configured in the Session Properties - Authentication panel. |
| 45 | + * @param {Credentials} credentials - an object containing the credentials values, as configured in the Session Properties - Users panel. |
| 46 | + * @returns {Object} The HTTP message used to perform the authentication. |
| 47 | + */ |
| 48 | +function authenticate(helper, paramsValues, credentials) { |
| 49 | + print("Authenticating via Ory Kratos..."); |
| 50 | + |
| 51 | + // Step 1: Initialize the login flow |
| 52 | + const kratosBaseUri = paramsValues.get("Kratos Base URL"); |
| 53 | + const initLoginUri = new URI( |
| 54 | + kratosBaseUri + "/self-service/login/browser", |
| 55 | + true |
| 56 | + ); |
| 57 | + const initLoginMsg = helper.prepareMessage(); |
| 58 | + initLoginMsg.setRequestHeader( |
| 59 | + new HttpRequestHeader( |
| 60 | + HttpRequestHeader.GET, |
| 61 | + initLoginUri, |
| 62 | + HttpHeader.HTTP11 |
| 63 | + ) |
| 64 | + ); |
| 65 | + print("Sending GET request to " + initLoginUri); |
| 66 | + helper.sendAndReceive(initLoginMsg, true); |
| 67 | + print( |
| 68 | + "Received response status code: " + |
| 69 | + initLoginMsg.getResponseHeader().getStatusCode() |
| 70 | + ); |
| 71 | + AuthenticationHelper.addAuthMessageToHistory(initLoginMsg); |
| 72 | + |
| 73 | + // Step 2: Submit login credentials |
| 74 | + const actionUrl = getActionurl(initLoginMsg); |
| 75 | + const loginUri = new URI(actionUrl, false); |
| 76 | + const loginMsg = helper.prepareMessage(); |
| 77 | + const csrf_token = getCsrfToken(initLoginMsg); |
| 78 | + const requestBody = |
| 79 | + "identifier=" + |
| 80 | + encodeURIComponent(credentials.getParam("username")) + |
| 81 | + "&password=" + |
| 82 | + encodeURIComponent(credentials.getParam("password")) + |
| 83 | + "&method=password" + |
| 84 | + "&csrf_token=" + |
| 85 | + encodeURIComponent(csrf_token); |
| 86 | + loginMsg.setRequestBody(requestBody); |
| 87 | + const requestHeader = new HttpRequestHeader( |
| 88 | + HttpRequestHeader.POST, |
| 89 | + loginUri, |
| 90 | + HttpHeader.HTTP11 |
| 91 | + ); |
| 92 | + loginMsg.setRequestHeader(requestHeader); |
| 93 | + |
| 94 | + loginMsg |
| 95 | + .getRequestHeader() |
| 96 | + .setHeader(HttpHeader.CONTENT_TYPE, "application/x-www-form-urlencoded"); |
| 97 | + loginMsg |
| 98 | + .getRequestHeader() |
| 99 | + .setContentLength(loginMsg.getRequestBody().length()); |
| 100 | + |
| 101 | + print("Sending POST request to " + loginUri); |
| 102 | + //! disable redirect to get the cookies from Kratos |
| 103 | + helper.sendAndReceive(loginMsg, false); |
| 104 | + print( |
| 105 | + "Received response status code: " + |
| 106 | + loginMsg.getResponseHeader().getStatusCode() |
| 107 | + ); |
| 108 | + AuthenticationHelper.addAuthMessageToHistory(loginMsg); |
| 109 | + |
| 110 | + return loginMsg; |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * |
| 115 | + * @param {*} request |
| 116 | + * @returns |
| 117 | + */ |
| 118 | +function getActionurl(request) { |
| 119 | + let iterator; |
| 120 | + let element; |
| 121 | + let actionUrl; |
| 122 | + const pageSource = |
| 123 | + request.getResponseHeader().toString() + |
| 124 | + request.getResponseBody().toString(); |
| 125 | + const src = new Source(pageSource); |
| 126 | + const elements = src.getAllElements("form"); |
| 127 | + |
| 128 | + for (iterator = elements.iterator(); iterator.hasNext(); ) { |
| 129 | + element = iterator.next(); |
| 130 | + actionUrl = element.getAttributeValue("action"); |
| 131 | + break; |
| 132 | + } |
| 133 | + |
| 134 | + return actionUrl; |
| 135 | +} |
| 136 | + |
| 137 | +function getCsrfToken(request) { |
| 138 | + let iterator; |
| 139 | + let element; |
| 140 | + let loginToken; |
| 141 | + const pageSource = |
| 142 | + request.getResponseHeader().toString() + |
| 143 | + request.getResponseBody().toString(); |
| 144 | + const src = new Source(pageSource); |
| 145 | + const elements = src.getAllElements("input"); |
| 146 | + |
| 147 | + for (iterator = elements.iterator(); iterator.hasNext(); ) { |
| 148 | + element = iterator.next(); |
| 149 | + if (element.getAttributeValue("name") == "csrf_token") { |
| 150 | + loginToken = element.getAttributeValue("value"); |
| 151 | + break; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return loginToken; |
| 156 | +} |
| 157 | +/** |
| 158 | + * Returns the required parameter names. |
| 159 | + * |
| 160 | + * @returns {Array<string>} An array of required parameter names. |
| 161 | + */ |
| 162 | +function getRequiredParamsNames() { |
| 163 | + return ["Kratos Base URL"]; |
| 164 | +} |
| 165 | + |
| 166 | +/** |
| 167 | + * Returns the optional parameter names. |
| 168 | + * |
| 169 | + * @returns {Array<string>} An array of optional parameter names. |
| 170 | + */ |
| 171 | +function getOptionalParamsNames() { |
| 172 | + return []; |
| 173 | +} |
| 174 | + |
| 175 | +/** |
| 176 | + * Returns the credentials parameter names. |
| 177 | + * |
| 178 | + * @returns {Array<string>} An array of credentials parameter names. |
| 179 | + */ |
| 180 | +function getCredentialsParamsNames() { |
| 181 | + return ["username", "password"]; |
| 182 | +} |
0 commit comments