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

Fix OAuth2 authenticated user account api #2816

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
@@ -1,17 +1,16 @@
package {{packageName}}.account.infrastructure.secondary;

import {{packageName}}.account.domain.Account;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
import org.springframework.stereotype.Service;

import {{packageName}}.account.domain.Account;

@Service
class OAuth2AuthenticationReader {

Expand Down Expand Up @@ -55,6 +54,10 @@ class OAuth2AuthenticationReader {
return oauthToken.getPrincipal().getAttributes();
}

if (authentication instanceof JwtAuthenticationToken jwtToken) {
return jwtToken.getTokenAttributes();
}

throw new UnknownAuthenticationSchemeException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import org.springframework.security.oauth2.client.authentication.OAuth2Authentic
import org.springframework.security.oauth2.core.oidc.OidcIdToken;
import org.springframework.security.oauth2.core.oidc.OidcUserInfo;
import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser;
import org.springframework.security.oauth2.jose.jws.JwsAlgorithms;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;

public final class OAuth2TokenFixture {

Expand Down Expand Up @@ -42,9 +45,19 @@ public final class OAuth2TokenFixture {
Instant now = Instant.now();

OidcIdToken token = new OidcIdToken(TOKEN_ID, now, now.plusSeconds(300), claims);
List<SimpleGrantedAuthority> authorities = List.of(new SimpleGrantedAuthority(Role.ADMIN.key()));
List<SimpleGrantedAuthority> authorities = adminAuthorities();
DefaultOidcUser user = new DefaultOidcUser(authorities, token, new OidcUserInfo(claims), "preferred_username");

return new OAuth2AuthenticationToken(user, authorities, "oidc");
}

public static JwtAuthenticationToken testJwtAuthenticationToken() {
Jwt.Builder jwt = Jwt.withTokenValue("token-just-for-drinking-beers").header("alg", JwsAlgorithms.RS256).subject("jhipster");
testAuthenticationClaims().forEach(jwt::claim);
return new JwtAuthenticationToken(jwt.build(), adminAuthorities());
}

private static List<SimpleGrantedAuthority> adminAuthorities() {
return List.of(new SimpleGrantedAuthority(Role.ADMIN.key()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class OAuth2AuthenticationReaderTest {
context.setAuthentication(authentication);
assertThatThrownBy(() -> tokens.authenticatedUserAccount()).isExactlyInstanceOf(UnknownAuthenticationSchemeException.class);
assertThatThrownBy(tokens::authenticatedUserAccount).isExactlyInstanceOf(UnknownAuthenticationSchemeException.class);
}

@Test
Expand All @@ -52,4 +52,11 @@ class OAuth2AuthenticationReaderTest {
assertThat(tokens.authenticatedUserAccount().get()).usingRecursiveComparison().isEqualTo(account());
}

@Test
void shouldReadJwtAuthentication() {
context.setAuthentication(testJwtAuthenticationToken());
assertThat(tokens.authenticatedUserAccount().get()).usingRecursiveComparison().isEqualTo(account());
}
}