Search in sources :

Example 16 with JwtAuthenticationToken

use of org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken in project spring-security by spring-projects.

the class JwtAuthenticationTokenTests method getNameWhenConstructedWithJwtAndAuthoritiesThenReturnsSubject.

@Test
public void getNameWhenConstructedWithJwtAndAuthoritiesThenReturnsSubject() {
    Collection<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList("test");
    Jwt jwt = builder().subject("Hayden").build();
    JwtAuthenticationToken token = new JwtAuthenticationToken(jwt, authorities);
    assertThat(token.getName()).isEqualTo("Hayden");
}
Also used : Jwt(org.springframework.security.oauth2.jwt.Jwt) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Test(org.junit.jupiter.api.Test)

Example 17 with JwtAuthenticationToken

use of org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken in project spring-security by spring-projects.

the class JwtAuthenticationTokenTests method constructorWhenUsingOnlyJwtThenConstructedCorrectly.

@Test
public void constructorWhenUsingOnlyJwtThenConstructedCorrectly() {
    Jwt jwt = builder().claim("claim", "value").build();
    JwtAuthenticationToken token = new JwtAuthenticationToken(jwt);
    assertThat(token.getAuthorities()).isEmpty();
    assertThat(token.getPrincipal()).isEqualTo(jwt);
    assertThat(token.getCredentials()).isEqualTo(jwt);
    assertThat(token.getToken()).isEqualTo(jwt);
    assertThat(token.getTokenAttributes()).isEqualTo(jwt.getClaims());
    assertThat(token.isAuthenticated()).isFalse();
}
Also used : Jwt(org.springframework.security.oauth2.jwt.Jwt) Test(org.junit.jupiter.api.Test)

Example 18 with JwtAuthenticationToken

use of org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken in project spring-security by spring-projects.

the class JwtAuthenticationTokenTests method getNameWhenConstructedWithNoSubjectThenReturnsNull.

@Test
public void getNameWhenConstructedWithNoSubjectThenReturnsNull() {
    Collection<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList("test");
    Jwt jwt = builder().claim("claim", "value").build();
    assertThat(new JwtAuthenticationToken(jwt, authorities, null).getName()).isNull();
    assertThat(new JwtAuthenticationToken(jwt, authorities).getName()).isNull();
    assertThat(new JwtAuthenticationToken(jwt).getName()).isNull();
}
Also used : Jwt(org.springframework.security.oauth2.jwt.Jwt) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Test(org.junit.jupiter.api.Test)

Example 19 with JwtAuthenticationToken

use of org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken in project spring-security by spring-projects.

the class JwtAuthenticationTokenTests method constructorWhenUsingCorrectParametersThenConstructedCorrectly.

@Test
public void constructorWhenUsingCorrectParametersThenConstructedCorrectly() {
    Collection<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList("test");
    Jwt jwt = builder().claim("claim", "value").build();
    JwtAuthenticationToken token = new JwtAuthenticationToken(jwt, authorities);
    assertThat(token.getAuthorities()).isEqualTo(authorities);
    assertThat(token.getPrincipal()).isEqualTo(jwt);
    assertThat(token.getCredentials()).isEqualTo(jwt);
    assertThat(token.getToken()).isEqualTo(jwt);
    assertThat(token.getTokenAttributes()).isEqualTo(jwt.getClaims());
    assertThat(token.isAuthenticated()).isTrue();
}
Also used : Jwt(org.springframework.security.oauth2.jwt.Jwt) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Test(org.junit.jupiter.api.Test)

Example 20 with JwtAuthenticationToken

use of org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken in project spring-security by spring-projects.

the class JwtAuthenticationTokenTests method getNameWhenConstructedWithJwtThenReturnsSubject.

@Test
public void getNameWhenConstructedWithJwtThenReturnsSubject() {
    Jwt jwt = builder().subject("Hayden").build();
    JwtAuthenticationToken token = new JwtAuthenticationToken(jwt);
    assertThat(token.getName()).isEqualTo("Hayden");
}
Also used : Jwt(org.springframework.security.oauth2.jwt.Jwt) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)20 Jwt (org.springframework.security.oauth2.jwt.Jwt)16 JwtAuthenticationToken (org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken)11 GrantedAuthority (org.springframework.security.core.GrantedAuthority)6 SecurityContext (org.springframework.security.core.context.SecurityContext)6 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)3 BearerTokenAuthenticationToken (org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken)3 Arrays (java.util.Arrays)2 List (java.util.List)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Authentication (org.springframework.security.core.Authentication)2 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)2 JwtDecoder (org.springframework.security.oauth2.jwt.JwtDecoder)2 NimbusJwtDecoder (org.springframework.security.oauth2.jwt.NimbusJwtDecoder)2 TestJwts (org.springframework.security.oauth2.jwt.TestJwts)2 HttpModuleAuthentication (com.evolveum.midpoint.authentication.impl.module.authentication.HttpModuleAuthentication)1 MidPointPrincipal (com.evolveum.midpoint.security.api.MidPointPrincipal)1 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 AfterEach (org.junit.jupiter.api.AfterEach)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1