-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into ian-UID2-1974-back-fill-site-created
- Loading branch information
Showing
23 changed files
with
545 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/java/com/uid2/admin/vertx/api/IBlockingRouteProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.uid2.admin.vertx.api; | ||
|
||
/* | ||
*Important* | ||
If you implement this interface, your route will be registered as a blocking handler. Use IRouteProvider | ||
instead if you want to provide a non-blocking handler. See `readme.md` for more information. | ||
*/ | ||
public interface IBlockingRouteProvider extends IRouteProvider { | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/uid2/admin/vertx/api/IRouteProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.uid2.admin.vertx.api; | ||
|
||
import io.vertx.core.Handler; | ||
import io.vertx.ext.web.Router; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
/* | ||
*Important* | ||
If you implement this interface, your route will be registered as a non-blocking handler. Use IBlockingRouteProvider | ||
instead if you want to provide a blocking handler. See `readme.md` for more information. | ||
*/ | ||
public interface IRouteProvider { | ||
Handler<RoutingContext> getHandler(); | ||
} | ||
|
||
|
18 changes: 18 additions & 0 deletions
18
src/main/java/com/uid2/admin/vertx/api/UrlParameterProviders.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.uid2.admin.vertx.api; | ||
|
||
import io.vertx.core.Handler; | ||
import io.vertx.ext.web.RoutingContext; | ||
import lombok.val; | ||
|
||
public class UrlParameterProviders { | ||
@FunctionalInterface | ||
public interface ISiteIdRouteHandler { | ||
void handle(RoutingContext rc, int siteId); | ||
} | ||
public static Handler<RoutingContext> provideSiteId(ISiteIdRouteHandler handler) { | ||
return (RoutingContext rc) -> { | ||
val siteId = Integer.parseInt(rc.pathParam("siteId")); | ||
handler.handle(rc, siteId); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.uid2.admin.vertx.api; | ||
|
||
import com.uid2.admin.vertx.api.annotations.Method; | ||
import com.uid2.admin.vertx.api.annotations.Path; | ||
import com.uid2.admin.vertx.api.annotations.Roles; | ||
import com.uid2.shared.middleware.AuthMiddleware; | ||
import io.vertx.core.Vertx; | ||
import io.vertx.ext.web.Router; | ||
import lombok.val; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Arrays; | ||
import java.util.Set; | ||
|
||
public class V2Router { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(V2Router.class); | ||
private final IRouteProvider[] routeProviders; | ||
private final AuthMiddleware auth; | ||
|
||
public V2Router(IRouteProvider[] routeProviders, AuthMiddleware auth) { | ||
this.routeProviders = routeProviders; | ||
this.auth = auth; | ||
} | ||
|
||
public void setupSubRouter(Vertx verticle, Router parentRouter) { | ||
val v2router = Router.router(verticle); | ||
|
||
for (IRouteProvider provider : routeProviders) { | ||
LOGGER.info("Configuring v2 router with " + provider.getClass()); | ||
java.lang.reflect.Method handler = null; | ||
try { | ||
handler = provider.getClass().getMethod("getHandler"); | ||
val path = handler.getAnnotation(Path.class).value(); | ||
val method = handler.getAnnotation(Method.class).value().vertxMethod; | ||
val roles = handler.getAnnotation(Roles.class).value(); | ||
val authWrappedHandler = auth.handle(provider.getHandler(), roles); | ||
if (Arrays.stream(provider.getClass().getInterfaces()).anyMatch(iface -> iface == IBlockingRouteProvider.class)) { | ||
LOGGER.info("Using blocking handler for " + provider.getClass().getName()); | ||
v2router.route(method, path).blockingHandler(authWrappedHandler); | ||
} | ||
else { | ||
LOGGER.info("Using non-blocking handler for " + provider.getClass().getName()); | ||
v2router.route(method, path).handler(authWrappedHandler); | ||
} | ||
} catch (NoSuchMethodException e) { | ||
LOGGER.error("Could not find handle method for API handler: " + provider.getClass().getName()); | ||
} | ||
} | ||
|
||
parentRouter.route("/api/v2/*").subRouter(v2router); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/uid2/admin/vertx/api/V2RouterModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.uid2.admin.vertx.api; | ||
|
||
import com.uid2.admin.secret.IKeypairManager; | ||
import com.uid2.admin.vertx.api.cstg.GetClientSideKeypairsBySite; | ||
import com.uid2.shared.middleware.AuthMiddleware; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class V2RouterModule { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(V2RouterModule.class); | ||
|
||
private final IKeypairManager keypairManager; | ||
private final AuthMiddleware authMiddleware; | ||
|
||
public V2RouterModule(IKeypairManager keypairManager, AuthMiddleware authMiddleware) { | ||
this.keypairManager = keypairManager; | ||
this.authMiddleware = authMiddleware; | ||
} | ||
|
||
protected IRouteProvider[] getRouteProviders() { | ||
return new IRouteProvider[] { | ||
new GetClientSideKeypairsBySite(keypairManager) | ||
}; | ||
} | ||
|
||
public V2Router getRouter() { | ||
return new V2Router(getRouteProviders(), authMiddleware); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/uid2/admin/vertx/api/annotations/ApiMethod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.uid2.admin.vertx.api.annotations; | ||
|
||
import io.vertx.core.http.HttpMethod; | ||
|
||
public enum ApiMethod { | ||
GET(HttpMethod.GET); | ||
public final HttpMethod vertxMethod; | ||
|
||
ApiMethod(HttpMethod vertxMethod) { | ||
this.vertxMethod = vertxMethod; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/uid2/admin/vertx/api/annotations/Method.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.uid2.admin.vertx.api.annotations; | ||
|
||
import io.vertx.core.http.HttpMethod; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Method { | ||
ApiMethod value(); | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/uid2/admin/vertx/api/annotations/Path.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.uid2.admin.vertx.api.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Path { | ||
String value(); | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/uid2/admin/vertx/api/annotations/Roles.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.uid2.admin.vertx.api.annotations; | ||
|
||
import com.uid2.shared.auth.Role; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Roles { | ||
Role[] value(); | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/uid2/admin/vertx/api/cstg/ClientSideKeypairResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.uid2.admin.vertx.api.cstg; | ||
|
||
import com.uid2.shared.model.ClientSideKeypair; | ||
import lombok.AllArgsConstructor; | ||
|
||
import java.time.Instant; | ||
|
||
@AllArgsConstructor | ||
public class ClientSideKeypairResponse { | ||
public final int siteId; | ||
public final String subscriptionId; | ||
public final String publicKey; | ||
public final Instant created; | ||
public final boolean disabled; | ||
|
||
static ClientSideKeypairResponse fromClientSiteKeypair(ClientSideKeypair keypair) { | ||
return new ClientSideKeypairResponse(keypair.getSiteId(), keypair.getSubscriptionId(), keypair.encodePublicKeyToString(), keypair.getCreated(), keypair.isDisabled()); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/com/uid2/admin/vertx/api/cstg/GetClientSideKeypairsBySite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.uid2.admin.vertx.api.cstg; | ||
|
||
import com.google.common.collect.Streams; | ||
import com.uid2.admin.secret.IKeypairManager; | ||
import com.uid2.admin.vertx.ResponseUtil; | ||
import com.uid2.admin.vertx.api.IRouteProvider; | ||
import com.uid2.admin.vertx.api.UrlParameterProviders; | ||
import com.uid2.admin.vertx.api.annotations.ApiMethod; | ||
import com.uid2.admin.vertx.api.annotations.Method; | ||
import com.uid2.admin.vertx.api.annotations.Path; | ||
import com.uid2.admin.vertx.api.annotations.Roles; | ||
import com.uid2.shared.auth.Role; | ||
import io.vertx.core.Handler; | ||
import io.vertx.ext.web.RoutingContext; | ||
import lombok.val; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class GetClientSideKeypairsBySite implements IRouteProvider { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(GetClientSideKeypairsBySite.class); | ||
|
||
private final IKeypairManager keypairManager; | ||
|
||
public GetClientSideKeypairsBySite(IKeypairManager keypairManager) { | ||
this.keypairManager = keypairManager; | ||
} | ||
|
||
@Path("/sites/:siteId/client-side-keypairs") | ||
@Method(ApiMethod.GET) | ||
@Roles({Role.ADMINISTRATOR, Role.SHARING_PORTAL}) | ||
public Handler<RoutingContext> getHandler() { | ||
return UrlParameterProviders.provideSiteId(this::handleGetClientSideKeys); | ||
} | ||
|
||
public void handleGetClientSideKeys(RoutingContext rc, int siteId) { | ||
val keypairs = keypairManager.getKeypairsBySite(siteId); | ||
if (keypairs != null) { | ||
val result = Streams.stream(keypairs) | ||
.map(kp -> ClientSideKeypairResponse.fromClientSiteKeypair(kp)) | ||
.toArray(ClientSideKeypairResponse[]::new); | ||
rc.json(result); | ||
} | ||
else { | ||
ResponseUtil.error(rc, 404, "No keypairs available for site ID: " + siteId); | ||
} | ||
} | ||
} |
Oops, something went wrong.