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;
}
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;
}
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;
}
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);
}
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;
}
Aggregations