Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds JwtService and AclService to the API #4

Merged
merged 4 commits into from
Oct 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.vaibhav-sinha</groupId>
<artifactId>kong-java-client</artifactId>
<version>0.1.2-SNAPSHOT</version>
<version>0.1.3-SNAPSHOT</version>

<packaging>jar</packaging>

Expand Down Expand Up @@ -110,9 +110,9 @@
<version>${log4j.version}</version>
</dependency>
<!--<dependency>-->
<!--<groupId>ch.qos.logback</groupId>-->
<!--<artifactId>logback-classic</artifactId>-->
<!--<version>${logback.version}</version>-->
<!--<groupId>ch.qos.logback</groupId>-->
<!--<artifactId>logback-classic</artifactId>-->
<!--<version>${logback.version}</version>-->
<!--</dependency>-->

<dependency>
Expand Down Expand Up @@ -200,4 +200,4 @@
</plugins>
</build>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

/**
* Created by vaibhav on 16/06/17.
*
* Updated by dvilela on 17/10/17.
*/
public interface JwtService {
JwtCredential addCredentials(String consumerIdOrUsername, JwtCredential request);
void deleteCredentials(String consumerIdOrUsername, String id);
JwtCredentialList listCredentials(String consumerIdOrUsername);
JwtCredentialList listCredentials(String consumerIdOrUsername, Long size, String offset);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package com.github.vaibhavsinha.kong.api.plugin.authentication;

import com.github.vaibhavsinha.kong.model.plugin.authentication.key.KeyAuthCredential;
import com.github.vaibhavsinha.kong.model.plugin.authentication.key.KeyAuthCredentialList;

/**
* Created by vaibhav on 15/06/17.
*
* Updated by dvilela on 17/10/17.
*/
public interface KeyAuthService {
void addCredentials(String consumerIdOrUsername, String key);
KeyAuthCredential addCredentials(String consumerIdOrUsername, String key);
KeyAuthCredentialList listCredentials(String consumerIdOrUsername, Long size, String offset);
void deleteCredential(String consumerIdOrUsername, String id);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.github.vaibhavsinha.kong.api.plugin.security;

import com.github.vaibhavsinha.kong.model.plugin.security.acl.AclList;

/**
* Created by vaibhav on 18/06/17.
*
* Upated by dvilela on 22/10/17.
*/
public interface AclService {
void associateConsumer(String usernameOrId, String group);
AclList listAcls(String usernameOrId, Long size, String offset);
}
38 changes: 20 additions & 18 deletions src/main/java/com/github/vaibhavsinha/kong/impl/KongClient.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
package com.github.vaibhavsinha.kong.impl;

import com.github.vaibhavsinha.kong.api.admin.*;
import com.github.vaibhavsinha.kong.api.plugin.authentication.BasicAuthService;
import com.github.vaibhavsinha.kong.api.plugin.authentication.HmacAuthService;
import com.github.vaibhavsinha.kong.api.plugin.authentication.KeyAuthService;
import com.github.vaibhavsinha.kong.api.plugin.authentication.OAuth2ManageService;
import com.github.vaibhavsinha.kong.api.plugin.authentication.OAuth2ProcessService;
import com.github.vaibhavsinha.kong.api.plugin.authentication.*;
import com.github.vaibhavsinha.kong.api.plugin.security.AclService;
import com.github.vaibhavsinha.kong.impl.helper.RetrofitServiceCreator;
import com.github.vaibhavsinha.kong.impl.service.plugin.authentication.BasicAuthServiceImpl;
import com.github.vaibhavsinha.kong.impl.service.plugin.authentication.HmacAuthServiceImpl;
import com.github.vaibhavsinha.kong.impl.service.plugin.authentication.JwtAuthServiceImpl;
import com.github.vaibhavsinha.kong.impl.service.plugin.authentication.KeyAuthServiceImpl;
import com.github.vaibhavsinha.kong.impl.service.plugin.security.AclServiceImpl;
import com.github.vaibhavsinha.kong.internal.admin.*;
import com.github.vaibhavsinha.kong.internal.plugin.authentication.RetrofitBasicAuthService;
import com.github.vaibhavsinha.kong.internal.plugin.authentication.RetrofitHmacAuthService;
import com.github.vaibhavsinha.kong.internal.plugin.authentication.RetrofitKeyAuthService;
import com.github.vaibhavsinha.kong.internal.plugin.authentication.RetrofitOAuth2ManageService;
import com.github.vaibhavsinha.kong.internal.plugin.authentication.RetrofitOAuth2ProcessService;
import com.github.vaibhavsinha.kong.utils.HttpsUtil;
import com.github.vaibhavsinha.kong.internal.plugin.authentication.*;
import com.github.vaibhavsinha.kong.internal.plugin.security.RetrofitAclService;
import lombok.Data;
import okhttp3.OkHttpClient;

/**
* Created by vaibhav on 12/06/17.
*
* Updated by fanhua on 2017-08-07.
*
* Updated by dvilela on 17/10/17.
*/
@Data
public class KongClient {
Expand All @@ -43,10 +39,13 @@ public class KongClient {
private BasicAuthService basicAuthService;
private KeyAuthService keyAuthService;
private HmacAuthService hmacAuthService;
private JwtService jwtService;

private OAuth2ProcessService oAuth2ProcessService;
private OAuth2ManageService oAuth2ManageService;

private AclService aclService;


public KongClient(String adminUrl) {
this(adminUrl, null, false);
Expand All @@ -59,12 +58,13 @@ public KongClient(String adminUrl, String proxyUrl, boolean needOAuth2Support) {
throw new IllegalArgumentException("The adminUrl cannot be null or empty!");
}

if (proxyUrl == null || proxyUrl.isEmpty()) {
throw new IllegalArgumentException("The proxyUrl cannot be null or empty!");
}

if (needOAuth2Support && (!proxyUrl.startsWith("https://"))) {
throw new IllegalArgumentException("The proxyUrl must use https if you need OAuth2 support!");
if (needOAuth2Support) {
if (proxyUrl == null || proxyUrl.isEmpty()) {
throw new IllegalArgumentException("The proxyUrl cannot be null or empty!");
}
if (!proxyUrl.startsWith("https://")) {
throw new IllegalArgumentException("The proxyUrl must use https if you need OAuth2 support!");
}
}


Expand All @@ -89,6 +89,8 @@ public KongClient(String adminUrl, String proxyUrl, boolean needOAuth2Support) {
basicAuthService = new BasicAuthServiceImpl(retrofitServiceCreatorForAdminUrl.createRetrofitService(RetrofitBasicAuthService.class));
keyAuthService = new KeyAuthServiceImpl(retrofitServiceCreatorForAdminUrl.createRetrofitService(RetrofitKeyAuthService.class));
hmacAuthService = new HmacAuthServiceImpl(retrofitServiceCreatorForAdminUrl.createRetrofitService(RetrofitHmacAuthService.class));
jwtService = new JwtAuthServiceImpl(retrofitServiceCreatorForAdminUrl.createRetrofitService(RetrofitJwtService.class));
aclService = new AclServiceImpl(retrofitServiceCreatorForAdminUrl.createRetrofitService(RetrofitAclService.class));
}

if(needOAuth2Support) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.github.vaibhavsinha.kong.impl.service.plugin.authentication;

import com.github.vaibhavsinha.kong.api.plugin.authentication.JwtService;
import com.github.vaibhavsinha.kong.exception.KongClientException;
import com.github.vaibhavsinha.kong.internal.plugin.authentication.RetrofitJwtService;
import com.github.vaibhavsinha.kong.model.plugin.authentication.jwt.JwtCredential;
import com.github.vaibhavsinha.kong.model.plugin.authentication.jwt.JwtCredentialList;

import java.io.IOException;

/**
* Created by dvilela on 10/16/17.
*/
public class JwtAuthServiceImpl implements JwtService {

private RetrofitJwtService retrofitJwtService;

public JwtAuthServiceImpl(RetrofitJwtService retrofitJwtService) {
this.retrofitJwtService = retrofitJwtService;
}

@Override
public JwtCredential addCredentials(String consumerIdOrUsername, JwtCredential request) {
try {
return retrofitJwtService.addCredentials(consumerIdOrUsername, request).execute().body();
} catch (IOException e) {
throw new KongClientException(e.getMessage());
}
}

@Override
public void deleteCredentials(String consumerIdOrUsername, String id) {
try {
retrofitJwtService.deleteCredentials(consumerIdOrUsername, id).execute();
} catch (IOException e) {
throw new KongClientException(e.getMessage());
}
}

@Override
public JwtCredentialList listCredentials(String consumerIdOrUsername, Long size, String offset) {
try {
return retrofitJwtService.listCredentials(consumerIdOrUsername, size, offset).execute().body();
} catch (IOException e) {
throw new KongClientException(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
import com.github.vaibhavsinha.kong.exception.KongClientException;
import com.github.vaibhavsinha.kong.internal.plugin.authentication.RetrofitKeyAuthService;
import com.github.vaibhavsinha.kong.model.plugin.authentication.key.KeyAuthCredential;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import com.github.vaibhavsinha.kong.model.plugin.authentication.key.KeyAuthCredentialList;

import java.io.IOException;

/**
* Created by vaibhav on 15/06/17.
*
* Updated by fanhua on 2017-08-07.
*
* Updated by dvilela on 17/10/17.
*/
public class KeyAuthServiceImpl implements KeyAuthService {

Expand All @@ -23,9 +24,28 @@ public KeyAuthServiceImpl(RetrofitKeyAuthService retrofitKeyAuthService) {
}

@Override
public void addCredentials(String consumerIdOrUsername, String key) {
public KeyAuthCredential addCredentials(String consumerIdOrUsername, String key) {
try {
return retrofitKeyAuthService.addCredentials(consumerIdOrUsername, new KeyAuthCredential(key)).execute()
.body();
} catch (IOException e) {
throw new KongClientException(e.getMessage());
}
}

@Override
public KeyAuthCredentialList listCredentials(String consumerIdOrUsername, Long size, String offset) {
try {
return retrofitKeyAuthService.listCredentials(consumerIdOrUsername, size, offset).execute().body();
} catch (IOException e) {
throw new KongClientException(e.getMessage());
}
}

@Override
public void deleteCredential(String consumerIdOrUsername, String id) {
try {
retrofitKeyAuthService.addCredentials(consumerIdOrUsername, new KeyAuthCredential(key)).execute();
retrofitKeyAuthService.deleteCredential(consumerIdOrUsername, id).execute();
} catch (IOException e) {
throw new KongClientException(e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
import com.github.vaibhavsinha.kong.exception.KongClientException;
import com.github.vaibhavsinha.kong.internal.plugin.security.RetrofitAclService;
import com.github.vaibhavsinha.kong.model.plugin.security.acl.Acl;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import com.github.vaibhavsinha.kong.model.plugin.security.acl.AclList;

import java.io.IOException;

/**
* Created by vaibhav on 18/06/17.
*
* Updated by fanhua on 2017-08-07.
*
* Upated by dvilela on 22/10/17.
*/
public class AclServiceImpl implements AclService {

Expand All @@ -29,4 +30,13 @@ public void associateConsumer(String usernameOrId, String group) {
throw new KongClientException(e.getMessage());
}
}

@Override
public AclList listAcls(String usernameOrId, Long size, String offset) {
try {
return retrofitAclService.listAcls(usernameOrId, size, offset).execute().body();
} catch (IOException e) {
throw new KongClientException(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

/**
* Created by vaibhav on 16/06/17.
*
* Updated by dvilela on 17/10/17.
*/
public interface RetrofitJwtService {

Expand All @@ -17,5 +19,5 @@ public interface RetrofitJwtService {
Call<Void> deleteCredentials(@Path("consumer") String consumerIdOrUsername, @Path("id") String id);

@GET("consumers/{consumer}/jwt")
Call<JwtCredentialList> listCredentials(@Path("consumer") String consumerIdOrUsername);
Call<JwtCredentialList> listCredentials(@Path("consumer") String consumerIdOrUsername, @Query("size") Long size, @Query("offset") String offset);
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package com.github.vaibhavsinha.kong.internal.plugin.authentication;

import com.github.vaibhavsinha.kong.model.plugin.authentication.basic.BasicAuthCredential;
import com.github.vaibhavsinha.kong.model.plugin.authentication.key.KeyAuthCredential;
import com.github.vaibhavsinha.kong.model.plugin.authentication.key.KeyAuthCredentialList;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;
import retrofit2.http.Path;
import retrofit2.http.*;

/**
* Created by vaibhav on 15/06/17.
*
* Updated by dvilela on 17/10/17.
*/
public interface RetrofitKeyAuthService {

@POST("consumers/{id}/key-auth")
Call<BasicAuthCredential> addCredentials(@Path("id") String consumerIdOrUsername, @Body KeyAuthCredential request);
Call<KeyAuthCredential> addCredentials(@Path("id") String consumerIdOrUsername, @Body KeyAuthCredential request);

@GET("consumers/{id}/key-auth")
Call<KeyAuthCredentialList> listCredentials(@Path("id") String consumerIdOrUsername, @Query("size") Long size, @Query("offset") String offset);

@DELETE("consumers/{consumer}/key-auth/{id}")
Call<Void> deleteCredential(@Path("consumer") String consumer, @Path("id") String id);
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package com.github.vaibhavsinha.kong.internal.plugin.security;

import com.github.vaibhavsinha.kong.model.plugin.security.acl.Acl;
import com.github.vaibhavsinha.kong.model.plugin.security.acl.AclList;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;
import retrofit2.http.Path;
import retrofit2.http.*;

/**
* Created by vaibhav on 18/06/17.
*
* Upated by dvilela on 22/10/17.
*/
public interface RetrofitAclService {
@POST("consumers/{id}/acls")
Call<Void> associateConsumer(@Path("id") String consumerIdOrUsername, @Body Acl request);

@GET("consumers/{id}/acls")
Call<AclList> listAcls(@Path("id") String consumerIdOrUsername, @Query("size") Long size, @Query("offset") String offset);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

/**
* Created by vaibhav on 16/06/17.
*
* Updated by dvilela on 17/10/17.
*/
@Data
@NoArgsConstructor
Expand All @@ -18,7 +20,7 @@ public class JwtCredential {
@SerializedName("id")
private String id;
@SerializedName("created_at")
private Integer createdAt;
private Long createdAt;
@SerializedName("key")
private String key;
@SerializedName("algorithm")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.github.vaibhavsinha.kong.model.plugin.authentication.jwt;

import com.github.vaibhavsinha.kong.model.common.AbstractEntityList;
import lombok.Data;

import java.util.List;

/**
* Created by vaibhav on 16/06/17.
*/
@Data
public class JwtCredentialList extends AbstractEntityList {
Long total;
List<JwtCredential> data;
Expand Down
Loading