Search in sources :

Example 1 with Filter

use of spark.Filter in project spark by perwendel.

the class FilterExample method main.

public static void main(String[] args) {
    usernamePasswords.put("foo", "bar");
    usernamePasswords.put("admin", "admin");
    before(new Filter() {

        @Override
        public void handle(Request request, Response response) {
            String user = request.queryParams("user");
            String password = request.queryParams("password");
            String dbPassword = usernamePasswords.get(user);
            if (!(password != null && password.equals(dbPassword))) {
                halt(401, "You are not welcome here!!!");
            }
        }
    });
    before("/hello", (request, response) -> {
        response.header("Foo", "Set by second before filter");
    });
    get("/hello", (request, response) -> {
        return "Hello World!";
    });
    after("/hello", (request, response) -> {
        response.header("spark", "added by after-filter");
    });
}
Also used : Response(spark.Response) Filter(spark.Filter) Request(spark.Request)

Example 2 with Filter

use of spark.Filter in project waltz by khartec.

the class AuthenticationEndpoint method instantiateFilter.

private Optional<Filter> instantiateFilter(String className) {
    try {
        LOG.info("Setting authentication filter to: " + className);
        Filter filter = (Filter) Class.forName(className).getConstructor(SettingsService.class).newInstance(settingsService);
        return Optional.of(filter);
    } catch (Exception e) {
        LOG.error("Cannot instantiate authentication filter class: " + className, e);
        return Optional.empty();
    }
}
Also used : Filter(spark.Filter)

Example 3 with Filter

use of spark.Filter in project waltz by khartec.

the class AuthenticationEndpoint method register.

@Override
public void register() {
    post(mkPath(BASE_URL, "login"), (request, response) -> {
        LoginRequest login = readBody(request, LoginRequest.class);
        if (userService.authenticate(login)) {
            Algorithm algorithmHS = Algorithm.HMAC512(JWTUtilities.SECRET);
            String[] roles = userRoleService.getUserRoles(login.userName()).stream().map(r -> r.name()).toArray(size -> new String[size]);
            String token = JWT.create().withIssuer(JWTUtilities.ISSUER).withSubject(login.userName()).withArrayClaim("roles", roles).withClaim("displayName", login.userName()).withClaim("employeeId", login.userName()).sign(algorithmHS);
            return newHashMap("token", token);
        } else {
            response.status(401);
            return "Unknown user/password";
        }
    }, transformer);
    before(mkPath("api", "*"), filter);
}
Also used : Endpoint(com.khartec.waltz.web.endpoints.Endpoint) JWT(com.auth0.jwt.JWT) UserService(com.khartec.waltz.service.user.UserService) Logger(org.slf4j.Logger) UserRoleService(com.khartec.waltz.service.user.UserRoleService) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Spark.post(spark.Spark.post) Supplier(java.util.function.Supplier) SettingsService(com.khartec.waltz.service.settings.SettingsService) NamedSettings(com.khartec.waltz.model.settings.NamedSettings) Algorithm(com.auth0.jwt.algorithms.Algorithm) Service(org.springframework.stereotype.Service) LoginRequest(com.khartec.waltz.model.user.LoginRequest) Optional(java.util.Optional) Filter(spark.Filter) MapUtilities.newHashMap(com.khartec.waltz.common.MapUtilities.newHashMap) Spark.before(spark.Spark.before) WebUtilities(com.khartec.waltz.web.WebUtilities) LoginRequest(com.khartec.waltz.model.user.LoginRequest) Algorithm(com.auth0.jwt.algorithms.Algorithm)

Example 4 with Filter

use of spark.Filter in project waltz by khartec.

the class AuthenticationEndpoint method instantiateFilter.

private Optional<Filter> instantiateFilter(String className) {
    try {
        LOG.info("Setting authentication filter to: " + className);
        Filter filter = (Filter) Class.forName(className).getConstructor(SettingsService.class).newInstance(settingsService);
        return Optional.of(filter);
    } catch (Exception e) {
        LOG.error("Cannot instantiate authentication filter class: " + className, e);
        return Optional.empty();
    }
}
Also used : Filter(spark.Filter)

Aggregations

Filter (spark.Filter)4 JWT (com.auth0.jwt.JWT)1 Algorithm (com.auth0.jwt.algorithms.Algorithm)1 MapUtilities.newHashMap (com.khartec.waltz.common.MapUtilities.newHashMap)1 NamedSettings (com.khartec.waltz.model.settings.NamedSettings)1 LoginRequest (com.khartec.waltz.model.user.LoginRequest)1 SettingsService (com.khartec.waltz.service.settings.SettingsService)1 UserRoleService (com.khartec.waltz.service.user.UserRoleService)1 UserService (com.khartec.waltz.service.user.UserService)1 WebUtilities (com.khartec.waltz.web.WebUtilities)1 Endpoint (com.khartec.waltz.web.endpoints.Endpoint)1 Optional (java.util.Optional)1 Supplier (java.util.function.Supplier)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Service (org.springframework.stereotype.Service)1 Request (spark.Request)1 Response (spark.Response)1 Spark.before (spark.Spark.before)1