Skip to content
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

Fix http-x-api-key #330

Merged
merged 1 commit into from
Mar 27, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@

package software.amazon.smithy.openapi.fromsmithy.security;

import java.util.Set;
import software.amazon.smithy.openapi.fromsmithy.Context;
import software.amazon.smithy.openapi.fromsmithy.SecuritySchemeConverter;
import software.amazon.smithy.openapi.model.SecurityScheme;
import software.amazon.smithy.utils.SetUtils;

/**
* Uses an HTTP header named X-Api-Key that contains an API key.
Expand All @@ -37,8 +39,13 @@ public SecurityScheme createSecurityScheme(Context context) {
return SecurityScheme.builder()
.type("apiKey")
.in("header")
.name("X-Api-Key")
.name("x-api-key")
.description("X-Api-Key authentication")
.build();
}

@Override
public Set<String> getAuthRequestHeaders() {
return SetUtils.of("x-api-key");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package software.amazon.smithy.openapi.fromsmithy.security;

import org.junit.jupiter.api.Test;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.openapi.fromsmithy.OpenApiConverter;
import software.amazon.smithy.openapi.model.OpenApi;
import software.amazon.smithy.utils.IoUtils;

public class XApiKeyTest {
@Test
public void addsXApiKey() {
Model model = Model.assembler()
.addImport(getClass().getResource("http-api-key-security.json"))
.assemble()
.unwrap();
OpenApi result = OpenApiConverter.create().convert(model, ShapeId.from("smithy.example#Service"));
Node expectedNode = Node.parse(IoUtils.toUtf8String(
getClass().getResourceAsStream("http-api-key-security.openapi.json")));

Node.assertEquals(result, expectedNode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"smithy": "0.5.0",
"shapes": {
"smithy.example#Service": {
"type": "service",
"version": "2006-03-01",
"operations": [
{
"target": "smithy.example#Operation1"
}
],
"traits": {
"smithy.api#protocols": [
{
"name": "aws.rest-json",
"auth": [
"http-x-api-key"
]
}
]
}
},
"smithy.example#Operation1": {
"type": "operation",
"traits": {
"smithy.api#http": {
"uri": "/",
"method": "GET"
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"openapi": "3.0.2",
"info": {
"title": "Service",
"version": "2006-03-01"
},
"paths": {
"/": {
"get": {
"operationId": "Operation1",
"responses": {
"200": {
"description": "Operation1 response"
}
}
}
}
},
"components": {
"securitySchemes": {
"http-x-api-key": {
"type": "apiKey",
"description": "X-Api-Key authentication",
"name": "x-api-key",
"in": "header"
}
}
},
"security": [
{
"http-x-api-key": []
}
]
}