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 f2c-ci-robot[bot]
parent 70b804efcf
commit 2817c1d7ca
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.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import io.metersphere.api.dto.ApiTestImportRequest; 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.sampler.MsHTTPSamplerProxy;
import io.metersphere.api.dto.definition.request.variable.JsonSchemaItem; import io.metersphere.api.dto.definition.request.variable.JsonSchemaItem;
import io.metersphere.api.dto.definition.response.HttpResponse; import io.metersphere.api.dto.definition.response.HttpResponse;
@ -72,7 +73,7 @@ public class Swagger2Parser extends SwaggerAbstractParser {
if (request.getAuthManager() != null if (request.getAuthManager() != null
&& StringUtils.isNotBlank(request.getAuthManager().getUsername()) && StringUtils.isNotBlank(request.getAuthManager().getUsername())
&& StringUtils.isNotBlank(request.getAuthManager().getPassword()) && StringUtils.isNotBlank(request.getAuthManager().getPassword())
&& request.getAuthManager().getVerification().equals("Basic Auth")) { && MsAuthManager.mechanismMap.containsKey(request.getAuthManager().getVerification())) {
AuthorizationValue authorizationValue = new AuthorizationValue(); AuthorizationValue authorizationValue = new AuthorizationValue();
authorizationValue.setType("header"); authorizationValue.setType("header");
authorizationValue.setKeyName("Authorization"); 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.SwaggerApiInfo;
import io.metersphere.api.dto.definition.parse.swagger.SwaggerInfo; import io.metersphere.api.dto.definition.parse.swagger.SwaggerInfo;
import io.metersphere.api.dto.definition.parse.swagger.SwaggerParams; 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.sampler.MsHTTPSamplerProxy;
import io.metersphere.api.dto.definition.request.variable.JsonSchemaItem; import io.metersphere.api.dto.definition.request.variable.JsonSchemaItem;
import io.metersphere.api.dto.definition.response.HttpResponse; import io.metersphere.api.dto.definition.response.HttpResponse;
@ -80,7 +81,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
if (request.getAuthManager() != null if (request.getAuthManager() != null
&& StringUtils.isNotBlank(request.getAuthManager().getUsername()) && StringUtils.isNotBlank(request.getAuthManager().getUsername())
&& StringUtils.isNotBlank(request.getAuthManager().getPassword()) && StringUtils.isNotBlank(request.getAuthManager().getPassword())
&& request.getAuthManager().getVerification().equals("Basic Auth")) { && MsAuthManager.mechanismMap.containsKey(request.getAuthManager().getVerification())) {
AuthorizationValue authorizationValue = new AuthorizationValue(); AuthorizationValue authorizationValue = new AuthorizationValue();
authorizationValue.setType("header"); authorizationValue.setType("header");
authorizationValue.setKeyName("Authorization"); authorizationValue.setKeyName("Authorization");

View File

@ -16,7 +16,9 @@ import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement; import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.HashTree;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
@Data @Data
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
@ -52,18 +54,19 @@ public class MsAuthManager extends MsTestElement {
@JSONField(ordinal = 28) @JSONField(ordinal = 28)
private String environment; 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 @Override
public void toHashTree(HashTree tree, List<MsTestElement> hashTree, MsParameter msParameter) { public void toHashTree(HashTree tree, List<MsTestElement> hashTree, MsParameter msParameter) {
if (!this.isEnable()) { if (!this.isEnable()) {
return; return;
} }
if (StringUtils.equals(this.getVerification(), "Basic Auth")) { if (mechanismMap.containsKey(this.getVerification())) {
ParameterConfig config = (ParameterConfig) msParameter; ParameterConfig config = (ParameterConfig) msParameter;
AuthManager authManager = new AuthManager(); AuthManager authManager = initBase();
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"));
Authorization auth = new Authorization(); Authorization auth = new Authorization();
if (this.url != null) { if (this.url != null) {
auth.setURL(this.url); auth.setURL(this.url);
@ -77,7 +80,7 @@ public class MsAuthManager extends MsTestElement {
} }
auth.setUser(this.username); auth.setUser(this.username);
auth.setPass(this.password); auth.setPass(this.password);
auth.setMechanism(AuthManager.Mechanism.DIGEST); auth.setMechanism(mechanismMap.get(this.getVerification()));
authManager.addAuth(auth); authManager.addAuth(auth);
tree.add(authManager); tree.add(authManager);
} }
@ -85,20 +88,25 @@ public class MsAuthManager extends MsTestElement {
public void setAuth(HashTree tree, MsAuthManager msAuthManager, HTTPSamplerProxy samplerProxy) { public void setAuth(HashTree tree, MsAuthManager msAuthManager, HTTPSamplerProxy samplerProxy) {
try { try {
AuthManager authManager = new AuthManager(); AuthManager authManager = initBase();
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"));
Authorization auth = new Authorization(); Authorization auth = new Authorization();
auth.setURL(samplerProxy.getProtocol() + "://" + samplerProxy.getDomain()); auth.setURL(samplerProxy.getProtocol() + "://" + samplerProxy.getDomain());
auth.setUser(msAuthManager.getUsername()); auth.setUser(msAuthManager.getUsername());
auth.setPass(msAuthManager.getPassword()); auth.setPass(msAuthManager.getPassword());
auth.setMechanism(AuthManager.Mechanism.DIGEST); auth.setMechanism(mechanismMap.get(msAuthManager.getVerification()));
authManager.addAuth(auth); authManager.addAuth(auth);
tree.add(authManager); tree.add(authManager);
} catch (Exception e) { } catch (Exception e) {
LogUtil.error(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.jmeter.utils.ScriptEngineUtils;
import io.metersphere.plugin.core.MsParameter; import io.metersphere.plugin.core.MsParameter;
import io.metersphere.plugin.core.MsTestElement; import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.utils.LoggerUtil;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import org.apache.commons.collections.CollectionUtils; 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); 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); this.authManager.setAuth(httpSamplerTree, this.authManager, sampler);
} }

View File

@ -79,7 +79,7 @@ export default {
}, },
data() { data() {
return { 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')}], encryptOptions: [{id: false, name: this.$t('commons.encrypted')}],
activeName: "verified", activeName: "verified",
rule: {}, rule: {},
@ -88,9 +88,9 @@ export default {
}, },
methods: { methods: {
change() { change() {
if (this.authConfig.verification === "Basic Auth") { if (this.authConfig.verification === "Basic Auth" || this.authConfig.verification === "Digest Auth") {
let authManager = createComponent("AuthManager"); let authManager = createComponent("AuthManager");
authManager.verification = "Basic Auth"; authManager.verification = this.authConfig.verification;
authManager.environment = this.request.useEnvironment; authManager.environment = this.request.useEnvironment;
if (this.request.hashTree == undefined) { if (this.request.hashTree == undefined) {
this.request.hashTree = []; this.request.hashTree = [];