Skip to content

[k6] Add authentication variables in headers and cookies #19060

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
merged 6 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/configs/k6.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
generatorName: k6
outputDir: samples/client/petstore/k6
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml
inputSpec: modules/openapi-generator/src/test/resources/3_0/k6/petstore-with-fake-endpoints-models-for-testing.yaml
templateDir: modules/openapi-generator/src/main/resources/k6
additionalProperties:
appName: PetstoreClient
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.RequestBody;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;

public class K6ClientCodegen extends DefaultCodegen implements CodegenConfig {
Expand Down Expand Up @@ -300,6 +302,7 @@ static class HTTPRequest {
@Nullable
HTTPBody body;
boolean hasBodyExample;
boolean hasCookie;
@Nullable
HTTPParameters params;
@Nullable
Expand All @@ -308,7 +311,7 @@ static class HTTPRequest {
DataExtractSubstituteParameter dataExtract;

public HTTPRequest(String operationId, String method, String path, @Nullable List<Parameter> query, @Nullable HTTPBody body,
boolean hasBodyExample, @Nullable HTTPParameters params, @Nullable List<k6Check> k6Checks,
boolean hasBodyExample, boolean hasCookie, @Nullable HTTPParameters params, @Nullable List<k6Check> k6Checks,
DataExtractSubstituteParameter dataExtract) {
// NOTE: https://k6.io/docs/javascript-api/k6-http/del-url-body-params
this.method = method.equals("delete") ? "del" : method;
Expand All @@ -318,6 +321,7 @@ public HTTPRequest(String operationId, String method, String path, @Nullable Lis
this.query = query;
this.body = body;
this.hasBodyExample = hasBodyExample;
this.hasCookie = hasCookie;
this.params = params;
this.k6Checks = k6Checks;
this.dataExtract = dataExtract;
Expand Down Expand Up @@ -494,6 +498,29 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
Set<Parameter> extraParameters = new HashSet<>();
Map<String, Set<Parameter>> pathVariables = new HashMap<>();

// get security schema from components
Map<String, SecurityScheme> securitySchemeMap = openAPI != null ?
(openAPI.getComponents() != null ? openAPI.getComponents().getSecuritySchemes() : null) : null;

// get global security requirements
List<SecurityRequirement> securityRequirements = openAPI.getSecurity();

// match global security requirements with security schemes and transform them to global auth methods
// global auth methods is a list of auth methods that are used in all requests
List<CodegenSecurity> globalAuthMethods = new ArrayList<>();
Map<String, SecurityScheme> globalSecurityMap = new HashMap<>();
if (securityRequirements != null) {
for (SecurityRequirement securityRequirement : securityRequirements) {
for (String securityRequirementKey : securityRequirement.keySet()) {
SecurityScheme securityScheme = securitySchemeMap.get(securityRequirementKey);
if (securityScheme != null) {
globalSecurityMap.put(securityRequirementKey, securityScheme);
}
}
}
globalAuthMethods = fromSecurity(globalSecurityMap);
}

for (String path : openAPI.getPaths().keySet()) {
Map<Integer, HTTPRequest> requests = new HashMap<>();
Set<Parameter> variables = new HashSet<>();
Expand All @@ -503,6 +530,7 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
for (Map.Entry<PathItem.HttpMethod, Operation> methodOperation : openAPI.getPaths().get(path).
readOperationsMap().entrySet()) {
List<Parameter> httpParams = new ArrayList<>();
List<Parameter> cookieParams = new ArrayList<>();
List<Parameter> queryParams = new ArrayList<>();
List<Parameter> bodyOrFormParams = new ArrayList<>();
List<k6Check> k6Checks = new ArrayList<>();
Expand Down Expand Up @@ -638,7 +666,19 @@ public void preprocessOpenAPI(OpenAPI openAPI) {

pathVariables.put(groupName, variables);

final HTTPParameters params = new HTTPParameters(null, null, httpParams, null, null, null, null, null,
// put auth medthods in header or cookie
for (CodegenSecurity globalAuthMethod : globalAuthMethods) {
if (globalAuthMethod.isKeyInHeader) {
httpParams.add(new Parameter(globalAuthMethod.keyParamName, getTemplateString(toVarName(globalAuthMethod.keyParamName))));
extraParameters.add(new Parameter(toVarName(globalAuthMethod.keyParamName), globalAuthMethod.keyParamName.toUpperCase(Locale.ROOT)));
}
if (globalAuthMethod.isKeyInCookie) {
cookieParams.add(new Parameter(globalAuthMethod.keyParamName, getTemplateString(toVarName(globalAuthMethod.keyParamName))));
extraParameters.add(new Parameter(toVarName(globalAuthMethod.keyParamName), globalAuthMethod.keyParamName.toUpperCase(Locale.ROOT)));
}
Comment on lines +671 to +678
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@willie5588912 Is it either/or? Or can it be both? 🤔

Copy link
Contributor Author

@willie5588912 willie5588912 Jul 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it can be in both. In some cases, we may need authentication token both in headers and cookies, and this depends on the requirement from the server side.
As the implementation here, it gives the flexibility for authentication token in headers and cookies since they are independent from each other and both ok if provided or not provided.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mostafa thanks for reviewing. I saw the thumb up so I'll merge it to get the ball rolling.

}

final HTTPParameters params = new HTTPParameters(null, cookieParams, httpParams, null, null, null, null, null,
responseType.length() > 0 ? responseType : null);

assert params.headers != null;
Expand All @@ -650,11 +690,18 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
// calculate order for this current request
Integer requestOrder = calculateRequestOrder(operationGroupingOrder, requests.size());

requests.put(requestOrder, new HTTPRequest(operationId, method.toString().toLowerCase(Locale.ROOT), path,
queryParams.size() > 0 ? queryParams : null,
bodyOrFormParams.size() > 0 ? new HTTPBody(bodyOrFormParams) : null, hasRequestBodyExample,
params.headers.size() > 0 ? params : null, k6Checks.size() > 0 ? k6Checks : null,
dataExtract.orElse(null)));
requests.put(requestOrder, new HTTPRequest(
operationId,
method.toString().toLowerCase(Locale.ROOT),
path,
queryParams.size() > 0 ? queryParams : null,
bodyOrFormParams.size() > 0 ? new HTTPBody(bodyOrFormParams) : null,
hasRequestBodyExample,
params.cookies.size() > 0 ? true : false,
params.headers.size() > 0 ? params : null,
k6Checks.size() > 0 ? k6Checks : null,
dataExtract.orElse(null))
);
}

addOrUpdateRequestGroup(requestGroups, groupName, pathVariables.get(groupName), requests);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ export default function() {
let body = {{#body}}{{=<% %>=}}{<%#parameters%>"<%& key%>": <%& value%><%^-last%>, <%/-last%><%/parameters%>}<%={{ }}=%>{{/body}};
{{/body}}
{{#params}}
let params = {{#params}}{{=<% %>=}}{headers: {<%# headers%>"<%& key%>": <%& value%><%^-last%>, <%/-last%><%/headers%><%#responseType%>, "Accept": <%& responseType%><%/responseType%>}<%# auth%>, auth: "<%& auth%>"<%/auth%>}<%={{ }}=%>{{/params}};
let params = {{#params}}{{=<% %>=}}{
headers: {
<%# headers%>"<%& key%>": <%& value%><%^-last%>, <%/-last%><%/headers%><%#responseType%>, "Accept": <%& responseType%><%/responseType%>
}<%#hasCookie%>, cookies: {
<%# cookies%>"<%& key%>": <%& value%><%^-last%>, <%/-last%><%/cookies%>
}<%/hasCookie%><%# auth%>, auth: "<%& auth%>"<%/auth%>
}<%={{ }}=%>{{/params}};
{{/params}}
{{#isDelete}}
{{#params}}
Expand Down
Loading
Loading