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

Add ScopeGrantedAuthority #23

Open
jzheaux opened this issue Jun 26, 2018 · 0 comments
Open

Add ScopeGrantedAuthority #23

jzheaux opened this issue Jun 26, 2018 · 0 comments

Comments

@jzheaux
Copy link
Owner

jzheaux commented Jun 26, 2018

Summary

There is data unique to scopes that, if preserved, would clean up code in a few places that need to recompute it each time.

For example, in BearerTokenAccessDeniedHandler, the scope attribute is returned with the list of scopes provided in the authentication token.

To find the scopes on the authentication token, though, it must rediscover the scope claim and then reparse it--two tasks that were already done during authentication.

ScopeGrantedAuthority would look something like:

public class ScopeGrantedAuthority extends GrantedAuthority {
    private final String scopePrefix = "SCOPE_";
    
    private final String authority;
    private final String scope;

    public ScopeGrantedAuthority(String scope) {
        this.scope = scope;
        this.authority = this.scopePrefix + scope;
    }

    public String getAuthority() {
        return this.authority;
    }

    public String getScope() {
        return this.scope;
    }

    // ... hashcode, equals, and toString
}

This would require very little change to authority extraction:

.map(scope -> "SCOPE_" + scope)
.map(SimpleGrantedAuthority::new)

would become

.map(ScopeGrantedAuthority::new)

And about 20 lines in BearerTokenAccessDeniedHandler would become:

authentication.getAuthorities().stream()
    .filter(authority -> authority instanceof ScopeGrantedAuthority)
    .map(authority -> ((ScopeGrantedAuthority) authority).getScope())
    .collect(Collectors.joining(" "));

Another example of where remembering the original scope is helpful is when constructing a BearerTokenError with the scope parameter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant