Skip to content

Commit 43d87b5

Browse files
committed
feat(*): support oauth2 security configuration
1 parent 3016cd1 commit 43d87b5

File tree

11 files changed

+195
-13
lines changed

11 files changed

+195
-13
lines changed

README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,54 @@ There are several other aspects you can optionally configure. Here is the full
7676
<skip>false</skip>
7777
<format>JSON</format>
7878
<filename>openapi-2.0</filename>
79+
<oauth2 />
7980
</configuration>
8081
</execution>
8182
</executions>
8283
</plugin>
8384
```
8485

85-
# Still in development
86+
### OAuth2 Configuration
87+
88+
By default the plugin will not generate any security information. You can optionally provide
89+
it using the `<oauth2 />` element. Here is an example:
90+
91+
```xml
92+
<plugin>
93+
<groupId>com.github.berkleytechnologyservices.restdocs-spec</groupId>
94+
<artifactId>restdocs-spec-maven-plugin</artifactId>
95+
<version>${restdocs-spec.version}</version>
96+
<executions>
97+
<execution>
98+
<goals>
99+
<goal>generate</goal>
100+
</goals>
101+
<configuration>
102+
<oauth>
103+
<tokenUrl>http://example.com/uaa/token</tokenUrl>
104+
<authorizationUrl>http://example.com/uaa/authorize</authorizationUrl>
105+
<flows>
106+
<flow>accessCode</flow>
107+
<flow>implicit</flow>
108+
</flows>
109+
<scopes>
110+
<scope>
111+
<name>read</name>
112+
<description>Access to read operations.</description>
113+
</scope>
114+
<scope>
115+
<name>write</name>
116+
<description>Access to write operations.</description>
117+
</scope>
118+
</scopes>
119+
</oauth>
120+
</configuration>
121+
</execution>
122+
</executions>
123+
</plugin>
124+
```
125+
126+
## Still in development
86127

87128
* Support for additional specification formats is currently in development. We plan to add
88129
support for OpenAPI 3.0 and Postman Collections.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<maven.compiler.target>1.8</maven.compiler.target>
3232
<maven.version>3.3.1</maven.version>
3333
<maven-plugin-tools.version>3.5.2</maven-plugin-tools.version>
34-
<restdocs-openapi.version>0.2.5</restdocs-openapi.version>
34+
<restdocs-openapi.version>0.2.8</restdocs-openapi.version>
3535
<junit.version>5.2.0</junit.version>
3636
<assertj.version>3.11.0</assertj.version>
3737
<mockito.version>2.21.0</mockito.version>

restdocs-spec-maven-plugin/src/it/default-settings/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<modelVersion>4.0.0</modelVersion>
55

6-
<groupId>com.berkleytechnologyservices.restdocs.it</groupId>
7-
<artifactId>simple-it</artifactId>
6+
<groupId>com.github.berkleytechnologyservices.restdocs-spec.it</groupId>
7+
<artifactId>default-settings-it</artifactId>
88
<version>1.0-SNAPSHOT</version>
99

10-
<description>A simple IT verifying the basic use case.</description>
10+
<description>Verify the default settings.</description>
1111

1212
<properties>
1313
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

restdocs-spec-maven-plugin/src/it/json-format/pom.xml

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
<modelVersion>4.0.0</modelVersion>
55

66
<groupId>com.berkleytechnologyservices.restdocs.it</groupId>
7-
<artifactId>simple-it</artifactId>
7+
<artifactId>json-format-it</artifactId>
88
<version>1.0-SNAPSHOT</version>
99

10-
<description>A simple IT verifying the basic use case.</description>
10+
<description>Verify using json as the specification format.</description>
1111

1212
<properties>
1313
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -50,6 +50,24 @@
5050
<snippetsDirectory>${project.basedir}/snippets</snippetsDirectory>
5151
<filename>sample-api-spec</filename>
5252
<format>JSON</format>
53+
<oauth2>
54+
<tokenUrl>https://example.org/uaa/token</tokenUrl>
55+
<authorizationUrl>https://example.org/uaa/authorize</authorizationUrl>
56+
<flows>
57+
<flow>accessCode</flow>
58+
<flow>password</flow>
59+
</flows>
60+
<scopes>
61+
<scope>
62+
<name>write:cart</name>
63+
<description>Write cart to the database.</description>
64+
</scope>
65+
<scope>
66+
<name>read:cart</name>
67+
<description>Read cart from the database.</description>
68+
</scope>
69+
</scopes>
70+
</oauth2>
5371
</configuration>
5472
</execution>
5573
</executions>

restdocs-spec-maven-plugin/src/it/json-format/snippets/cart-get/resource.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@
1919
"requestParameters" : [ ],
2020
"requestFields" : [ ],
2121
"example" : null,
22-
"securityRequirements" : null
22+
"securityRequirements" : {
23+
"type": "OAUTH2",
24+
"requiredScopes": [
25+
"read:cart"
26+
]
27+
}
2328
},
2429
"response" : {
2530
"status" : 200,

restdocs-spec-maven-plugin/src/it/json-format/snippets/carts-create/resource.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313
"requestParameters" : [ ],
1414
"requestFields" : [ ],
1515
"example" : null,
16-
"securityRequirements" : null
16+
"securityRequirements" : {
17+
"type": "OAUTH2",
18+
"requiredScopes": [
19+
"write:cart"
20+
]
21+
}
1722
},
1823
"response" : {
1924
"status" : 201,

restdocs-spec-maven-plugin/src/main/java/com/berkleytechnologyservices/restdocs/mojo/GenerateMojo.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.berkleytechnologyservices.restdocs.mojo;
22

33
import com.berkleytechnologyservices.restdocs.spec.ApiDetails;
4+
import com.berkleytechnologyservices.restdocs.spec.AuthConfig;
45
import com.berkleytechnologyservices.restdocs.spec.Specification;
56
import com.berkleytechnologyservices.restdocs.spec.SpecificationFormat;
67
import com.berkleytechnologyservices.restdocs.spec.generator.SpecificationGeneratorException;
@@ -100,6 +101,9 @@ public class GenerateMojo extends AbstractMojo {
100101
@Parameter
101102
private List<SpecificationOptions> specifications = Collections.emptyList();
102103

104+
@Parameter
105+
private AuthConfig oauth2 = new AuthConfig();
106+
103107
private final SnippetReader snippetReader;
104108
private final SpecificationGeneratorFactory specificationGeneratorFactory;
105109

@@ -188,7 +192,8 @@ private ApiDetails createApiDetails(SpecificationOptions options) {
188192
.host(host)
189193
.basePath(basePath)
190194
.schemes(schemes)
191-
.format(options.getFormat());
195+
.format(options.getFormat())
196+
.authConfig(oauth2);
192197
}
193198

194199
private List<SpecificationOptions> getAllSpecificationOptions() {

restdocs-spec-maven-plugin/src/main/java/com/berkleytechnologyservices/restdocs/spec/ApiDetails.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class ApiDetails {
1717
private String basePath;
1818
private List<String> schemes;
1919
private SpecificationFormat format;
20+
private AuthConfig authConfig;
2021

2122
public ApiDetails() {
2223
this.name = DEFAULT_NAME;
@@ -25,6 +26,7 @@ public ApiDetails() {
2526
this.basePath = null;
2627
this.schemes = DEFAULT_SCHEMES;
2728
this.format = DEFAULT_FORMAT;
29+
this.authConfig = new AuthConfig();
2830
}
2931

3032
public String getName() {
@@ -80,4 +82,13 @@ public ApiDetails format(SpecificationFormat format) {
8082
this.format = format != null ? format : DEFAULT_FORMAT;
8183
return this;
8284
}
85+
86+
public AuthConfig getAuthConfig() {
87+
return this.authConfig;
88+
}
89+
90+
public ApiDetails authConfig(AuthConfig authConfig) {
91+
this.authConfig = authConfig != null ? authConfig : new AuthConfig();
92+
return this;
93+
}
8394
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.berkleytechnologyservices.restdocs.spec;
2+
3+
import java.util.Collections;
4+
import java.util.List;
5+
6+
public class AuthConfig {
7+
8+
private String tokenUrl;
9+
private String authorizationUrl;
10+
private List<String> flows;
11+
private List<Scope> scopes;
12+
13+
public AuthConfig() {
14+
this.flows = Collections.emptyList();
15+
this.scopes = Collections.emptyList();
16+
}
17+
18+
public String getTokenUrl() {
19+
return tokenUrl;
20+
}
21+
22+
public void setTokenUrl(String tokenUrl) {
23+
this.tokenUrl = tokenUrl;
24+
}
25+
26+
public String getAuthorizationUrl() {
27+
return authorizationUrl;
28+
}
29+
30+
public void setAuthorizationUrl(String authorizationUrl) {
31+
this.authorizationUrl = authorizationUrl;
32+
}
33+
34+
public List<String> getFlows() {
35+
return flows;
36+
}
37+
38+
public void setFlows(List<String> flows) {
39+
this.flows = flows != null ? Collections.unmodifiableList(flows) : Collections.emptyList();
40+
}
41+
42+
public List<Scope> getScopes() {
43+
return scopes;
44+
}
45+
46+
public void setScopes(List<Scope> scopes) {
47+
this.scopes = scopes != null ? Collections.unmodifiableList(scopes) : Collections.emptyList();
48+
}
49+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.berkleytechnologyservices.restdocs.spec;
2+
3+
public class Scope {
4+
5+
private String name;
6+
private String description;
7+
8+
public String getName() {
9+
return name;
10+
}
11+
12+
public void setName(String name) {
13+
this.name = name;
14+
}
15+
16+
public String getDescription() {
17+
return description;
18+
}
19+
20+
public void setDescription(String description) {
21+
this.description = description;
22+
}
23+
}

0 commit comments

Comments
 (0)