Skip to content

Commit

Permalink
Fix OAuth2 authenticated user account api
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalgrimaud committed Jul 28, 2022
1 parent 213462e commit 969cc24
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
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 @@ -54,7 +53,10 @@ class OAuth2AuthenticationReader {
if (authentication instanceof OAuth2AuthenticationToken oauthToken) {
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());
}
}

0 comments on commit 969cc24

Please sign in to comment.