Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for the new security schemes in OAS
(2022)
- Loading branch information
1 parent
42236c4
commit ee0ddcc
Showing
7 changed files
with
363 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
springfox-core/src/main/java/springfox/documentation/builders/HttpAuthenticationBuilder.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,52 @@ | ||
package springfox.documentation.builders; | ||
|
||
import springfox.documentation.service.HttpAuthenticationScheme; | ||
import springfox.documentation.service.VendorExtension; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static springfox.documentation.builders.BuilderDefaults.*; | ||
|
||
public class HttpAuthenticationBuilder { | ||
private String name; | ||
private String description; | ||
private String scheme; | ||
private String bearerFormat; | ||
private final List<VendorExtension> extensions = new ArrayList<>(); | ||
|
||
public HttpAuthenticationBuilder name(String name) { | ||
this.name = name; | ||
return this; | ||
} | ||
|
||
public HttpAuthenticationBuilder scheme(String scheme) { | ||
this.scheme = scheme; | ||
return this; | ||
} | ||
|
||
public HttpAuthenticationBuilder bearerFormat(String bearerFormat) { | ||
this.bearerFormat = bearerFormat; | ||
return this; | ||
} | ||
|
||
public HttpAuthenticationBuilder description(String description) { | ||
this.description = description; | ||
return this; | ||
} | ||
|
||
public HttpAuthenticationBuilder extensions(List<VendorExtension> extensions) { | ||
this.extensions.addAll(nullToEmptyList(extensions)); | ||
return this; | ||
} | ||
|
||
public HttpAuthenticationScheme build() { | ||
return new HttpAuthenticationScheme( | ||
name, | ||
description, | ||
"http", | ||
scheme, | ||
bearerFormat, | ||
extensions); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
springfox-core/src/main/java/springfox/documentation/builders/OAuth2SchemeBuilder.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,85 @@ | ||
package springfox.documentation.builders; | ||
|
||
import springfox.documentation.service.AuthorizationScope; | ||
import springfox.documentation.service.OAuth2Scheme; | ||
import springfox.documentation.service.VendorExtension; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static springfox.documentation.builders.BuilderDefaults.*; | ||
import static springfox.documentation.builders.NoopValidator.*; | ||
|
||
@SuppressWarnings("VisibilityModifier") | ||
public class OAuth2SchemeBuilder { | ||
//accessible Validator | ||
String name; | ||
String flowType; | ||
String description; | ||
String authorizationUrl; | ||
String tokenUrl; | ||
String refreshUrl; | ||
final List<AuthorizationScope> scopes = new ArrayList<>(); | ||
private final List<VendorExtension> extensions = new ArrayList<>(); | ||
private Validator<OAuth2SchemeBuilder> validator = new OAuth2SchemeValidator<>(); | ||
|
||
public OAuth2SchemeBuilder(String flowType) { | ||
this.flowType = flowType; | ||
} | ||
|
||
public OAuth2SchemeBuilder name(String name) { | ||
this.name = name; | ||
return this; | ||
} | ||
|
||
public OAuth2SchemeBuilder description(String description) { | ||
this.description = description; | ||
return this; | ||
} | ||
|
||
public OAuth2SchemeBuilder authorizationUrl(String authorizationUrl) { | ||
this.authorizationUrl = authorizationUrl; | ||
return this; | ||
} | ||
|
||
public OAuth2SchemeBuilder tokenUrl(String tokenUrl) { | ||
this.tokenUrl = tokenUrl; | ||
return this; | ||
} | ||
|
||
public OAuth2SchemeBuilder refreshUrl(String refreshUrl) { | ||
this.refreshUrl = refreshUrl; | ||
return this; | ||
} | ||
|
||
public OAuth2SchemeBuilder scopes(List<AuthorizationScope> scopes) { | ||
this.scopes.addAll(nullToEmptyList(scopes)); | ||
return this; | ||
} | ||
|
||
public OAuth2SchemeBuilder extensions(List<VendorExtension> extensions) { | ||
this.extensions.addAll(nullToEmptyList(extensions)); | ||
return this; | ||
} | ||
|
||
public OAuth2SchemeBuilder validator(Validator<OAuth2SchemeBuilder> validator) { | ||
this.validator = validator; | ||
return this; | ||
} | ||
|
||
public OAuth2Scheme build() { | ||
List<ValidationResult> results = validator.validate(this); | ||
if (logProblems(results).size() > 0) { | ||
return null; | ||
} | ||
return new OAuth2Scheme( | ||
name, | ||
flowType, | ||
description, | ||
authorizationUrl, | ||
tokenUrl, | ||
refreshUrl, | ||
scopes, | ||
extensions); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
springfox-core/src/main/java/springfox/documentation/builders/OAuth2SchemeValidator.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,63 @@ | ||
package springfox.documentation.builders; | ||
|
||
import org.springframework.util.StringUtils; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class OAuth2SchemeValidator<T> implements Validator<OAuth2SchemeBuilder> { | ||
@Override | ||
public List<ValidationResult> validate(OAuth2SchemeBuilder builder) { | ||
List<ValidationResult> results = new ArrayList<>(); | ||
if (builder.name == null) { | ||
results.add( | ||
new ValidationResult( | ||
"OAuth2Scheme", | ||
"name", | ||
"Parameter name is required")); | ||
} | ||
if (builder.flowType == null) { | ||
results.add(new ValidationResult( | ||
"OAuth2Scheme", | ||
"flowType", | ||
"Flow type is required")); | ||
} | ||
switch (builder.flowType) { | ||
case "implicit": | ||
requiredAttribute(results, "authorizationUrl", builder.authorizationUrl); | ||
break; | ||
case "password": | ||
case "clientCredentials": | ||
requiredAttribute(results, "tokenUrl", builder.tokenUrl); | ||
break; | ||
case "authorizationCode": | ||
requiredAttribute(results, "authorizationUrl", builder.authorizationUrl); | ||
requiredAttribute(results, "tokenUrl", builder.tokenUrl); | ||
break; | ||
default: | ||
results.add(new ValidationResult( | ||
"OAuth2Scheme", | ||
"flowType", | ||
"Flow type should be one of (implicit, password, clientCredentials, authorizationCode)")); | ||
} | ||
if (builder.scopes.isEmpty()) { | ||
results.add(new ValidationResult( | ||
"OAuth2Scheme", | ||
"scopes", | ||
"Scopes are required")); | ||
} | ||
return results; | ||
} | ||
|
||
private void requiredAttribute( | ||
List<ValidationResult> results, | ||
String name, | ||
String value) { | ||
if (StringUtils.isEmpty(value)) { | ||
results.add(new ValidationResult( | ||
"OAuth2Scheme", | ||
"name", | ||
String.format("Parameter %s is required", name))); | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...ngfox-core/src/main/java/springfox/documentation/builders/OpenIdConnectSchemeBuilder.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,46 @@ | ||
package springfox.documentation.builders; | ||
|
||
import springfox.documentation.service.OpenIdConnectScheme; | ||
import springfox.documentation.service.VendorExtension; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static springfox.documentation.builders.BuilderDefaults.*; | ||
|
||
public class OpenIdConnectSchemeBuilder { | ||
private String name; | ||
private String type; | ||
private String description; | ||
private final List<VendorExtension> extensions = new ArrayList<>(); | ||
private String openIdConnectUrl; | ||
|
||
public OpenIdConnectSchemeBuilder name(String name) { | ||
this.name = name; | ||
return this; | ||
} | ||
|
||
public OpenIdConnectSchemeBuilder type(String type) { | ||
this.type = type; | ||
return this; | ||
} | ||
|
||
public OpenIdConnectSchemeBuilder description(String description) { | ||
this.description = description; | ||
return this; | ||
} | ||
|
||
public OpenIdConnectSchemeBuilder extensions(List<VendorExtension> extensions) { | ||
this.extensions.addAll(nullToEmptyList(extensions)); | ||
return this; | ||
} | ||
|
||
public OpenIdConnectSchemeBuilder openIdConnectUrl(String openIdConnectUrl) { | ||
this.openIdConnectUrl = openIdConnectUrl; | ||
return this; | ||
} | ||
|
||
public OpenIdConnectScheme createOpenIdConnectScheme() { | ||
return new OpenIdConnectScheme(name, type, description, extensions, openIdConnectUrl); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
springfox-core/src/main/java/springfox/documentation/service/HttpAuthenticationScheme.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,35 @@ | ||
package springfox.documentation.service; | ||
|
||
import springfox.documentation.builders.HttpAuthenticationBuilder; | ||
|
||
import java.util.List; | ||
|
||
public class HttpAuthenticationScheme extends SecurityScheme { | ||
public static final HttpAuthenticationBuilder BASIC_AUTH_BUILDER = new HttpAuthenticationBuilder().scheme("basic"); | ||
public static final HttpAuthenticationBuilder JWT_BEARER_BUILDER = new HttpAuthenticationBuilder() | ||
.scheme("bearer") | ||
.bearerFormat("JWT"); | ||
private final String scheme; | ||
private final String bearerFormat; | ||
|
||
|
||
public HttpAuthenticationScheme( | ||
String name, | ||
String description, | ||
String type, | ||
String scheme, | ||
String bearerFormat, | ||
List<VendorExtension> extensions) { | ||
super(name, type, description, extensions); | ||
this.scheme = scheme; | ||
this.bearerFormat = bearerFormat; | ||
} | ||
|
||
public String getScheme() { | ||
return scheme; | ||
} | ||
|
||
public String getBearerFormat() { | ||
return bearerFormat; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
springfox-core/src/main/java/springfox/documentation/service/OAuth2Scheme.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,61 @@ | ||
package springfox.documentation.service; | ||
|
||
import springfox.documentation.builders.OAuth2SchemeBuilder; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class OAuth2Scheme extends SecurityScheme { | ||
public static final OAuth2SchemeBuilder OAUTH2_IMPLICIT_FLOW_BUILDER | ||
= new OAuth2SchemeBuilder("implicit"); | ||
public static final OAuth2SchemeBuilder OAUTH2_PASSWORD_FLOW_BUILDER | ||
= new OAuth2SchemeBuilder("password"); | ||
public static final OAuth2SchemeBuilder OAUTH2_CLIENT_CREDENTIALS_FLOW_BUILDER | ||
= new OAuth2SchemeBuilder("clientCredentials"); | ||
public static final OAuth2SchemeBuilder OAUTH2_AUTHORIZATION_CODE_FLOW_BUILDER | ||
= new OAuth2SchemeBuilder("authorizationCode"); | ||
|
||
private final String flowType; | ||
private final String authorizationUrl; | ||
private final String tokenUrl; | ||
private final String refreshUrl; | ||
private final List<AuthorizationScope> scopes = new ArrayList<>(); | ||
|
||
@SuppressWarnings("ParameterNumber") | ||
public OAuth2Scheme( | ||
String name, | ||
String flowType, | ||
String description, | ||
String authorizationUrl, | ||
String tokenUrl, | ||
String refreshUrl, | ||
List<AuthorizationScope> scopes, | ||
List<VendorExtension> extensions) { | ||
super(name, "oauth2", description, extensions); | ||
this.flowType = flowType; | ||
this.authorizationUrl = authorizationUrl; | ||
this.tokenUrl = tokenUrl; | ||
this.refreshUrl = refreshUrl; | ||
this.scopes.addAll(scopes); | ||
} | ||
|
||
public String getFlowType() { | ||
return flowType; | ||
} | ||
|
||
public String getAuthorizationUrl() { | ||
return authorizationUrl; | ||
} | ||
|
||
public String getTokenUrl() { | ||
return tokenUrl; | ||
} | ||
|
||
public String getRefreshUrl() { | ||
return refreshUrl; | ||
} | ||
|
||
public List<AuthorizationScope> getScopes() { | ||
return scopes; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
springfox-core/src/main/java/springfox/documentation/service/OpenIdConnectScheme.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,21 @@ | ||
package springfox.documentation.service; | ||
|
||
import java.util.List; | ||
|
||
public class OpenIdConnectScheme extends SecurityScheme { | ||
private final String openIdConnectUrl; | ||
|
||
public OpenIdConnectScheme( | ||
String name, | ||
String type, | ||
String description, | ||
List<VendorExtension> extensions, | ||
String openIdConnectUrl) { | ||
super(name, type, description, extensions); | ||
this.openIdConnectUrl = openIdConnectUrl; | ||
} | ||
|
||
public String getOpenIdConnectUrl() { | ||
return openIdConnectUrl; | ||
} | ||
} |