Skip to content

Commit

Permalink
better handle of oauth (#1838)
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 authored Jan 7, 2019
1 parent 233ce09 commit ed5bd5f
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,11 @@ private Map<String, Object> buildSupportFileBundle(List<Object> allOperations, L
if (authMethods != null && !authMethods.isEmpty()) {
bundle.put("authMethods", authMethods);
bundle.put("hasAuthMethods", true);

if (hasOAuthMethods(authMethods)) {
bundle.put("hasOAuthMethods", true);
bundle.put("oauthMethods", getOAuthMethods(authMethods));
}
}

List<CodegenServer> servers = config.fromServers(openAPI.getServers());
Expand Down Expand Up @@ -1182,4 +1187,26 @@ private Map<String, SecurityScheme> getAuthMethods(List<SecurityRequirement> sec
}
return authMethods;
}

private boolean hasOAuthMethods(List<CodegenSecurity> authMethods) {
for (CodegenSecurity cs : authMethods) {
if (cs.isOAuth) {
return true;
}
}

return false;
}

private List<CodegenSecurity> getOAuthMethods(List<CodegenSecurity> authMethods) {
List<CodegenSecurity> oauthMethods = new ArrayList<>();

for (CodegenSecurity cs : authMethods) {
if (cs.isOAuth) {
oauthMethods.add(cs);
}
}

return oauthMethods;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import org.threeten.bp.LocalDate;
import org.threeten.bp.OffsetDateTime;
import org.threeten.bp.format.DateTimeFormatter;
{{/threetenbp}}

{{#hasOAuthMethods}}
import org.apache.oltu.oauth2.client.request.OAuthClientRequest.TokenRequestBuilder;
import org.apache.oltu.oauth2.common.message.types.GrantType;
{{/hasOAuthMethods}}

import javax.net.ssl.*;
import java.io.File;
Expand Down Expand Up @@ -92,7 +93,10 @@ public class ApiClient {
// Prevent the authentications from being modified.
authentications = Collections.unmodifiableMap(authentications);
}
{{#authMethods}}{{#isOAuth}}

{{#hasOAuthMethods}}
{{#oauthMethods}}
{{#-first}}
/*
* Constructor for ApiClient to support access token retry on 401/403 configured with client ID
*/
Expand Down Expand Up @@ -123,7 +127,10 @@ public class ApiClient {
// Prevent the authentications from being modified.
authentications = Collections.unmodifiableMap(authentications);
}
{{/isOAuth}}{{/authMethods}}

{{/-first}}
{{/oauthMethods}}
{{/hasOAuthMethods}}
private void init() {
httpClient = new OkHttpClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,14 @@ if(hasProperty('target') && target == 'android') {
}

dependencies {
compile 'io.swagger:swagger-annotations:1.5.17'
compile 'io.swagger:swagger-annotations:1.5.21'
compile 'com.squareup.okhttp:okhttp:2.7.5'
compile 'com.squareup.okhttp:logging-interceptor:2.7.5'
compile 'com.google.code.gson:gson:2.8.1'
compile 'com.google.code.gson:gson:2.8.5'
compile 'io.gsonfire:gson-fire:1.8.0'
{{#hasOAuthMethods}}
compile group: 'org.apache.oltu.oauth2', name: 'org.apache.oltu.oauth2.client', version: '1.0.1'
{{/hasOAuthMethods}}
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.8.1'
{{#joda}}
compile 'joda-time:joda-time:2.9.9'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ lazy val root = (project in file(".")).
publishArtifact in (Compile, packageDoc) := false,
resolvers += Resolver.mavenLocal,
libraryDependencies ++= Seq(
"io.swagger" % "swagger-annotations" % "1.5.17",
"io.swagger" % "swagger-annotations" % "1.5.21",
"com.squareup.okhttp" % "okhttp" % "2.7.5",
"com.squareup.okhttp" % "logging-interceptor" % "2.7.5",
"com.google.code.gson" % "gson" % "2.8.1",
"com.google.code.gson" % "gson" % "2.8.5",
"org.apache.commons" % "commons-lang3" % "3.8.1",
{{#hasOAuthMethods}}
"org.apache.oltu.oauth2" % "org.apache.oltu.oauth2.client" % "1.0.1",
{{/hasOAuthMethods}}
{{#joda}}
"joda-time" % "joda-time" % "2.9.9" % "compile",
{{/joda}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,13 @@
<artifactId>gson-fire</artifactId>
<version>${gson-fire-version}</version>
</dependency>
{{#hasOAuthMethods}}
<dependency>
<groupId>org.apache.oltu.oauth2</groupId>
<artifactId>org.apache.oltu.oauth2.client</artifactId>
<version>1.0.1</version>
</dependency>
{{/hasOAuthMethods}}
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand Down Expand Up @@ -288,9 +290,9 @@
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<gson-fire-version>1.8.0</gson-fire-version>
<swagger-core-version>1.5.18</swagger-core-version>
<swagger-core-version>1.5.21</swagger-core-version>
<okhttp-version>2.7.5</okhttp-version>
<gson-version>2.8.1</gson-version>
<gson-version>2.8.5</gson-version>
<commons-lang3-version>3.8.1</commons-lang3-version>
{{#joda}}
<jodatime-version>2.9.9</jodatime-version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ if(hasProperty('target') && target == 'android') {
}

dependencies {
compile 'io.swagger:swagger-annotations:1.5.17'
compile 'io.swagger:swagger-annotations:1.5.21'
compile 'com.squareup.okhttp:okhttp:2.7.5'
compile 'com.squareup.okhttp:logging-interceptor:2.7.5'
compile 'com.google.code.gson:gson:2.8.1'
compile 'com.google.code.gson:gson:2.8.5'
compile 'io.gsonfire:gson-fire:1.8.0'
compile group: 'org.apache.oltu.oauth2', name: 'org.apache.oltu.oauth2.client', version: '1.0.1'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.8.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ lazy val root = (project in file(".")).
publishArtifact in (Compile, packageDoc) := false,
resolvers += Resolver.mavenLocal,
libraryDependencies ++= Seq(
"io.swagger" % "swagger-annotations" % "1.5.17",
"io.swagger" % "swagger-annotations" % "1.5.21",
"com.squareup.okhttp" % "okhttp" % "2.7.5",
"com.squareup.okhttp" % "logging-interceptor" % "2.7.5",
"com.google.code.gson" % "gson" % "2.8.1",
"com.google.code.gson" % "gson" % "2.8.5",
"org.apache.commons" % "commons-lang3" % "3.8.1",
"org.apache.oltu.oauth2" % "org.apache.oltu.oauth2.client" % "1.0.1",
"org.threeten" % "threetenbp" % "1.3.5" % "compile",
"io.gsonfire" % "gson-fire" % "1.8.0" % "compile",
"junit" % "junit" % "4.12" % "test",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,9 @@
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<gson-fire-version>1.8.0</gson-fire-version>
<swagger-core-version>1.5.18</swagger-core-version>
<swagger-core-version>1.5.21</swagger-core-version>
<okhttp-version>2.7.5</okhttp-version>
<gson-version>2.8.1</gson-version>
<gson-version>2.8.5</gson-version>
<commons-lang3-version>3.8.1</commons-lang3-version>
<threetenbp-version>1.3.5</threetenbp-version>
<maven-plugin-version>1.0.0</maven-plugin-version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.threeten.bp.LocalDate;
import org.threeten.bp.OffsetDateTime;
import org.threeten.bp.format.DateTimeFormatter;

import org.apache.oltu.oauth2.client.request.OAuthClientRequest.TokenRequestBuilder;
import org.apache.oltu.oauth2.common.message.types.GrantType;

Expand Down Expand Up @@ -92,7 +91,7 @@ public ApiClient() {
// Prevent the authentications from being modified.
authentications = Collections.unmodifiableMap(authentications);
}

/*
* Constructor for ApiClient to support access token retry on 401/403 configured with client ID
*/
Expand Down Expand Up @@ -123,7 +122,7 @@ public ApiClient(String clientId, String clientSecret, Map<String, String> param
// Prevent the authentications from being modified.
authentications = Collections.unmodifiableMap(authentications);
}

private void init() {
httpClient = new OkHttpClient();

Expand Down
4 changes: 2 additions & 2 deletions samples/client/petstore/java/okhttp-gson/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ if(hasProperty('target') && target == 'android') {
}

dependencies {
compile 'io.swagger:swagger-annotations:1.5.17'
compile 'io.swagger:swagger-annotations:1.5.21'
compile 'com.squareup.okhttp:okhttp:2.7.5'
compile 'com.squareup.okhttp:logging-interceptor:2.7.5'
compile 'com.google.code.gson:gson:2.8.1'
compile 'com.google.code.gson:gson:2.8.5'
compile 'io.gsonfire:gson-fire:1.8.0'
compile group: 'org.apache.oltu.oauth2', name: 'org.apache.oltu.oauth2.client', version: '1.0.1'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.8.1'
Expand Down
5 changes: 3 additions & 2 deletions samples/client/petstore/java/okhttp-gson/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ lazy val root = (project in file(".")).
publishArtifact in (Compile, packageDoc) := false,
resolvers += Resolver.mavenLocal,
libraryDependencies ++= Seq(
"io.swagger" % "swagger-annotations" % "1.5.17",
"io.swagger" % "swagger-annotations" % "1.5.21",
"com.squareup.okhttp" % "okhttp" % "2.7.5",
"com.squareup.okhttp" % "logging-interceptor" % "2.7.5",
"com.google.code.gson" % "gson" % "2.8.1",
"com.google.code.gson" % "gson" % "2.8.5",
"org.apache.commons" % "commons-lang3" % "3.8.1",
"org.apache.oltu.oauth2" % "org.apache.oltu.oauth2.client" % "1.0.1",
"org.threeten" % "threetenbp" % "1.3.5" % "compile",
"io.gsonfire" % "gson-fire" % "1.8.0" % "compile",
"junit" % "junit" % "4.12" % "test",
Expand Down
4 changes: 2 additions & 2 deletions samples/client/petstore/java/okhttp-gson/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,9 @@
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<gson-fire-version>1.8.0</gson-fire-version>
<swagger-core-version>1.5.18</swagger-core-version>
<swagger-core-version>1.5.21</swagger-core-version>
<okhttp-version>2.7.5</okhttp-version>
<gson-version>2.8.1</gson-version>
<gson-version>2.8.5</gson-version>
<commons-lang3-version>3.8.1</commons-lang3-version>
<threetenbp-version>1.3.5</threetenbp-version>
<maven-plugin-version>1.0.0</maven-plugin-version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.threeten.bp.LocalDate;
import org.threeten.bp.OffsetDateTime;
import org.threeten.bp.format.DateTimeFormatter;

import org.apache.oltu.oauth2.client.request.OAuthClientRequest.TokenRequestBuilder;
import org.apache.oltu.oauth2.common.message.types.GrantType;

Expand Down Expand Up @@ -92,7 +91,7 @@ public ApiClient() {
// Prevent the authentications from being modified.
authentications = Collections.unmodifiableMap(authentications);
}

/*
* Constructor for ApiClient to support access token retry on 401/403 configured with client ID
*/
Expand Down Expand Up @@ -123,7 +122,7 @@ public ApiClient(String clientId, String clientSecret, Map<String, String> param
// Prevent the authentications from being modified.
authentications = Collections.unmodifiableMap(authentications);
}

private void init() {
httpClient = new OkHttpClient();

Expand Down

0 comments on commit ed5bd5f

Please sign in to comment.