-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added scheme property to HttpApiKeyAuth trait (#893)
Add scheme property to HttpApiKeyAuth trait
- Loading branch information
1 parent
d404932
commit 7742723
Showing
12 changed files
with
245 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...java/software/amazon/smithy/model/validation/validators/HttpApiKeyAuthTraitValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 software.amazon.smithy.model.validation.validators; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Set; | ||
import software.amazon.smithy.model.Model; | ||
import software.amazon.smithy.model.shapes.ServiceShape; | ||
import software.amazon.smithy.model.traits.HttpApiKeyAuthTrait; | ||
import software.amazon.smithy.model.validation.AbstractValidator; | ||
import software.amazon.smithy.model.validation.ValidationEvent; | ||
|
||
/** | ||
* Validates that if an HttpApiKeyAuth trait's scheme field is present then | ||
* the 'in' field must specify "header". Scheme should only be used with the | ||
* "Authorization" http header. | ||
*/ | ||
public final class HttpApiKeyAuthTraitValidator extends AbstractValidator { | ||
@Override | ||
public List<ValidationEvent> validate(Model model) { | ||
Set<ServiceShape> serviceShapesWithTrait = model.getServiceShapesWithTrait(HttpApiKeyAuthTrait.class); | ||
List<ValidationEvent> events = new ArrayList<>(); | ||
|
||
for (ServiceShape serviceShape : serviceShapesWithTrait) { | ||
HttpApiKeyAuthTrait trait = serviceShape.expectTrait(HttpApiKeyAuthTrait.class); | ||
trait.getScheme().ifPresent(scheme -> { | ||
if (trait.getIn() != HttpApiKeyAuthTrait.Location.HEADER) { | ||
events.add(error(serviceShape, trait, | ||
String.format("The httpApiKeyAuth trait must have an `in` value of `header` when a `scheme`" | ||
+ " is provided, found: %s", trait.getIn()))); | ||
} | ||
}); | ||
} | ||
|
||
return events; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...amazon/smithy/model/errorfiles/validators/http-api-key-scheme-trait-validator-test.errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[ERROR] ns.foo#MyService: The httpApiKeyAuth trait must have an `in` value of `header` when a `scheme` is provided, found: query | HttpApiKeyAuthTrait |
39 changes: 39 additions & 0 deletions
39
...e/amazon/smithy/model/errorfiles/validators/http-api-key-scheme-trait-validator-test.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{ | ||
"smithy": "1.0", | ||
"shapes": { | ||
"ns.foo#MyService": { | ||
"type": "service", | ||
"version": "2017-01-17", | ||
"operations": [ | ||
{ | ||
"target": "ns.foo#A" | ||
} | ||
], | ||
"traits": { | ||
"smithy.api#httpApiKeyAuth": { | ||
"scheme": "Baz", | ||
"name": "ApiKeyName", | ||
"in": "query" | ||
} | ||
} | ||
}, | ||
"ns.foo#A": { | ||
"type": "operation", | ||
"input": { | ||
"target": "ns.foo#AInput" | ||
}, | ||
"output": { | ||
"target": "ns.foo#AOutput" | ||
}, | ||
"traits": { | ||
"smithy.api#readonly": { } | ||
} | ||
}, | ||
"ns.foo#AInput": { | ||
"type": "structure" | ||
}, | ||
"ns.foo#AOutput": { | ||
"type": "structure" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...rces/software/amazon/smithy/openapi/fromsmithy/security/http-api-key-bearer-security.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"smithy": "1.0", | ||
"shapes": { | ||
"smithy.example#Service": { | ||
"type": "service", | ||
"version": "2006-03-01", | ||
"operations": [ | ||
{ | ||
"target": "smithy.example#Operation" | ||
} | ||
], | ||
"traits": { | ||
"aws.protocols#restJson1": {}, | ||
"smithy.api#httpApiKeyAuth": { | ||
"name": "Authorization", | ||
"in": "header", | ||
"scheme": "ApiKey" | ||
} | ||
} | ||
}, | ||
"smithy.example#Operation": { | ||
"type": "operation", | ||
"traits": { | ||
"smithy.api#http": { | ||
"uri": "/", | ||
"method": "GET" | ||
} | ||
} | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...tware/amazon/smithy/openapi/fromsmithy/security/http-api-key-bearer-security.openapi.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"openapi": "3.0.2", | ||
"info": { | ||
"title": "Service", | ||
"version": "2006-03-01" | ||
}, | ||
"paths": { | ||
"/": { | ||
"get": { | ||
"operationId": "Operation", | ||
"responses": { | ||
"200": { | ||
"description": "Operation response" | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"components": { | ||
"securitySchemes": { | ||
"smithy.api.httpApiKeyAuth": { | ||
"type": "http", | ||
"description": "ApiKey authentication semantics via 'Authorization' header", | ||
"name": "Authorization", | ||
"in": "header", | ||
"scheme": "ApiKey" | ||
} | ||
} | ||
}, | ||
"security": [ | ||
{ | ||
"smithy.api.httpApiKeyAuth": [ ] | ||
} | ||
] | ||
} |