io.restassured.authentication.FormAuthConfig Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rest-assured Show documentation
Show all versions of rest-assured Show documentation
Java DSL for easy testing of REST services
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.restassured.authentication;
import io.restassured.config.LogConfig;
import io.restassured.filter.log.LogDetail;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static io.restassured.internal.common.assertion.AssertParameter.notNull;
/**
* Configuration of form authentication to correctly identify which form that contains the username and password
* and the action of the form.
*/
public class FormAuthConfig {
private final String formAction;
private final String userInputTagName;
private final String passwordInputTagName;
private final LogConfig logConfig;
private final LogDetail logDetail;
private final List additionalInputFieldNames;
/**
* Create a form auth config with a pre-defined form action, username input tag, password input tag.
* E.g. let's say that the login form on your login page looks like this:
*
* <form action="/j_spring_security_check">
* <label for="j_username">Username</label>
* <input type="text" name="j_username" id="j_username"/>
* <br/>
* <label for="j_password">Password</label>
* <input type="password" name="j_password" id="j_password"/>
* <br/>
* <input type='checkbox' name='_spring_security_remember_me'/> Remember me on this computer.
* <br/>
* <input type="submit" value="Login"/>
* </form>
*
*
* This means that formAction
should be set to /j_spring_security_check
, userNameInputTagName
* should be set to j_username
and passwordInputTagName
should be set to j_password
.
*
* @param formAction The action of the form
* @param userNameInputTagName The name of the username input tag in the login form
* @param passwordInputTagName The name of the password input tag in the login form
*/
public FormAuthConfig(String formAction, String userNameInputTagName, String passwordInputTagName) {
this(formAction, userNameInputTagName, passwordInputTagName, null, null, Collections.emptyList());
}
/**
* Creates a new empty {@link FormAuthConfig}.
*/
public FormAuthConfig() {
this(null, null, null);
}
private FormAuthConfig(String formAction, String userNameInputTagName, String passwordInputTagName, LogDetail logDetail, LogConfig logConfig,
List additionalInputFieldNames) {
this.formAction = formAction;
this.userInputTagName = userNameInputTagName;
this.passwordInputTagName = passwordInputTagName;
this.logDetail = logDetail;
this.logConfig = logConfig;
this.additionalInputFieldNames = Collections.unmodifiableList(additionalInputFieldNames);
}
/**
* @return A predefined form authentication config for default Spring Security configuration (tested in version 3.0.5).
*/
public static FormAuthConfig springSecurity() {
return new FormAuthConfig("/j_spring_security_check", "j_username", "j_password");
}
/**
* Include additional field when using form authentication by including input field value with the specified name.
* For example if the login page looks like this:
*
* <html>
* <head>
* <title>Login</title>
* </head>
* <body>
* <form action="j_spring_security_check_with_csrf" method="POST">
* <table>
* <tr>
* <td>User: </td>
* <td><input type="text" name="j_username"></td>
* </tr>
* <tr>
* <td>Password:</td>
* <td><input type="password" name="j_password"></td>
* </tr>
* <tr>
* <td colspan="2"><input name="submit" type="submit"/></td>
* </tr>
* </table>
* <input type="hidden" name="something" value="8adf2ea1-b246-40aa-8e13-a85fb7914341"/>
* </form>
* </body>
* </html>
*
* and you'd like to include the field named something
as an additional form parameter in the request you can do like this:
*
*
* given().auth().form(..., new FormAuthConfig(..).withAdditionalField("something"). ..
*
* and then REST Assured will send the form parameter something=8adf2ea1-b246-40aa-8e13-a85fb7914341
*
*
* Important: When including an additional field without specifying a value then REST Assured must always make an additional request to the server in order to
* be able to figure out the field value. This will slow down the tests.
*
* @param fieldName The first field name to include
* @return A new FormAuthConfig instance.
*/
public FormAuthConfig withAdditionalField(String fieldName) {
notNull(fieldName, "Additional field name");
List list = new ArrayList<>(additionalInputFieldNames);
list.add(fieldName);
return new FormAuthConfig(formAction, userInputTagName, passwordInputTagName, logDetail, logConfig, list);
}
/**
* Include multiple additional fields when using form authentication by including input field values with the specified name.
* This is the same as {@link #withAdditionalField(String)} but for multiple fields.
*
*
* Important: When including an additional field without specifying a value then REST Assured must always make an additional request to the server in order to
* be able to figure out the field value. This will slow down the tests.
*
* @param firstFieldName The first additional input field to include
* @param secondFieldName The second additional input field to include
* @param additionalFieldNames Additional field name to include (optional)
* @return A new FormAuthConfig instance.
*/
public FormAuthConfig withAdditionalFields(String firstFieldName, String secondFieldName, String... additionalFieldNames) {
notNull(firstFieldName, "First additional field name");
notNull(secondFieldName, "Second additional field name");
List list = new ArrayList<>(additionalInputFieldNames);
list.add(firstFieldName);
list.add(secondFieldName);
if (additionalFieldNames != null && additionalFieldNames.length > 0) {
list.addAll(Arrays.asList(additionalFieldNames));
}
return new FormAuthConfig(formAction, userInputTagName, passwordInputTagName, logDetail, logConfig, list);
}
/**
* Enables logging with log level {@link LogDetail#ALL} of the request made to authenticate using
* form authentication. Both the request and the response is logged.
*
* @return A new FormAuthConfig instance.
*/
public FormAuthConfig withLoggingEnabled() {
return withLoggingEnabled(LogDetail.ALL);
}
/**
* Enables logging with the supplied logDetail of the request made to authenticate using form authentication.
* Both the request and the response is logged.
*
* @return A new FormAuthConfig instance.
*/
public FormAuthConfig withLoggingEnabled(LogDetail logDetail) {
return withLoggingEnabled(logDetail, new LogConfig());
}
/**
* Enables logging with log level {@link LogDetail#ALL} of the request made to authenticate using
* form authentication using the specified {@link LogConfig}. Both the request and the response is logged.
*
* @return A new FormAuthConfig instance.
*/
public FormAuthConfig withLoggingEnabled(LogConfig logConfig) {
return withLoggingEnabled(LogDetail.ALL, logConfig);
}
/**
* Enables logging with the supplied log detail of the request made to authenticate using form authentication using the
* specified {@link LogConfig}. Both the request and the response is logged.
*
* @return A new FormAuthConfig instance.
*/
public FormAuthConfig withLoggingEnabled(LogDetail logDetail, LogConfig logConfig) {
notNull(logDetail, LogDetail.class);
notNull(logConfig, LogConfig.class);
return new FormAuthConfig(formAction, userInputTagName, passwordInputTagName, logDetail, logConfig, additionalInputFieldNames);
}
/**
* Creates a new empty {@link FormAuthConfig}.
*
* @return A new FormAuthConfig instance.
*/
public static FormAuthConfig formAuthConfig() {
return new FormAuthConfig(null, null, null);
}
/**
* Syntactic sugar
*
* @return The same FormAuthConfig instance
*/
public FormAuthConfig and() {
return this;
}
public String getFormAction() {
return formAction;
}
public String getUserInputTagName() {
return userInputTagName;
}
/**
* @return The password input tag name or null
if undefined
*/
public String getPasswordInputTagName() {
return passwordInputTagName;
}
/**
* @return The logging configuration
*/
public LogConfig getLogConfig() {
return logConfig;
}
/**
* @return true
if logging is enabled or false
otherwise.
*/
public boolean isLoggingEnabled() {
return logConfig != null && logDetail != null;
}
/**
* @return The specified log detail or null
if undefined
*/
public LogDetail getLogDetail() {
return logDetail;
}
/**
* @return The additional input field names
*/
public List getAdditionalInputFieldNames() {
return additionalInputFieldNames;
}
/**
* @return true
if additional input field name have been specified or false
otherwise.
*/
public boolean hasAdditionalInputFieldNames() {
return !additionalInputFieldNames.isEmpty();
}
/**
* @return true
if the user input tag name is defined or false
otherwise.
*/
public boolean hasUserInputTagName() {
return StringUtils.isNotBlank(userInputTagName);
}
/**
* @return true
if the password input tag name is defined or false
otherwise.
*/
public boolean hasPasswordInputTagName() {
return StringUtils.isNotBlank(passwordInputTagName);
}
/**
* @return true
if the form action is defined or false
otherwise.
*/
public boolean hasFormAction() {
return StringUtils.isNotBlank(formAction);
}
/**
* @return true
if the {@link FormAuthConfig} instance contains settings that require REST Assured to make a request to the server before applying form authentication, false
otherwise.
*/
public boolean requiresParsingOfLoginPage() {
return !hasFormAction() || !hasUserInputTagName() || !hasPasswordInputTagName() || hasAdditionalInputFieldNames();
}
}