Skip to content
GitLab
    • Explore Projects Groups Snippets
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Merge requests
  • !10965

replaced oltu with scribejava for Java - okhttp client

  • Review changes

  • Download
  • Email patches
  • Plain diff
Open Administrator requested to merge github/fork/valmoz/okhttp-oltu-scribejava-replacement-2 into master 3 years ago
  • Overview 0
  • Commits 5
  • Pipelines 1
  • Changes 47

Created by: valmoz

#10661

This PR replaces the deprecated Apache Oltu library with ScribeJava for the Java OkHttp client. It also tries to improve the OAuth code as follows:

  • uniforming the OAuth authentication to the other auth methods, managing the Authentication as a parameter instead of using an OkHttp interceptor;
  • introducing the AuthorizationCode, Password, and ClientCredentials Grants;
  • managing authorization URL, refresh URL, and scope parameters for the oauth2 config.

this replaces #10732

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    Commit all changed files. This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master. These must match the expectations made by your contribution. You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*. For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master (5.3.0), 6.0.x
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@wing328 @bbdouglas @sreeshas @jfiala @lukoyanov @cbornet @jeff9finger @karismann @Zomzog @lwlee2608 @nmuesch

Compare
  • version 1
    0eeef9af
    2 years ago

  • master (base)

and
  • latest version
    0eeef9af
    5 commits, 2 years ago

  • version 1
    0eeef9af
    5 commits, 2 years ago

47 files
+ 1794
- 852

    Preferences

    File browser
    Compare changes
modules/openap‎i-generator/src‎
ma‎in‎
java/org/openapitoo‎ls/codegen/languages‎
JavaClientC‎odegen.java‎ +5 -3
resources/Java/lib‎raries/okhttp-gson‎
au‎th‎
DefaultApi20‎Impl.mustache‎ +65 -0
OAuthAuthorization‎CodeGrant.mustache‎ +105 -0
OAuthClientCredent‎ialsGrant.mustache‎ +64 -0
OAuthOkHttpCl‎ient.mustache‎ +0 -70
OAuthPassword‎Grant.mustache‎ +80 -0
RetryingOAu‎th.mustache‎ +0 -184
ApiClient‎.mustache‎ +64 -51
build.grad‎le.mustache‎ +2 -1
build.sbt‎.mustache‎ +2 -1
pom.mu‎stache‎ +8 -3
test/java/org/ope‎napitools/codegen‎
DefaultGener‎atorTest.java‎ +1 -1
samples‎/client‎
others/java/…/…‎/…/…/…/…/client‎
ApiClie‎nt.java‎ +1 -5
petsto‎re/java‎
okhtt‎p-gson‎
.openapi-‎generator‎
FI‎LES‎ +4 -2
s‎rc‎
main/java/org/op‎enapitools/client‎
au‎th‎
DefaultApi‎20Impl.java‎ +63 -0
OAuthAuthorizati‎onCodeGrant.java‎ +103 -0
OAuthClientCrede‎ntialsGrant.java‎ +62 -0
OAuthOkHttp‎Client.java‎ +0 -68
OAuthPasswo‎rdGrant.java‎ +78 -0
RetryingO‎Auth.java‎ +0 -182
ApiClie‎nt.java‎ +62 -45
test/java/org/op‎enapitools/client‎
a‎pi‎
PetApiT‎est.java‎ +0 -4
au‎th‎
OAuthAuthorization‎CodeGrantTest.java‎ +96 -0
OAuthClientCredent‎ialsGrantTest.java‎ +73 -0
OAuthPassword‎GrantTest.java‎ +76 -0
RetryingOAu‎thTest.java‎ +0 -123
build.‎gradle‎ +2 -1
buil‎d.sbt‎ +2 -1
pom‎.xml‎ +8 -3
okhttp-gson-dy‎namicOperations‎
.openapi-‎generator‎
FI‎LES‎ +4 -2
src/main/java/org/‎openapitools/client‎
au‎th‎
DefaultApi‎20Impl.java‎ +63 -0
OAuthAuthorizati‎onCodeGrant.java‎ +103 -0
OAuthClientCrede‎ntialsGrant.java‎ +62 -0
OAuthPasswo‎rdGrant.java‎ +78 -0
ApiClie‎nt.java‎ +62 -45
build.‎gradle‎ +2 -1
buil‎d.sbt‎ +2 -1
pom‎.xml‎ +8 -3
okhttp-gson-p‎arcelableModel‎
.openapi-‎generator‎
FI‎LES‎ +4 -2
src/main/java/org/‎openapitools/client‎
au‎th‎
DefaultApi‎20Impl.java‎ +63 -0
OAuthAuthorizati‎onCodeGrant.java‎ +103 -0
OAuthClientCrede‎ntialsGrant.java‎ +62 -0
OAuthPasswo‎rdGrant.java‎ +78 -0
ApiClie‎nt.java‎ +62 -45
build.‎gradle‎ +2 -1
buil‎d.sbt‎ +2 -1
pom‎.xml‎ +8 -3
modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java
+ 5
- 3
  • View file @ 0eeef9af

  • Edit in single-file editor

  • Open in Web IDE


Conflict: This file was modified in both the source and target branches. Ask someone with write access to resolve it.
@@ -635,9 +635,11 @@ public class JavaClientCodegen extends AbstractJavaCodegen
// has OAuth defined
if (ProcessUtils.hasOAuthMethods(openAPI)) {
// for okhttp-gson (default), check to see if OAuth is defined and included OAuth-related files accordingly
if ((OKHTTP_GSON.equals(getLibrary()) || StringUtils.isEmpty(getLibrary())) || OKHTTP_GSON_NEXTGEN.equals(getLibrary())) {
supportingFiles.add(new SupportingFile("auth/OAuthOkHttpClient.mustache", authFolder, "OAuthOkHttpClient.java"));
supportingFiles.add(new SupportingFile("auth/RetryingOAuth.mustache", authFolder, "RetryingOAuth.java"));
if ((OKHTTP_GSON.equals(getLibrary()) || StringUtils.isEmpty(getLibrary()) || OKHTTP_GSON_NEXTGEN.equals(getLibrary()))) {
supportingFiles.add(new SupportingFile("auth/DefaultApi20Impl.mustache", authFolder, "DefaultApi20Impl.java"));
supportingFiles.add(new SupportingFile("auth/OAuthAuthorizationCodeGrant.mustache", authFolder, "OAuthAuthorizationCodeGrant.java"));
supportingFiles.add(new SupportingFile("auth/OAuthClientCredentialsGrant.mustache", authFolder, "OAuthClientCredentialsGrant.java"));
supportingFiles.add(new SupportingFile("auth/OAuthPasswordGrant.mustache", authFolder, "OAuthPasswordGrant.java"));
}
// google-api-client doesn't use the OpenAPI auth, because it uses Google Credential directly (HttpRequestInitializer)
modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/auth/DefaultApi20Impl.mustache 0 → 100644
+ 65
- 0
  • View file @ 0eeef9af

  • Edit in single-file editor

  • Open in Web IDE

{{#hasOAuthMethods}}
package {{invokerPackage}}.auth;
import com.github.scribejava.core.builder.api.DefaultApi20;
import com.github.scribejava.core.extractors.OAuth2AccessTokenJsonExtractor;
import com.github.scribejava.core.extractors.TokenExtractor;
import com.github.scribejava.core.model.OAuth2AccessToken;
import com.github.scribejava.core.oauth2.bearersignature.BearerSignature;
import com.github.scribejava.core.oauth2.bearersignature.BearerSignatureURIQueryParameter;
import com.github.scribejava.core.oauth2.clientauthentication.ClientAuthentication;
import com.github.scribejava.core.oauth2.clientauthentication.RequestBodyAuthenticationScheme;
{{>generatedAnnotation}}
public class DefaultApi20Impl extends DefaultApi20 {
private final String accessTokenEndpoint;
private final String authorizationBaseUrl;
private final String refreshTokenEndpoint;
protected DefaultApi20Impl(String accessTokenEndpoint) {
this(accessTokenEndpoint, null, null);
}
protected DefaultApi20Impl(String accessTokenEndpoint, String refreshTokenEndpoint) {
this(accessTokenEndpoint, null, refreshTokenEndpoint);
}
protected DefaultApi20Impl(String accessTokenEndpoint, String authorizationBaseUrl, String refreshTokenEndpoint) {
this.accessTokenEndpoint = accessTokenEndpoint;
this.authorizationBaseUrl = authorizationBaseUrl;
this.refreshTokenEndpoint = refreshTokenEndpoint;
}
@Override
public String getRefreshTokenEndpoint() {
if (this.refreshTokenEndpoint == null || this.refreshTokenEndpoint.trim().isEmpty()) {
return this.getAccessTokenEndpoint();
}
return this.refreshTokenEndpoint;
}
@Override
public String getAccessTokenEndpoint() {
return accessTokenEndpoint;
}
@Override
protected String getAuthorizationBaseUrl() {
return this.authorizationBaseUrl;
}
@Override
public BearerSignature getBearerSignature() {
return BearerSignatureURIQueryParameter.instance();
}
@Override
public ClientAuthentication getClientAuthentication() {
return RequestBodyAuthenticationScheme.instance();
}
@Override
public TokenExtractor<OAuth2AccessToken> getAccessTokenExtractor() {
return OAuth2AccessTokenJsonExtractor.instance();
}
}
{{/hasOAuthMethods}}
\ No newline at end of file
modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/auth/OAuthAuthorizationCodeGrant.mustache 0 → 100644
+ 105
- 0
  • View file @ 0eeef9af

  • Edit in single-file editor

  • Open in Web IDE

{{#hasOAuthMethods}}
package {{invokerPackage}}.auth;
import com.github.scribejava.core.builder.ServiceBuilder;
import com.github.scribejava.core.model.OAuth2AccessToken;
import com.github.scribejava.core.model.OAuth2Authorization;
import com.github.scribejava.core.oauth.AccessTokenRequestParams;
import com.github.scribejava.core.oauth.OAuth20Service;
import com.github.scribejava.httpclient.okhttp.OkHttpHttpClient;
import okhttp3.OkHttpClient;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ExecutionException;
{{>generatedAnnotation}}
public class OAuthAuthorizationCodeGrant extends OAuth {
private DefaultApi20Impl apiInstance;
private OAuth20Service service;
private Map<String, String> params;
public OAuthAuthorizationCodeGrant(String clientId, String clientSecret, String authorizationBaseUrl, String accessTokenEndpoint, String callbackUrl, Map<String, String> params) {
this(clientId, clientSecret, null, authorizationBaseUrl, accessTokenEndpoint, callbackUrl, null, params, null);
}
public OAuthAuthorizationCodeGrant(String clientId, String clientSecret, String scope, String authorizationBaseUrl, String accessTokenEndpoint, String callbackUrl, Map<String, String> params) {
this(clientId, clientSecret, scope, authorizationBaseUrl, accessTokenEndpoint, callbackUrl, null, params, null);
}
public OAuthAuthorizationCodeGrant(String clientId, String clientSecret, String scope, String authorizationBaseUrl, String accessTokenEndpoint, String callbackUrl, String refreshUrl, Map<String, String> params) {
this(clientId, clientSecret, scope, authorizationBaseUrl, accessTokenEndpoint, callbackUrl, refreshUrl, params, null);
}
public OAuthAuthorizationCodeGrant(String clientId, String clientSecret, String scope, String authorizationBaseUrl, String accessTokenEndpoint, String callbackUrl, String refreshUrl, Map<String, String> params, OkHttpClient httpClient) {
this.apiInstance = new DefaultApi20Impl(accessTokenEndpoint, authorizationBaseUrl, refreshUrl);
ServiceBuilder serviceBuilder = new ServiceBuilder(clientId)
.apiSecret(clientSecret)
.callback(callbackUrl);
if (scope != null && !scope.trim().isEmpty()) {
serviceBuilder.defaultScope(scope);
}
if (httpClient != null) {
serviceBuilder.httpClient(new OkHttpHttpClient(httpClient));
} else {
serviceBuilder.httpClient(new OkHttpHttpClient(new OkHttpClient()));
}
this.service = serviceBuilder.build(this.apiInstance);
this.params = params;
}
public String getAuthorizationUrl(String state) {
if (state != null && !state.trim().isEmpty()) {
return this.service.getAuthorizationUrl(state);
} else {
return this.service.getAuthorizationUrl();
}
}
public OAuth2Authorization extractAuthorization(String redirectLocation) {
return service.extractAuthorization(redirectLocation);
}
public OAuth2AccessToken obtainAccessToken(OAuth2Authorization authorization, String scope) throws IOException, ExecutionException, InterruptedException {
return this.obtainAccessToken(authorization.getCode(), scope);
}
public OAuth2AccessToken obtainAccessToken(String code, String scope) throws IOException, ExecutionException, InterruptedException {
AccessTokenRequestParams reqParams = new AccessTokenRequestParams(code);
reqParams.addExtraParameters(params);
if (scope != null && !scope.trim().isEmpty()) {
reqParams.scope(scope);
}
OAuth2AccessToken tokenResponse = service.getAccessToken(reqParams);
this.setAccessToken(tokenResponse.getAccessToken());
return tokenResponse;
}
public void setToken(OAuth2AccessToken token) {
this.setAccessToken(token.getAccessToken());
}
public void setToken(String accessToken) {
this.setAccessToken(accessToken);
}
public OAuth2AccessToken refreshToken(OAuth2AccessToken token, String scope) throws IOException, ExecutionException, InterruptedException {
return this.refreshToken(token.getRefreshToken(), scope);
}
public OAuth2AccessToken refreshToken(String refreshToken, String scope) throws IOException, ExecutionException, InterruptedException {
if (scope != null && !scope.trim().isEmpty()) {
return this.service.refreshAccessToken(refreshToken, scope);
} else {
return this.service.refreshAccessToken(refreshToken);
}
}
}
{{/hasOAuthMethods}}
\ No newline at end of file
modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/auth/OAuthClientCredentialsGrant.mustache 0 → 100644
+ 64
- 0
  • View file @ 0eeef9af

  • Edit in single-file editor

  • Open in Web IDE

{{#hasOAuthMethods}}
package {{invokerPackage}}.auth;
import com.github.scribejava.core.builder.ServiceBuilder;
import com.github.scribejava.core.model.OAuth2AccessToken;
import com.github.scribejava.core.oauth.OAuth20Service;
import com.github.scribejava.httpclient.okhttp.OkHttpHttpClient;
import okhttp3.OkHttpClient;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
{{>generatedAnnotation}}
public class OAuthClientCredentialsGrant extends OAuth {
private DefaultApi20Impl apiInstance;
private OAuth20Service service;
public OAuthClientCredentialsGrant(String clientId, String clientSecret, String accessTokenEndpoint) {
this(clientId, clientSecret, null, accessTokenEndpoint, null);
}
public OAuthClientCredentialsGrant(String clientId, String clientSecret, String scope, String accessTokenEndpoint) {
this(clientId, clientSecret, scope, accessTokenEndpoint, null);
}
public OAuthClientCredentialsGrant(String clientId, String clientSecret, String scope, String accessTokenEndpoint, OkHttpClient httpClient) {
this.apiInstance = new DefaultApi20Impl(accessTokenEndpoint, null, null);
ServiceBuilder serviceBuilder = new ServiceBuilder(clientId)
.apiSecret(clientSecret);
if (scope != null && !scope.trim().isEmpty()) {
serviceBuilder.defaultScope(scope);
}
if (httpClient != null) {
serviceBuilder.httpClient(new OkHttpHttpClient(httpClient));
} else {
serviceBuilder.httpClient(new OkHttpHttpClient(new OkHttpClient()));
}
this.service = serviceBuilder.build(this.apiInstance);
}
public OAuth2AccessToken obtainAccessToken(String scope) throws IOException, ExecutionException, InterruptedException {
OAuth2AccessToken tokenResponse;
if (scope != null && !scope.trim().isEmpty()) {
tokenResponse = service.getAccessTokenClientCredentialsGrant(scope);
} else {
tokenResponse = service.getAccessTokenClientCredentialsGrant();
}
this.setAccessToken(tokenResponse.getAccessToken());
return tokenResponse;
}
public void setToken(OAuth2AccessToken token) {
this.setToken(token.getAccessToken());
}
public void setToken(String accessToken) {
this.setAccessToken(accessToken);
}
}
{{/hasOAuthMethods}}
\ No newline at end of file
modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/auth/OAuthOkHttpClient.mustache deleted 100644 → 0
+ 0
- 70
  • View file @ b2daa5a8

Conflict: This file was removed in the source branch, but modified in the target branch. Ask someone with write access to resolve it.
{{#hasOAuthMethods}}
package {{invokerPackage}}.auth;
import okhttp3.OkHttpClient;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.apache.oltu.oauth2.client.HttpClient;
import org.apache.oltu.oauth2.client.request.OAuthClientRequest;
import org.apache.oltu.oauth2.client.response.OAuthClientResponse;
import org.apache.oltu.oauth2.client.response.OAuthClientResponseFactory;
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
public class OAuthOkHttpClient implements HttpClient {
private OkHttpClient client;
public OAuthOkHttpClient() {
this.client = new OkHttpClient();
}
public OAuthOkHttpClient(OkHttpClient client) {
this.client = client;
}
@Override
public <T extends OAuthClientResponse> T execute(OAuthClientRequest request, Map<String, String> headers,
String requestMethod, Class<T> responseClass)
throws OAuthSystemException, OAuthProblemException {
MediaType mediaType = MediaType.parse("application/json");
Request.Builder requestBuilder = new Request.Builder().url(request.getLocationUri());
if(headers != null) {
for (Entry<String, String> entry : headers.entrySet()) {
if (entry.getKey().equalsIgnoreCase("Content-Type")) {
mediaType = MediaType.parse(entry.getValue());
} else {
requestBuilder.addHeader(entry.getKey(), entry.getValue());
}
}
}
RequestBody body = request.getBody() != null ? RequestBody.create(request.getBody(), mediaType) : null;
requestBuilder.method(requestMethod, body);
try {
Response response = client.newCall(requestBuilder.build()).execute();
return OAuthClientResponseFactory.createCustomResponse(
response.body().string(),
response.body().contentType().toString(),
response.code(),
responseClass);
} catch (IOException e) {
throw new OAuthSystemException(e);
}
}
@Override
public void shutdown() {
// Nothing to do here
}
}
{{/hasOAuthMethods}}
0 Assignees
None
Assign to
0 Reviewers
None
Request review from
Labels
3
Feature: Enum Feature: Generator Issue: Bug
3
Feature: Enum Feature: Generator Issue: Bug
    Assign labels
  • Manage project labels

Milestone
6.0.0
6.0.0 (expired)
None
Time tracking
No estimate or time spent
Lock merge request
Unlocked
0
0 participants
Reference: OpenAPITools/openapi-generator!11653
Source branch: github/fork/valmoz/okhttp-oltu-scribejava-replacement-2

Menu

Explore Projects Groups Snippets