feat(接口测试): 认证支持basic和digest两种方式

--story=1008766 --user=赵勇 [接口测试]github#15669接口测试,Basic Auth 认证没生效 https://www.tapd.cn/55049933/s/1199311
This commit is contained in:
fit2-zhao 2022-07-12 11:41:08 +08:00 committed by fit2-zhao
parent d5a57e2723
commit 2d92e1ea75
5 changed files with 58 additions and 49 deletions

View File

@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.metersphere.api.dto.ApiTestImportRequest;
import io.metersphere.api.dto.definition.request.auth.MsAuthManager;
import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
import io.metersphere.api.dto.definition.request.variable.JsonSchemaItem;
import io.metersphere.api.dto.definition.response.HttpResponse;
@ -72,7 +73,7 @@ public class Swagger2Parser extends SwaggerAbstractParser {
if (request.getAuthManager() != null
&& StringUtils.isNotBlank(request.getAuthManager().getUsername())
&& StringUtils.isNotBlank(request.getAuthManager().getPassword())
&& request.getAuthManager().getVerification().equals("Basic Auth")) {
&& MsAuthManager.mechanismMap.containsKey(request.getAuthManager().getVerification())) {
AuthorizationValue authorizationValue = new AuthorizationValue();
authorizationValue.setType("header");
authorizationValue.setKeyName("Authorization");

View File

@ -9,6 +9,7 @@ import io.metersphere.api.dto.definition.SwaggerApiExportResult;
import io.metersphere.api.dto.definition.parse.swagger.SwaggerApiInfo;
import io.metersphere.api.dto.definition.parse.swagger.SwaggerInfo;
import io.metersphere.api.dto.definition.parse.swagger.SwaggerParams;
import io.metersphere.api.dto.definition.request.auth.MsAuthManager;
import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
import io.metersphere.api.dto.definition.request.variable.JsonSchemaItem;
import io.metersphere.api.dto.definition.response.HttpResponse;
@ -80,7 +81,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
if (request.getAuthManager() != null
&& StringUtils.isNotBlank(request.getAuthManager().getUsername())
&& StringUtils.isNotBlank(request.getAuthManager().getPassword())
&& request.getAuthManager().getVerification().equals("Basic Auth")) {
&& MsAuthManager.mechanismMap.containsKey(request.getAuthManager().getVerification())) {
AuthorizationValue authorizationValue = new AuthorizationValue();
authorizationValue.setType("header");
authorizationValue.setKeyName("Authorization");

View File

@ -16,7 +16,9 @@ import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.collections.HashTree;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Data
@EqualsAndHashCode(callSuper = true)
@ -52,18 +54,19 @@ public class MsAuthManager extends MsTestElement {
@JSONField(ordinal = 28)
private String environment;
public static final Map<String, AuthManager.Mechanism> mechanismMap = new HashMap<>() {{
this.put("Basic Auth", AuthManager.Mechanism.BASIC);
this.put("Digest Auth", AuthManager.Mechanism.DIGEST);
}};
@Override
public void toHashTree(HashTree tree, List<MsTestElement> hashTree, MsParameter msParameter) {
if (!this.isEnable()) {
return;
}
if (StringUtils.equals(this.getVerification(), "Basic Auth")) {
if (mechanismMap.containsKey(this.getVerification())) {
ParameterConfig config = (ParameterConfig) msParameter;
AuthManager authManager = new AuthManager();
authManager.setEnabled(true);
authManager.setName(StringUtils.isNotEmpty(this.getName()) ? this.getName() : "AuthManager");
authManager.setProperty(TestElement.TEST_CLASS, AuthManager.class.getName());
authManager.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("AuthPanel"));
AuthManager authManager = initBase();
Authorization auth = new Authorization();
if (this.url != null) {
auth.setURL(this.url);
@ -77,7 +80,7 @@ public class MsAuthManager extends MsTestElement {
}
auth.setUser(this.username);
auth.setPass(this.password);
auth.setMechanism(AuthManager.Mechanism.DIGEST);
auth.setMechanism(mechanismMap.get(this.getVerification()));
authManager.addAuth(auth);
tree.add(authManager);
}
@ -85,20 +88,25 @@ public class MsAuthManager extends MsTestElement {
public void setAuth(HashTree tree, MsAuthManager msAuthManager, HTTPSamplerProxy samplerProxy) {
try {
AuthManager authManager = new AuthManager();
authManager.setEnabled(true);
authManager.setName(StringUtils.isNotEmpty(this.getName()) ? this.getName() : "AuthManager");
authManager.setProperty(TestElement.TEST_CLASS, AuthManager.class.getName());
authManager.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("AuthPanel"));
AuthManager authManager = initBase();
Authorization auth = new Authorization();
auth.setURL(samplerProxy.getProtocol() + "://" + samplerProxy.getDomain());
auth.setUser(msAuthManager.getUsername());
auth.setPass(msAuthManager.getPassword());
auth.setMechanism(AuthManager.Mechanism.DIGEST);
auth.setMechanism(mechanismMap.get(msAuthManager.getVerification()));
authManager.addAuth(auth);
tree.add(authManager);
} catch (Exception e) {
LogUtil.error(e);
}
}
private AuthManager initBase() {
AuthManager authManager = new AuthManager();
authManager.setEnabled(true);
authManager.setName(StringUtils.isNotEmpty(this.getName()) ? this.getName() : "AuthManager");
authManager.setProperty(TestElement.TEST_CLASS, AuthManager.class.getName());
authManager.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("AuthPanel"));
return authManager;
}
}

View File

@ -34,7 +34,6 @@ import io.metersphere.commons.utils.LogUtil;
import io.metersphere.jmeter.utils.ScriptEngineUtils;
import io.metersphere.plugin.core.MsParameter;
import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.utils.LoggerUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.collections.CollectionUtils;
@ -238,7 +237,7 @@ public class MsHTTPSamplerProxy extends MsTestElement {
MsDNSCacheManager.addEnvironmentDNS(httpSamplerTree, this.getName(), config.getConfig().get(this.getProjectId()), httpConfig);
}
if (this.authManager != null && StringUtils.equals(this.authManager.getVerification(), "Basic Auth")) {
if (this.authManager != null && MsAuthManager.mechanismMap.containsKey(this.authManager.getVerification())) {
this.authManager.setAuth(httpSamplerTree, this.authManager, sampler);
}
@ -665,28 +664,28 @@ public class MsHTTPSamplerProxy extends MsTestElement {
list.stream().
filter(KeyValue::isValid).
filter(KeyValue::isEnable).forEach(keyValue -> {
try {
String value = StringUtils.isNotEmpty(keyValue.getValue()) && keyValue.getValue().startsWith("@") ? ScriptEngineUtils.buildFunctionCallString(keyValue.getValue()) : keyValue.getValue();
HTTPArgument httpArgument = new HTTPArgument(keyValue.getName(), value);
if (keyValue.getValue() == null) {
httpArgument.setValue("");
}
httpArgument.setAlwaysEncoded(keyValue.isUrlEncode());
if (StringUtils.isNotBlank(keyValue.getContentType())) {
httpArgument.setContentType(keyValue.getContentType());
}
if (StringUtils.equalsIgnoreCase(this.method, "get")) {
if (StringUtils.isNotEmpty(httpArgument.getValue())) {
arguments.addArgument(httpArgument);
}
} else {
arguments.addArgument(httpArgument);
}
} catch (Exception e) {
}
try {
String value = StringUtils.isNotEmpty(keyValue.getValue()) && keyValue.getValue().startsWith("@") ? ScriptEngineUtils.buildFunctionCallString(keyValue.getValue()) : keyValue.getValue();
HTTPArgument httpArgument = new HTTPArgument(keyValue.getName(), value);
if (keyValue.getValue() == null) {
httpArgument.setValue("");
}
);
httpArgument.setAlwaysEncoded(keyValue.isUrlEncode());
if (StringUtils.isNotBlank(keyValue.getContentType())) {
httpArgument.setContentType(keyValue.getContentType());
}
if (StringUtils.equalsIgnoreCase(this.method, "get")) {
if (StringUtils.isNotEmpty(httpArgument.getValue())) {
arguments.addArgument(httpArgument);
}
} else {
arguments.addArgument(httpArgument);
}
} catch (Exception e) {
}
}
);
return arguments;
}

View File

@ -8,10 +8,10 @@
<el-select v-model="authConfig.verification" @change="change" :disabled="isReadOnly"
:placeholder="$t('api_test.definition.request.verification_method')" filterable size="small">
<el-option
v-for="item in options"
:key="item.name"
:label="item.name"
:value="item.name">
v-for="item in options"
:key="item.name"
:label="item.name"
:value="item.name">
</el-option>
</el-select>
@ -43,10 +43,10 @@
<el-select v-model="authConfig.encrypt" :disabled="isReadOnly"
:placeholder="$t('api_test.definition.request.verification_method')" filterable size="small">
<el-option
v-for="item in encryptOptions"
:key="item.id"
:label="item.name"
:value="item.id">
v-for="item in encryptOptions"
:key="item.id"
:label="item.name"
:value="item.id">
</el-option>
</el-select>
</el-form-item>
@ -79,7 +79,7 @@ export default {
},
data() {
return {
options: [{name: "No Auth"}, {name: "Basic Auth"}],
options: [{name: "No Auth"}, {name: "Basic Auth"}, {name: "Digest Auth"}],
encryptOptions: [{id: false, name: this.$t('commons.encrypted')}],
activeName: "verified",
rule: {},
@ -88,9 +88,9 @@ export default {
},
methods: {
change() {
if (this.authConfig.verification === "Basic Auth") {
if (this.authConfig.verification === "Basic Auth" || this.authConfig.verification === "Digest Auth") {
let authManager = createComponent("AuthManager");
authManager.verification = "Basic Auth";
authManager.verification = this.authConfig.verification;
authManager.environment = this.request.useEnvironment;
if (this.request.hashTree == undefined) {
this.request.hashTree = [];