Skip to content

Commit

Permalink
java-cxf-client: Support multiple authentication schemes
Browse files Browse the repository at this point in the history
  • Loading branch information
karlvr committed Feb 28, 2024
1 parent 822b2bb commit 252baab
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 43 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-bikes-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@openapi-generator-plus/java-cxf-client-generator": minor
---

Support multiple authentication schemes.
58 changes: 33 additions & 25 deletions packages/java-cxf-client/templates/hooks/apiImplClassBody.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ private org.apache.cxf.jaxrs.client.Client client;

{{#each @root/securitySchemes}}
{{#ifeq type 'apiKey'}}
private java.lang.String authorization;
private java.lang.String {{{identifier name}}}Authorization;
{{/ifeq}}
{{#ifeq type 'http'}}
{{#ifeq scheme 'basic'}}
private java.lang.String username;
private java.lang.String password;
private java.lang.String {{{identifier name}}}Username;
private java.lang.String {{{identifier name}}}Password;
{{else}}
private java.lang.String authorization;
private java.lang.String {{{identifier name}}}Authorization;
{{/ifeq}}
{{/ifeq}}
{{#or (ifeq type 'oauth2') (ifeq type 'openIdConnect')}}
private java.lang.String authorization;
private java.lang.String {{{identifier name}}}Authorization;
{{/or}}
{{/each}}

Expand Down Expand Up @@ -42,9 +42,17 @@ public {{className name}}ApiImpl({{@root.apiSpecPackage}}.{{className name}}ApiS
}

{{#each @root/securitySchemes}}
{{#if (and @first @last)}}
{{#set '__securitySchemeName'}}
{{/set}}
{{else}}
{{#set '__securitySchemeName'}}
{{{className name}}}
{{/set}}
{{/if}}
{{#ifeq type 'apiKey'}}
public void authorize(java.lang.String apiKeyValue) {
this.authorization = apiKeyValue;
public void authorize{{{__securitySchemeName}}}(java.lang.String apiKeyValue) {
this.{{{identifier name}}}Authorization = apiKeyValue;
{{#ifeq in 'header'}}
client.header({{{stringLiteral paramName}}}, apiKeyValue);
{{else ifeq in 'cookie'}}
Expand All @@ -55,54 +63,54 @@ public void authorize(java.lang.String apiKeyValue) {
}

@java.lang.Override
public java.lang.String getAuthorization() {
return authorization;
public java.lang.String get{{{__securitySchemeName}}}Authorization() {
return {{{identifier name}}}Authorization;
}
{{/ifeq}}
{{#ifeq type 'http'}}
{{#ifeq scheme 'basic'}}
public void authorize(java.lang.String username, java.lang.String password) {
this.username = username;
this.password = password;
public void authorize{{{__securitySchemeName}}}(java.lang.String username, java.lang.String password) {
this.{{{identifier name}}}Username = username;
this.{{{identifier name}}}Password = password;
client.authorization("Basic " + java.util.Base64.getEncoder().encodeToString((username + ":" + password).getBytes(java.nio.charset.StandardCharsets.UTF_8)));
}

@java.lang.Override
public java.lang.String getAuthorizationUsername() {
return username;
public java.lang.String get{{#if __securitySchemeName}}{{{__securitySchemeName}}}{{else}}Authorization{{/if}}Username() {
return {{{identifier name}}}Username;
}

@java.lang.Override
public java.lang.String getAuthorizationPassword() {
return password;
public java.lang.String get{{#if __securitySchemeName}}{{{__securitySchemeName}}}{{else}}Authorization{{/if}}Password() {
return {{{identifier name}}}Password;
}
{{else}}
@java.lang.Override
public void authorize(java.lang.String value) {
this.authorization = value;
public void authorize{{{__securitySchemeName}}}(java.lang.String value) {
this.{{{identifier name}}}Authorization = value;
client.authorization({{{stringLiteral (concat (capitalize scheme) ' ')}}} + value);
}

@java.lang.Override
public java.lang.String getAuthorization() {
return authorization;
public java.lang.String get{{{__securitySchemeName}}}Authorization() {
return {{{identifier name}}}Authorization;
}
{{/ifeq}}
{{/ifeq}}
{{#or (ifeq type 'oauth2') (ifeq type 'openIdConnect')}}
@java.lang.Override
public void authorize(java.lang.String bearerToken) {
this.authorization = bearerToken;
public void authorize{{{__securitySchemeName}}}(java.lang.String bearerToken) {
this.{{{identifier name}}}Authorization = bearerToken;
client.authorization("Bearer " + bearerToken);
}

@java.lang.Override
public java.lang.String getAuthorization() {
return authorization;
public java.lang.String get{{{__securitySchemeName}}}Authorization() {
return {{{identifier name}}}Authorization;
}
{{/or}}
{{/each}}

{{/each}}
@java.lang.Override
public org.apache.cxf.jaxrs.client.Client client() {
return client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,65 @@ The body of the API group invoker interface.
--}}
{{#if securitySchemes}}
{{#each securitySchemes}}
{{#if (and @first @last)}}
{{#set '__securitySchemeName'}}
{{/set}}
{{else}}
{{#set '__securitySchemeName'}}
{{{className name}}}
{{/set}}
{{/if}}
{{#ifeq type 'apiKey'}}
/**
* Authorize requests using an API key.
* Authorize requests using an API key for the security scheme "{{name}}"
*/
void authorize(java.lang.String apiKeyValue);
void authorize{{{__securitySchemeName}}}(java.lang.String apiKeyValue);

/**
* Returns the API key used to authorize requests.
* Returns the API key used to authorize requests for the security scheme "{{name}}".
*/
java.lang.String getAuthorization();
java.lang.String get{{{__securitySchemeName}}}Authorization();
{{/ifeq}}
{{#ifeq type 'http'}}
{{#ifeq scheme 'basic'}}
/**
* Authorize requests using HTTP Basic authorization.
* Authorize requests using HTTP Basic authorization for the security scheme "{{name}}".
*/
void authorize(java.lang.String username, java.lang.String password);
void authorize{{{__securitySchemeName}}}(java.lang.String username, java.lang.String password);

/**
* Returns the username used to authorize requests.
* Returns the username used to authorize requests for the security scheme "{{name}}".
*/
java.lang.String getAuthorizationUsername();
java.lang.String get{{#if __securitySchemeName}}{{{__securitySchemeName}}}{{else}}Authorization{{/if}}Username();

/**
* Returns the password used to authorize requests.
* Returns the password used to authorize requests for the security scheme "{{name}}".
*/
java.lang.String getAuthorizationPassword();
java.lang.String get{{#if __securitySchemeName}}{{{__securitySchemeName}}}{{else}}Authorization{{/if}}Password();
{{else}}
/**
* Authorize requests using {{scheme}} authorization.
* Authorize requests using {{scheme}} authorization for the security scheme "{{name}}".
*/
void authorize(java.lang.String value);
void authorize{{{__securitySchemeName}}}(java.lang.String value);

/**
* Returns the value used to authorize requests.
* Returns the value used to authorize requests for the security scheme "{{name}}".
*/
java.lang.String getAuthorization();
java.lang.String get{{{__securitySchemeName}}}Authorization();
{{/ifeq}}
{{/ifeq}}
{{#or (ifeq type 'oauth2') (ifeq type 'openIdConnect')}}
/**
* Authorize requests using an OAuth2 Bearer token value.
* Authorize requests using an OAuth2 Bearer token value for the security scheme "{{name}}".
*/
void authorize(java.lang.String bearerToken);
void authorize{{{__securitySchemeName}}}(java.lang.String bearerToken);

/**
* Returns the OAuth2 Bearer token value used to authorize requests.
* Returns the OAuth2 Bearer token value used to authorize requests for the security scheme "{{name}}".
*/
java.lang.String getAuthorization();
java.lang.String get{{{__securitySchemeName}}}Authorization();
{{/or}}

{{/each}}
{{/if}}
org.apache.cxf.jaxrs.client.Client client();
Expand Down

0 comments on commit 252baab

Please sign in to comment.