Java源码示例:io.swagger.v3.oas.models.security.OAuthFlows
示例1
public Optional<ChangedOAuthFlows> diff(OAuthFlows left, OAuthFlows right) {
ChangedOAuthFlows changedOAuthFlows = new ChangedOAuthFlows(left, right);
if (left != null && right != null) {
openApiDiff
.getOAuthFlowDiff()
.diff(left.getImplicit(), right.getImplicit())
.ifPresent(changedOAuthFlows::setImplicitOAuthFlow);
openApiDiff
.getOAuthFlowDiff()
.diff(left.getPassword(), right.getPassword())
.ifPresent(changedOAuthFlows::setPasswordOAuthFlow);
openApiDiff
.getOAuthFlowDiff()
.diff(left.getClientCredentials(), right.getClientCredentials())
.ifPresent(changedOAuthFlows::setClientCredentialOAuthFlow);
openApiDiff
.getOAuthFlowDiff()
.diff(left.getAuthorizationCode(), right.getAuthorizationCode())
.ifPresent(changedOAuthFlows::setAuthorizationCodeOAuthFlow);
}
openApiDiff
.getExtensionsDiff()
.diff(getExtensions(left), getExtensions(right))
.ifPresent(changedOAuthFlows::setExtensions);
return isChanged(changedOAuthFlows);
}
示例2
private OpenAPI createBasicModel() {
OpenAPI openAPI = new OpenAPI();
Info info = new Info();
info.setTitle(configuration.getApplicationTitle());
info.setVersion(configuration.getApplicationApiVersion());
openAPI.setInfo(info);
Paths paths = new Paths();
openAPI.setPaths(paths);
Server server = new Server();
server.setUrl(configuration.getServerUrl());
server.setDescription(configuration.getServerDescription());
openAPI.setServers(Collections.singletonList(server));
Components components = new Components();
SecurityScheme vaadinConnectOAuth2Scheme = new SecurityScheme()
.type(SecurityScheme.Type.OAUTH2)
.flows(new OAuthFlows().password(new OAuthFlow()
.tokenUrl(VAADIN_CONNECT_OAUTH2_TOKEN_URL)
.scopes(new Scopes())));
components.addSecuritySchemes(VAADIN_CONNECT_OAUTH2_SECURITY_SCHEME,
vaadinConnectOAuth2Scheme);
openAPI.components(components);
return openAPI;
}
示例3
private SecurityScheme getAuthScheme() {
try {
final KeycloakClientConfig keycloakConfig = systemConfig.getKeycloakServiceAccountConfig();
if (keycloakConfig == null || StringUtils.isEmpty(keycloakConfig.getAuthServerUrl())) {
return null;
}
URI keycloakURL = new URI(keycloakConfig.getAuthServerUrl() + "/")
.resolve("realms/" + keycloakConfig.getRealm() + "/protocol/openid-connect/auth");
final OAuthFlow implicitFlow = new OAuthFlow().authorizationUrl(keycloakURL.toString());
SecurityScheme scheme = new SecurityScheme();
scheme.type(SecurityScheme.Type.OAUTH2)
.description("This application uses Keycloak oauth authentication")
.flows(new OAuthFlows().implicit(implicitFlow));
return scheme;
} catch (URISyntaxException ex) {
logger.warn("Failed to parse Keycloak setting", ex);
return null;
}
}
示例4
/**
* Is empty boolean.
*
* @param oAuthFlows the o auth flows
* @return the boolean
*/
private static boolean isEmpty(io.swagger.v3.oas.annotations.security.OAuthFlows oAuthFlows) {
boolean result;
if (oAuthFlows == null)
result = true;
else if (!isEmpty(oAuthFlows.implicit()) || !isEmpty(oAuthFlows.authorizationCode()) || !isEmpty(oAuthFlows.clientCredentials()) || !isEmpty(oAuthFlows.password()))
result = false;
else result = oAuthFlows.extensions().length <= 0;
return result;
}
示例5
private SecurityScheme getDummyScheme() {
SecurityScheme scheme = new SecurityScheme();
scheme.type(SecurityScheme.Type.HTTP);
scheme.description("desc");
scheme.name("name");
scheme.$ref("ref");
scheme.in(SecurityScheme.In.COOKIE);
scheme.scheme("scheme");
scheme.bearerFormat("format");
scheme.flows(new OAuthFlows());
scheme.openIdConnectUrl("url");
scheme.extensions(Collections.emptyMap());
return scheme;
}
示例6
@Bean
public OpenAPI openApi() {
return new OpenAPI()
.components(new Components()
//HTTP Basic, see: https://swagger.io/docs/specification/authentication/basic-authentication/
.addSecuritySchemes("basicScheme", new SecurityScheme()
.type(SecurityScheme.Type.HTTP)
.scheme("basic")
)
//API Key, see: https://swagger.io/docs/specification/authentication/api-keys/
.addSecuritySchemes("apiKeyScheme", new SecurityScheme()
.type(SecurityScheme.Type.APIKEY)
.in(SecurityScheme.In.HEADER)
.name("X-API-KEY")
)
//OAuth 2.0, see: https://swagger.io/docs/specification/authentication/oauth2/
.addSecuritySchemes("oAuthScheme", new SecurityScheme()
.type(SecurityScheme.Type.OAUTH2)
.description("This API uses OAuth 2 with the implicit grant flow. [More info](https://api.example.com/docs/auth)")
.flows(new OAuthFlows()
.implicit(new OAuthFlow()
.authorizationUrl("https://api.example.com/oauth2/authorize")
.scopes(new Scopes()
.addString("read_pets", "read your pets")
.addString("write_pets", "modify pets in your account")
)
)
)
)
)
.addSecurityItem(new SecurityRequirement()
.addList("basicScheme")
)
.addSecurityItem(new SecurityRequirement()
.addList("apiKeyScheme")
)
.addSecurityItem(new SecurityRequirement()
.addList("oAuthScheme")
)
;
}
示例7
public ChangedOAuthFlows(OAuthFlows oldOAuthFlows, OAuthFlows newOAuthFlows) {
this.oldOAuthFlows = oldOAuthFlows;
this.newOAuthFlows = newOAuthFlows;
}
示例8
private static Map<String, Object> getExtensions(OAuthFlows oAuthFlow) {
return ofNullable(oAuthFlow).map(OAuthFlows::getExtensions).orElse(null);
}
示例9
private SecurityScheme convertOauth2SecurityScheme(SecuritySchemeDefinition definition) {
SecurityScheme securityScheme = new SecurityScheme();
OAuth2Definition oAuth2Definition = (OAuth2Definition) definition;
OAuthFlows oAuthFlows = new OAuthFlows();
OAuthFlow oAuthFlow = new OAuthFlow();
securityScheme.setType(SecurityScheme.Type.OAUTH2);
String flow = oAuth2Definition.getFlow();
if (flow != null) {
switch (flow) {
case "implicit":
oAuthFlow.setAuthorizationUrl(oAuth2Definition.getAuthorizationUrl());
oAuthFlows.setImplicit(oAuthFlow);
break;
case "password":
oAuthFlow.setTokenUrl(oAuth2Definition.getTokenUrl());
oAuthFlows.setPassword(oAuthFlow);
break;
case "application":
oAuthFlow.setTokenUrl(oAuth2Definition.getTokenUrl());
oAuthFlows.setClientCredentials(oAuthFlow);
break;
case "accessCode":
oAuthFlow.setAuthorizationUrl(oAuth2Definition.getAuthorizationUrl());
oAuthFlow.setTokenUrl(oAuth2Definition.getTokenUrl());
oAuthFlows.setAuthorizationCode(oAuthFlow);
break;
}
}
Scopes scopes = new Scopes();
Map<String, String> oAuth2Scopes = oAuth2Definition.getScopes();
if (oAuth2Scopes != null) {
oAuth2Scopes.forEach((k, v) -> scopes.addString(k, v));
}
oAuthFlow.setScopes(scopes);
securityScheme.setFlows(oAuthFlows);
return securityScheme;
}
示例10
public OAuthFlows getOAuthFlows(ObjectNode node, String location, ParseResult result) {
if (node == null) {
return null;
}
OAuthFlows oAuthFlows = new OAuthFlows();
ObjectNode objectNode = getObject("implicit", node, false, location, result);
if(objectNode!= null) {
oAuthFlows.setImplicit(getOAuthFlow("implicit", objectNode, location, result));
}
objectNode = getObject("password", node, false, location, result);
if(objectNode!= null) {
oAuthFlows.setPassword(getOAuthFlow("password", objectNode, location, result));
}
objectNode = getObject("clientCredentials", node, false, location, result);
if(objectNode!= null) {
oAuthFlows.setClientCredentials(getOAuthFlow("clientCredentials", objectNode, location, result));
}
objectNode = getObject("authorizationCode", node, false, location, result);
if(objectNode!= null) {
oAuthFlows.setAuthorizationCode(getOAuthFlow("authorizationCode", objectNode, location, result));
}
Map <String,Object> extensions = getExtensions(node);
if(extensions != null && extensions.size() > 0) {
oAuthFlows.setExtensions(extensions);
}
Set<String> oAuthFlowKeys = getKeys(node);
for(String key : oAuthFlowKeys) {
if(!OAUTHFLOWS_KEYS.contains(key) && !key.startsWith("x-")) {
result.extra(location, key, node.get(key));
}
}
return oAuthFlows;
}
示例11
/**
* This is to avoid removing the `scopes` field of default security scheme when there are no scopes present. This
* will set an empty scope object there.
*
* securitySchemes:
* default:
* type: oauth2
* flows:
* implicit:
* authorizationUrl: 'https://test.com'
* scopes: {}
* x-scopes-bindings: {}
*
* @param swagger OpenAPI object
*/
private void checkAndSetEmptyScope(OpenAPI swagger) {
Components comp = swagger.getComponents();
Map<String, SecurityScheme> securitySchemeMap;
SecurityScheme securityScheme;
OAuthFlows oAuthFlows;
OAuthFlow implicitFlow;
if (comp != null && (securitySchemeMap = comp.getSecuritySchemes()) != null &&
(securityScheme = securitySchemeMap.get(OPENAPI_SECURITY_SCHEMA_KEY)) != null &&
(oAuthFlows = securityScheme.getFlows()) != null &&
(implicitFlow = oAuthFlows.getImplicit()) != null && implicitFlow.getScopes() == null) {
implicitFlow.setScopes(new Scopes());
}
}