Skip to content

Commit

Permalink
Add new user role for authenticated users. (#71)
Browse files Browse the repository at this point in the history
* Add new user role for authenticated users.
* Accept the isAuthenticated() keyword also for admins
  • Loading branch information
bjornandre authored Nov 20, 2023
1 parent acc2d17 commit 0ed0659
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 4 deletions.
2 changes: 2 additions & 0 deletions conf/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ pseudo.secrets:
type: TINK_WDEK

app-roles:
users:
- isAuthenticated()
admins:
- [email protected]
- [email protected]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,18 @@
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.Principal;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import static no.ssb.dlp.pseudo.core.util.Zips.ZipOptions.zipOpts;

@RequiredArgsConstructor
@Controller
@Slf4j
@Secured(SecurityRule.IS_AUTHENTICATED)
@Secured({PseudoServiceRole.USER, PseudoServiceRole.ADMIN})
@Tag(name = "Pseudo operations")
public class PseudoController {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.micronaut.context.annotation.Requirements;
import io.micronaut.context.annotation.Requires;
import io.micronaut.context.env.Environment;
import io.micronaut.security.rules.SecurityRule;
import io.micronaut.security.token.DefaultRolesFinder;
import io.micronaut.security.token.RolesFinder;
import io.micronaut.security.token.config.TokenConfiguration;
Expand Down Expand Up @@ -31,9 +32,14 @@ public List<String> resolveRoles(Map<String, Object> attributes) {
List<String> roles = new ArrayList<>();

Object username = attributes.get(tokenConfiguration.getNameKey());
if (rolesConfig.getAdmins().contains(username)) {
if (rolesConfig.getAdmins().contains(SecurityRule.IS_AUTHENTICATED)
||rolesConfig.getAdmins().contains(username)) {
roles.add(PseudoServiceRole.ADMIN);
}
if (rolesConfig.getUsers().contains(SecurityRule.IS_AUTHENTICATED)
|| rolesConfig.getUsers().contains(username)) {
roles.add(PseudoServiceRole.USER);
}

return roles;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ public final class PseudoServiceRole {
private PseudoServiceRole() {}

public static final String ADMIN = "admin";
public static final String USER = "user";
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
@Data
public class StaticRolesConfig {
@NotBlank
private List<String> users = new ArrayList<>();
private List<String> admins = new ArrayList<>();
}

0 comments on commit 0ed0659

Please sign in to comment.