Search in sources :

Example 6 with AuthProvider

use of io.vertx.ext.auth.AuthProvider in project vertx-auth by vert-x3.

the class AuthJWTExamples method example8.

public void example8(Vertx vertx) {
    JWTAuthOptions config = new JWTAuthOptions().addPubSecKey(new PubSecKeyOptions().setAlgorithm("RS256").setPublicKey("BASE64-ENCODED-PUBLIC_KEY"));
    AuthProvider provider = JWTAuth.create(vertx, config);
}
Also used : JWTAuthOptions(io.vertx.ext.auth.jwt.JWTAuthOptions) PubSecKeyOptions(io.vertx.ext.auth.PubSecKeyOptions) AuthProvider(io.vertx.ext.auth.AuthProvider)

Example 7 with AuthProvider

use of io.vertx.ext.auth.AuthProvider in project vertx-examples by vert-x3.

the class Server method start.

@Override
public void start() throws Exception {
    // quick load of test data, this is a *sync* helper not intended for
    // real deployments...
    setUpInitialData("jdbc:hsqldb:mem:test?shutdown=true");
    // Create a JDBC client with a test database
    JDBCClient client = JDBCClient.createShared(vertx, new JsonObject().put("url", "jdbc:hsqldb:mem:test?shutdown=true").put("driver_class", "org.hsqldb.jdbcDriver"));
    // If you are planning NOT to build a fat jar, then use the BoneCP pool since it
    // can handle loading the jdbc driver classes from outside vert.x lib directory
    // JDBCClient client = JDBCClient.createShared(vertx, new JsonObject()
    // .put("provider_class", "io.vertx.ext.jdbc.spi.impl.BoneCPDataSourceProvider")
    // .put("jdbcUrl", "jdbc:hsqldb:mem:test?shutdown=true")
    // .put("username", "sa")
    // .put("password", ""));
    Router router = Router.router(vertx);
    // We need cookies, sessions and request bodies
    router.route().handler(CookieHandler.create());
    router.route().handler(BodyHandler.create());
    router.route().handler(SessionHandler.create(LocalSessionStore.create(vertx)));
    // Simple auth service which uses a JDBC data source
    AuthProvider authProvider = JDBCAuth.create(vertx, client);
    // We need a user session handler too to make sure the user is stored in the session between requests
    router.route().handler(UserSessionHandler.create(authProvider));
    // Any requests to URI starting '/private/' require login
    router.route("/private/*").handler(RedirectAuthHandler.create(authProvider, "/loginpage.html"));
    // Serve the static private pages from directory 'private'
    router.route("/private/*").handler(StaticHandler.create().setCachingEnabled(false).setWebRoot("private"));
    // Handles the actual login
    router.route("/loginhandler").handler(FormLoginHandler.create(authProvider));
    // Implement logout
    router.route("/logout").handler(context -> {
        context.clearUser();
        // Redirect back to the index page
        context.response().putHeader("location", "/").setStatusCode(302).end();
    });
    // Serve the non private static pages
    router.route().handler(StaticHandler.create());
    vertx.createHttpServer().requestHandler(router).listen(8080);
}
Also used : JDBCClient(io.vertx.ext.jdbc.JDBCClient) JsonObject(io.vertx.core.json.JsonObject) Router(io.vertx.ext.web.Router) AuthProvider(io.vertx.ext.auth.AuthProvider)

Example 8 with AuthProvider

use of io.vertx.ext.auth.AuthProvider in project vertx-web by vert-x3.

the class ChainAuthHandlerTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    JsonObject authConfig = new JsonObject().put("properties_path", "classpath:login/loginusers.properties");
    AuthProvider authProvider = ShiroAuth.create(vertx, ShiroAuthRealmType.PROPERTIES, authConfig);
    // create a chain
    chain = ChainAuthHandler.create();
    chain.append(JWTAuthHandler.create(null)).append(BasicAuthHandler.create(authProvider)).append(RedirectAuthHandler.create(authProvider));
    router.route().handler(SessionHandler.create(LocalSessionStore.create(vertx)));
    router.route().handler(chain);
    router.route().handler(ctx -> ctx.response().end());
}
Also used : JsonObject(io.vertx.core.json.JsonObject) AuthProvider(io.vertx.ext.auth.AuthProvider)

Example 9 with AuthProvider

use of io.vertx.ext.auth.AuthProvider in project vertx-web by vert-x3.

the class EventbusBridgeTest method testSendRequiresAuthorityHasAuthority.

@Test
public void testSendRequiresAuthorityHasAuthority() throws Exception {
    sockJSHandler.bridge(defaultOptions.addInboundPermitted(new PermittedOptions().setAddress(addr).setRequiredAuthority("bang_sticks")));
    router.clear();
    router.route().handler(CookieHandler.create());
    SessionStore store = LocalSessionStore.create(vertx);
    router.route().handler(SessionHandler.create(store));
    JsonObject authConfig = new JsonObject().put("properties_path", "classpath:login/loginusers.properties");
    AuthProvider authProvider = ShiroAuth.create(vertx, ShiroAuthRealmType.PROPERTIES, authConfig);
    addLoginHandler(router, authProvider);
    router.route("/eventbus/*").handler(sockJSHandler);
    testSend("foo");
}
Also used : LocalSessionStore(io.vertx.ext.web.sstore.LocalSessionStore) SessionStore(io.vertx.ext.web.sstore.SessionStore) JsonObject(io.vertx.core.json.JsonObject) AuthProvider(io.vertx.ext.auth.AuthProvider) PermittedOptions(io.vertx.ext.bridge.PermittedOptions) Test(org.junit.Test)

Example 10 with AuthProvider

use of io.vertx.ext.auth.AuthProvider in project vertx-web by vert-x3.

the class EventbusBridgeTest method testSendRequiresAuthorityHasnotAuthority.

@Test
public void testSendRequiresAuthorityHasnotAuthority() throws Exception {
    sockJSHandler.bridge(defaultOptions.addInboundPermitted(new PermittedOptions().setAddress(addr).setRequiredAuthority("pick_nose")));
    router.clear();
    router.route().handler(CookieHandler.create());
    SessionStore store = LocalSessionStore.create(vertx);
    router.route().handler(SessionHandler.create(store));
    JsonObject authConfig = new JsonObject().put("properties_path", "classpath:login/loginusers.properties");
    AuthProvider authProvider = ShiroAuth.create(vertx, ShiroAuthRealmType.PROPERTIES, authConfig);
    addLoginHandler(router, authProvider);
    router.route("/eventbus/*").handler(sockJSHandler);
    testError(new JsonObject().put("type", "send").put("address", addr).put("body", "foo"), "access_denied");
}
Also used : LocalSessionStore(io.vertx.ext.web.sstore.LocalSessionStore) SessionStore(io.vertx.ext.web.sstore.SessionStore) JsonObject(io.vertx.core.json.JsonObject) AuthProvider(io.vertx.ext.auth.AuthProvider) PermittedOptions(io.vertx.ext.bridge.PermittedOptions) Test(org.junit.Test)

Aggregations

AuthProvider (io.vertx.ext.auth.AuthProvider)17 JsonObject (io.vertx.core.json.JsonObject)15 SessionStore (io.vertx.ext.web.sstore.SessionStore)7 Test (org.junit.Test)7 HttpMethod (io.vertx.core.http.HttpMethod)5 ShiroAuth (io.vertx.ext.auth.shiro.ShiroAuth)5 RoutingContext (io.vertx.ext.web.RoutingContext)5 AsyncResult (io.vertx.core.AsyncResult)4 Future (io.vertx.core.Future)4 Handler (io.vertx.core.Handler)4 Buffer (io.vertx.core.buffer.Buffer)4 ClusterSerializable (io.vertx.core.shareddata.impl.ClusterSerializable)4 PRNG (io.vertx.ext.auth.PRNG)4 ShiroAuthRealmType (io.vertx.ext.auth.shiro.ShiroAuthRealmType)4 Router (io.vertx.ext.web.Router)4 Session (io.vertx.ext.web.Session)4 SessionImpl (io.vertx.ext.web.sstore.impl.SessionImpl)4 Map (java.util.Map)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4