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

SECAUTH-1483 #640

Merged
merged 4 commits into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,18 @@ public AbstractAuthenticationToken convert(Jwt jwt) {
}

protected Collection<GrantedAuthority> localScopeAuthorities(Jwt jwt) {
Collection<GrantedAuthority> localScopeAuthorities = new ArrayList<>();
Collection<String> scopes = jwt.getClaimAsStringList(TokenClaims.XSUAA.SCOPES);
if (scopes == null) {
return Collections.emptySet();
}
return localScopeAuthorities(jwt, scopes);
}

protected Collection<GrantedAuthority> localScopeAuthorities(Jwt jwt, Collection<String> scopes) {
Copy link
Contributor

Choose a reason for hiding this comment

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

the jwt seems to be not used in the method, is it required to have it in the argument list?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good finding!
But in this case this protected method is foreseen as "enhancement spot" and app developers may need access to other token claims.

Collection<GrantedAuthority> localScopeAuthorities = new ArrayList<>();
for (String scope : scopes) {
if (scope.startsWith(appId + ".")) {
localScopeAuthorities.add(new SimpleGrantedAuthority(scope.replaceFirst(appId + ".", "")));
localScopeAuthorities.add(new SimpleGrantedAuthority(scope.substring(appId.length() + 1)));
}
}
return localScopeAuthorities;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import static com.sap.cloud.security.config.Service.XSUAA;
import static org.junit.jupiter.api.Assertions.*;

class XsuaaTokenAuthenticationConverterTest {
class XsuaaTokenAuthorizationConverterTest {
String xsAppName = "my-app-name!400";
JwtGenerator jwtGenerator = JwtGenerator.getInstance(XSUAA, "theClientId").withAppId(xsAppName);
XsuaaTokenAuthorizationConverter cut = new XsuaaTokenAuthorizationConverter(xsAppName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected Set<String> getScopes(XsuaaToken jwt, String appId) {
}
return scopes.stream()
.filter(scope -> scope.startsWith(appId + "."))
.map(scope -> scope.replaceFirst(appId + ".", ""))
.map(scope -> scope.substring(appId.length() + 1))
.collect(Collectors.toSet());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.HashSet;
import java.util.Set;

import com.sap.cloud.security.xsuaa.extractor.LocalAuthoritiesExtractor;
import org.junit.Before;
import org.junit.Test;
import org.springframework.security.authentication.AbstractAuthenticationToken;
Expand Down Expand Up @@ -106,6 +107,21 @@ public void authoritiesHaveLocalScopesWithoutAppIdPrefix() {
assertThat(authenticationToken.getAuthorities(), hasItem(new SimpleGrantedAuthority("Read")));
}

@Test
public void checkFollowingInstanceScope() {
String scopeWithClientId = "7cf2e319-3a7d-4f99-8207-afdc8e8e6d64!b123|trustedclientid!b333.API_OVERVIEW";

Jwt jwt = new JwtGenerator("sb-7cf2e319-3a7d-4f99-8207-afdc8e8e6d64!b123|trustedclientid!b333")
.addScopes(xsAppName + "." + scopeAdmin, scopeRead, scopeWithClientId)
.getToken();
TokenAuthenticationConverter converter = new TokenAuthenticationConverter(
new MyFollowingInstanceAuthoritiesExtractor());

assertThat(converter.convert(jwt).getAuthorities().size(), is(1));
assertThat(converter.convert(jwt).getAuthorities(), hasItem(new SimpleGrantedAuthority("API_OVERVIEW")));

}

private static class MyAuthoritiesExtractor implements AuthoritiesExtractor {
private String[] xsUserAttributes;
private AuthoritiesExtractor authoritiesExtractor;
Expand Down Expand Up @@ -141,4 +157,18 @@ private static String getSidForAttributeValue(String attributeName, String attri
}

}

private static class MyFollowingInstanceAuthoritiesExtractor implements AuthoritiesExtractor {

@Override
public Collection<GrantedAuthority> getAuthorities(XsuaaToken token) {
String appId = "";
if (token.getClientId().startsWith("sb-")) {
appId = token.getClientId().replaceFirst("sb-", "");
}
AuthoritiesExtractor authoritiesExtractor = new LocalAuthoritiesExtractor(appId);
return authoritiesExtractor.getAuthorities(token);
}

nenaraab marked this conversation as resolved.
Show resolved Hide resolved
}
}