Skip to content

Instantly share code, notes, and snippets.

@pendula95
Created May 10, 2024 14:39
Show Gist options
  • Save pendula95/0460494f9f6bacaf6ec8e5c63473fe70 to your computer and use it in GitHub Desktop.
Save pendula95/0460494f9f6bacaf6ec8e5c63473fe70 to your computer and use it in GitHub Desktop.
import io.reactivex.rxjava3.core.Completable;
import io.reactivex.rxjava3.core.Single;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;
import io.vertx.ext.auth.oauth2.OAuth2Options;
import io.vertx.ext.web.client.WebClientOptions;
import io.vertx.rxjava3.core.AbstractVerticle;
import io.vertx.rxjava3.ext.auth.oauth2.OAuth2Auth;
import io.vertx.rxjava3.ext.web.Router;
import io.vertx.rxjava3.ext.web.client.WebClient;
import io.vertx.rxjava3.ext.web.handler.BodyHandler;
import io.vertx.rxjava3.ext.web.handler.OAuth2AuthHandler;
import java.util.List;
public class ScopeTest extends AbstractVerticle {
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
vertx.deployVerticle(new ScopeTest());
}
@Override
public void start(Promise<Void> startFuture) throws Exception {
Router router = Router.router(vertx);
router.route().handler(BodyHandler.create());
OAuth2Auth authProvider = OAuth2Auth.create(vertx, new OAuth2Options()
.setClientId("test-client-id")
.setClientSecret("test-client-secret")
.setSite("http://www.localhost:10000")
.setAuthorizationPath("/oauth")
.setTokenPath("/api/oauth/token"));
OAuth2AuthHandler oauth2Rx = OAuth2AuthHandler
.create(vertx, authProvider, "http://localhost:9999/callbackrx")
.setupCallback(router.get("/callbackrx"))
.withScopes(List.of("read"));
io.vertx.ext.web.handler.OAuth2AuthHandler oauth2 = io.vertx.ext.web.handler.OAuth2AuthHandler
.create(vertx.getDelegate(), authProvider.getDelegate(), "http://localhost:9999/callback")
.setupCallback(router.getDelegate().get("/callback"))
.withScope("read");
router.route("/protectedrx/*")
.handler(oauth2Rx);
router.getDelegate().route("/protected/*")
.handler(oauth2);
router.route("/protected/somepage")
.handler(ctx -> ctx.response().end("Welcome to the protected resource!"));
router.route("/protectedrx/somepage")
.handler(ctx -> ctx.response().end("Welcome to the protected resource!"));
WebClient webClient = WebClient.create(vertx, new WebClientOptions().setFollowRedirects(false));
vertx.createHttpServer().requestHandler(router).rxListen(Integer.parseInt(System.getProperty("http.port", "9999")))
.ignoreElement()
.andThen(Single.defer(() -> {
return webClient.getAbs("http://localhost:9999/protectedrx/somepage")
.rxSend();
}))
.doOnSuccess(response -> {
String header = response.getHeader("Location");
System.out.println(header);
})
.ignoreElement()
.andThen(Single.defer(() -> {
return webClient.getAbs("http://localhost:9999/protected/somepage")
.rxSend();
}))
.doOnSuccess(response -> {
String header = response.getHeader("Location");
System.out.println(header);
})
.ignoreElement()
.subscribe(startFuture::complete, startFuture::fail);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment