Search in sources :

Example 1 with AuthoritiesImpl

use of org.eclipse.hono.auth.AuthoritiesImpl in project hono by eclipse.

the class FileBasedAuthenticationService method toAuthorities.

private Authorities toAuthorities(final JsonArray authorities) {
    final AuthoritiesImpl result = new AuthoritiesImpl();
    authorities.stream().filter(JsonObject.class::isInstance).map(JsonObject.class::cast).forEach(authSpec -> {
        final JsonArray activities = authSpec.getJsonArray(FIELD_ACTIVITIES, new JsonArray());
        final String resource = authSpec.getString(FIELD_RESOURCE);
        final String operation = authSpec.getString(FIELD_OPERATION);
        if (resource != null) {
            final List<Activity> activityList = activities.stream().filter(String.class::isInstance).map(String.class::cast).map(Activity::valueOf).collect(Collectors.toList());
            result.addResource(resource, activityList.toArray(Activity[]::new));
        } else if (operation != null) {
            final String[] parts = operation.split(":", 2);
            if (parts.length == 2) {
                result.addOperation(parts[0], parts[1]);
            } else {
                log.debug("ignoring malformed operation spec [{}], operation name missing", operation);
            }
        } else {
            throw new IllegalArgumentException("malformed authorities");
        }
    });
    return result;
}
Also used : JsonArray(io.vertx.core.json.JsonArray) AuthoritiesImpl(org.eclipse.hono.auth.AuthoritiesImpl) JsonObject(io.vertx.core.json.JsonObject) Activity(org.eclipse.hono.auth.Activity)

Example 2 with AuthoritiesImpl

use of org.eclipse.hono.auth.AuthoritiesImpl in project hono by eclipse.

the class FileBasedAuthenticationService method getAuthorities.

private Authorities getAuthorities(final JsonObject user) {
    AuthoritiesImpl result = new AuthoritiesImpl();
    user.getJsonArray(FIELD_AUTHORITIES).forEach(obj -> {
        final String authority = (String) obj;
        Authorities roleAuthorities = roles.get(authority);
        if (roleAuthorities != null) {
            result.addAll(roleAuthorities);
        }
    });
    return result;
}
Also used : AuthoritiesImpl(org.eclipse.hono.auth.AuthoritiesImpl) Authorities(org.eclipse.hono.auth.Authorities)

Example 3 with AuthoritiesImpl

use of org.eclipse.hono.auth.AuthoritiesImpl in project hono by eclipse.

the class FileBasedAuthenticationService method toAuthorities.

private Authorities toAuthorities(final JsonArray authorities) {
    AuthoritiesImpl result = new AuthoritiesImpl();
    Objects.requireNonNull(authorities).stream().filter(obj -> obj instanceof JsonObject).forEach(obj -> {
        final JsonObject authSpec = (JsonObject) obj;
        final JsonArray activities = authSpec.getJsonArray(FIELD_ACTIVITIES, new JsonArray());
        final String resource = authSpec.getString(FIELD_RESOURCE);
        final String operation = authSpec.getString(FIELD_OPERATION);
        if (resource != null) {
            List<Activity> activityList = new ArrayList<>();
            activities.forEach(s -> {
                Activity act = Activity.valueOf((String) s);
                if (act != null) {
                    activityList.add(act);
                }
            });
            result.addResource(resource, activityList.toArray(new Activity[activityList.size()]));
        } else if (operation != null) {
            String[] parts = operation.split(":", 2);
            if (parts.length == 2) {
                result.addOperation(parts[0], parts[1]);
            } else {
                log.debug("ignoring malformed operation spec [{}], operation name missing", operation);
            }
        } else {
            throw new IllegalArgumentException("malformed authorities");
        }
    });
    return result;
}
Also used : AuthTokenHelper(org.eclipse.hono.service.auth.AuthTokenHelper) AuthoritiesImpl(org.eclipse.hono.auth.AuthoritiesImpl) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) HonoUser(org.eclipse.hono.auth.HonoUser) ArrayList(java.util.ArrayList) AuthenticationConstants(org.eclipse.hono.service.auth.AuthenticationConstants) Service(org.springframework.stereotype.Service) Map(java.util.Map) Qualifier(org.springframework.beans.factory.annotation.Qualifier) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Resource(org.springframework.core.io.Resource) UTF_8(java.nio.charset.StandardCharsets.UTF_8) IOException(java.io.IOException) Reader(java.io.Reader) Instant(java.time.Instant) Activity(org.eclipse.hono.auth.Activity) InputStreamReader(java.io.InputStreamReader) Profile(org.springframework.context.annotation.Profile) Future(io.vertx.core.Future) FileNotFoundException(java.io.FileNotFoundException) Objects(java.util.Objects) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) AbstractHonoAuthenticationService(org.eclipse.hono.service.auth.AbstractHonoAuthenticationService) Handler(io.vertx.core.Handler) Authorities(org.eclipse.hono.auth.Authorities) JsonArray(io.vertx.core.json.JsonArray) AuthoritiesImpl(org.eclipse.hono.auth.AuthoritiesImpl) ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) Activity(org.eclipse.hono.auth.Activity)

Example 4 with AuthoritiesImpl

use of org.eclipse.hono.auth.AuthoritiesImpl in project hono by eclipse.

the class AuthTokenHelperImplTest method testCreateAndExpandToken.

/**
 * Verifies that the helper can create a token for a given set of
 * authorities and can then parse the token again.
 */
@Test
public void testCreateAndExpandToken() {
    final Authorities authorities = new AuthoritiesImpl().addResource("telemetry", "*", Activity.READ, Activity.WRITE).addOperation("registration", "*", "assert");
    final Instant expirationMin = Instant.now().plusSeconds(59);
    final Instant expirationMax = expirationMin.plusSeconds(2);
    final String token = helper.createToken("userA", authorities);
    final Jws<Claims> parsedToken = helper.expand(token);
    assertThat(parsedToken.getBody()).isNotNull();
    assertThat(parsedToken.getBody().getExpiration().toInstant()).isAtLeast(expirationMin);
    assertThat(parsedToken.getBody().getExpiration().toInstant()).isAtMost(expirationMax);
}
Also used : Claims(io.jsonwebtoken.Claims) AuthoritiesImpl(org.eclipse.hono.auth.AuthoritiesImpl) Instant(java.time.Instant) Authorities(org.eclipse.hono.auth.Authorities) Test(org.junit.jupiter.api.Test)

Example 5 with AuthoritiesImpl

use of org.eclipse.hono.auth.AuthoritiesImpl in project hono by eclipse.

the class FileBasedAuthenticationService method getAuthorities.

private Authorities getAuthorities(final JsonObject user) {
    final AuthoritiesImpl result = new AuthoritiesImpl();
    user.getJsonArray(FIELD_AUTHORITIES).forEach(obj -> {
        final String authority = (String) obj;
        final Authorities roleAuthorities = roles.get(authority);
        if (roleAuthorities != null) {
            result.addAll(roleAuthorities);
        }
    });
    return result;
}
Also used : AuthoritiesImpl(org.eclipse.hono.auth.AuthoritiesImpl) Authorities(org.eclipse.hono.auth.Authorities)

Aggregations

AuthoritiesImpl (org.eclipse.hono.auth.AuthoritiesImpl)6 Authorities (org.eclipse.hono.auth.Authorities)4 JsonArray (io.vertx.core.json.JsonArray)2 JsonObject (io.vertx.core.json.JsonObject)2 Instant (java.time.Instant)2 Map (java.util.Map)2 Activity (org.eclipse.hono.auth.Activity)2 HonoUser (org.eclipse.hono.auth.HonoUser)2 Claims (io.jsonwebtoken.Claims)1 AsyncResult (io.vertx.core.AsyncResult)1 Context (io.vertx.core.Context)1 Future (io.vertx.core.Future)1 Handler (io.vertx.core.Handler)1 Vertx (io.vertx.core.Vertx)1 ProtonConnection (io.vertx.proton.ProtonConnection)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)1