Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add roles-separator to split roles from attributes #796

Merged

Conversation

frehov
Copy link
Contributor

@frehov frehov commented Sep 28, 2021

closes #787

@frehov frehov force-pushed the feature/customize_roles_conversion branch from f9d8d41 to c5a549a Compare September 28, 2021 11:02
@frehov frehov changed the title Add roles-separator to split roles from attributes feat: Add roles-separator to split roles from attributes Sep 28, 2021
@@ -52,6 +53,10 @@ public DefaultRolesFinder(TokenConfiguration tokenConfiguration) {
List<String> roles = new ArrayList<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you think if we rewrite this method as:

@NonNull
    private List<String> rolesAtObject(@Nullable Object rolesObject) {
        if (rolesObject == null) {
            return Collections.emptyList();
        }
        if (rolesObject instanceof CharSequence && tokenConfiguration.getRolesSeparator() != null) {
            return Arrays.asList(rolesObject.toString().split(tokenConfiguration.getRolesSeparator()));
        }
        if (rolesObject instanceof Iterable) {
            List<String> roles = new ArrayList<>();
            for (Object o : ((Iterable<?>) rolesObject)) {
                roles.add(o.toString());
            }
            return roles;
        }
        return Collections.singletonList(rolesObject.toString());
    }

Moreover we have to update the Javadoc to explain the change

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I should've done.
I do prefer static imports from Colleactions, as it mostly helps with readability, but I'm not sure if that's allowed by the checkstyle

}

/**
* Used to change how the roles from the {@link io.micronaut.security.authentication.Authentication} attributes map are converted. Default value {@value io.micronaut.security.token.config.TokenConfiguration#DEFAULT_ROLES_SEPARATOR}.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure about the copy. This shows in our docs. I would like to convey it is used if the roles is a string.

What do you think if we change it to something like the following?

If the entry used for the roles in the {@link io.micronaut.security.authentication.Authentication} attributes map is a String, you can use the separator to split its value into multiple roles. Default value {@value io.micronaut.security.token.config.TokenConfiguration#DEFAULT_ROLES_SEPARATOR}.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm with you on that, it conveys the intention better

@sdelamo sdelamo added the type: improvement A minor improvement to an existing feature label Sep 28, 2021
@sdelamo sdelamo added this to the 3.1.0 milestone Sep 28, 2021
@sdelamo sdelamo merged commit 1c07908 into micronaut-projects:master Sep 29, 2021
@frehov frehov deleted the feature/customize_roles_conversion branch September 29, 2021 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: improvement A minor improvement to an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

micronaut-security-jwt: scopes are not converted to list/iterable when remapping the rolesfinder
2 participants