|
| 1 | +/* |
| 2 | + * Copyright 2002-2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.oauth2.server.resource.authentication; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | + |
| 21 | +import org.junit.Test; |
| 22 | + |
| 23 | +import org.springframework.security.authentication.AbstractAuthenticationToken; |
| 24 | +import org.springframework.security.core.authority.SimpleGrantedAuthority; |
| 25 | +import org.springframework.security.oauth2.jwt.Jwt; |
| 26 | + |
| 27 | +import static org.assertj.core.api.Assertions.assertThat; |
| 28 | + |
| 29 | +/** |
| 30 | + * Tests for {@link JwtBearerTokenAuthenticationConverter} |
| 31 | + * |
| 32 | + * @author Josh Cummings |
| 33 | + */ |
| 34 | +public class JwtBearerTokenAuthenticationConverterTests { |
| 35 | + private final JwtBearerTokenAuthenticationConverter converter = |
| 36 | + new JwtBearerTokenAuthenticationConverter(); |
| 37 | + |
| 38 | + @Test |
| 39 | + public void convertWhenJwtThenBearerTokenAuthentication() { |
| 40 | + Jwt jwt = Jwt.withTokenValue("token-value") |
| 41 | + .claim("claim", "value") |
| 42 | + .header("header", "value") |
| 43 | + .build(); |
| 44 | + |
| 45 | + AbstractAuthenticationToken token = this.converter.convert(jwt); |
| 46 | + |
| 47 | + assertThat(token).isInstanceOf(BearerTokenAuthentication.class); |
| 48 | + BearerTokenAuthentication bearerToken = (BearerTokenAuthentication) token; |
| 49 | + assertThat(bearerToken.getToken().getTokenValue()).isEqualTo("token-value"); |
| 50 | + assertThat(bearerToken.getTokenAttributes()).containsOnlyKeys("claim"); |
| 51 | + assertThat(bearerToken.getAuthorities()).isEmpty(); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void convertWhenJwtWithScopeAttributeThenBearerTokenAuthentication() { |
| 56 | + Jwt jwt = Jwt.withTokenValue("token-value") |
| 57 | + .claim("scope", "message:read message:write") |
| 58 | + .header("header", "value") |
| 59 | + .build(); |
| 60 | + |
| 61 | + AbstractAuthenticationToken token = this.converter.convert(jwt); |
| 62 | + |
| 63 | + assertThat(token).isInstanceOf(BearerTokenAuthentication.class); |
| 64 | + BearerTokenAuthentication bearerToken = (BearerTokenAuthentication) token; |
| 65 | + assertThat(bearerToken.getAuthorities()) |
| 66 | + .containsExactly(new SimpleGrantedAuthority("SCOPE_message:read"), |
| 67 | + new SimpleGrantedAuthority("SCOPE_message:write")); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void convertWhenJwtWithScpAttributeThenBearerTokenAuthentication() { |
| 72 | + Jwt jwt = Jwt.withTokenValue("token-value") |
| 73 | + .claim("scp", Arrays.asList("message:read", "message:write")) |
| 74 | + .header("header", "value") |
| 75 | + .build(); |
| 76 | + |
| 77 | + AbstractAuthenticationToken token = this.converter.convert(jwt); |
| 78 | + |
| 79 | + assertThat(token).isInstanceOf(BearerTokenAuthentication.class); |
| 80 | + BearerTokenAuthentication bearerToken = (BearerTokenAuthentication) token; |
| 81 | + assertThat(bearerToken.getAuthorities()) |
| 82 | + .containsExactly(new SimpleGrantedAuthority("SCOPE_message:read"), |
| 83 | + new SimpleGrantedAuthority("SCOPE_message:write")); |
| 84 | + } |
| 85 | +} |
0 commit comments