Search in sources :

Example 6 with Token

use of org.springframework.boot.actuate.autoconfigure.cloudfoundry.Token in project spring-boot by spring-projects.

the class CloudFoundrySecurityInterceptorTests method preHandleSuccessfulWithFullAccess.

@Test
void preHandleSuccessfulWithFullAccess() {
    String accessToken = mockAccessToken();
    this.request.addHeader("Authorization", "Bearer " + accessToken);
    given(this.securityService.getAccessLevel(accessToken, "my-app-id")).willReturn(AccessLevel.FULL);
    SecurityResponse response = this.interceptor.preHandle(this.request, EndpointId.of("test"));
    ArgumentCaptor<Token> tokenArgumentCaptor = ArgumentCaptor.forClass(Token.class);
    then(this.tokenValidator).should().validate(tokenArgumentCaptor.capture());
    Token token = tokenArgumentCaptor.getValue();
    assertThat(token.toString()).isEqualTo(accessToken);
    assertThat(response.getStatus()).isEqualTo(HttpStatus.OK);
    assertThat(this.request.getAttribute("cloudFoundryAccessLevel")).isEqualTo(AccessLevel.FULL);
}
Also used : Token(org.springframework.boot.actuate.autoconfigure.cloudfoundry.Token) SecurityResponse(org.springframework.boot.actuate.autoconfigure.cloudfoundry.SecurityResponse) Test(org.junit.jupiter.api.Test)

Example 7 with Token

use of org.springframework.boot.actuate.autoconfigure.cloudfoundry.Token in project spring-boot by spring-projects.

the class TokenValidatorTests method validateTokenWhenKidValidationSucceedsInTheSecondAttempt.

@Test
void validateTokenWhenKidValidationSucceedsInTheSecondAttempt() throws Exception {
    ReflectionTestUtils.setField(this.tokenValidator, "tokenKeys", INVALID_KEYS);
    given(this.securityService.fetchTokenKeys()).willReturn(VALID_KEYS);
    given(this.securityService.getUaaUrl()).willReturn("http://localhost:8080/uaa");
    String header = "{ \"alg\": \"RS256\",  \"kid\": \"valid-key\",\"typ\": \"JWT\"}";
    String claims = "{ \"exp\": 2147483647, \"iss\": \"http://localhost:8080/uaa/oauth/token\", \"scope\": [\"actuator.read\"]}";
    this.tokenValidator.validate(new Token(getSignedToken(header.getBytes(), claims.getBytes())));
    then(this.securityService).should().fetchTokenKeys();
}
Also used : Token(org.springframework.boot.actuate.autoconfigure.cloudfoundry.Token) Test(org.junit.jupiter.api.Test)

Example 8 with Token

use of org.springframework.boot.actuate.autoconfigure.cloudfoundry.Token in project spring-boot by spring-projects.

the class CloudFoundrySecurityInterceptor method check.

private void check(HttpServletRequest request, EndpointId endpointId) throws Exception {
    Token token = getToken(request);
    this.tokenValidator.validate(token);
    AccessLevel accessLevel = this.cloudFoundrySecurityService.getAccessLevel(token.toString(), this.applicationId);
    if (!accessLevel.isAccessAllowed((endpointId != null) ? endpointId.toLowerCaseString() : "")) {
        throw new CloudFoundryAuthorizationException(Reason.ACCESS_DENIED, "Access denied");
    }
    request.setAttribute(AccessLevel.REQUEST_ATTRIBUTE, accessLevel);
}
Also used : CloudFoundryAuthorizationException(org.springframework.boot.actuate.autoconfigure.cloudfoundry.CloudFoundryAuthorizationException) Token(org.springframework.boot.actuate.autoconfigure.cloudfoundry.Token) AccessLevel(org.springframework.boot.actuate.autoconfigure.cloudfoundry.AccessLevel)

Example 9 with Token

use of org.springframework.boot.actuate.autoconfigure.cloudfoundry.Token in project spring-boot by spring-projects.

the class ReactiveTokenValidatorTests method validateTokenWhenCacheValidShouldNotFetchTokenKeys.

@Test
void validateTokenWhenCacheValidShouldNotFetchTokenKeys() throws Exception {
    PublisherProbe<Map<String, String>> fetchTokenKeys = PublisherProbe.empty();
    ReflectionTestUtils.setField(this.tokenValidator, "cachedTokenKeys", VALID_KEYS);
    given(this.securityService.getUaaUrl()).willReturn(Mono.just("http://localhost:8080/uaa"));
    String header = "{\"alg\": \"RS256\",  \"kid\": \"valid-key\",\"typ\": \"JWT\"}";
    String claims = "{\"exp\": 2147483647, \"iss\": \"http://localhost:8080/uaa/oauth/token\", \"scope\": [\"actuator.read\"]}";
    StepVerifier.create(this.tokenValidator.validate(new Token(getSignedToken(header.getBytes(), claims.getBytes())))).verifyComplete();
    fetchTokenKeys.assertWasNotSubscribed();
}
Also used : Token(org.springframework.boot.actuate.autoconfigure.cloudfoundry.Token) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.jupiter.api.Test)

Example 10 with Token

use of org.springframework.boot.actuate.autoconfigure.cloudfoundry.Token in project spring-boot by spring-projects.

the class ReactiveTokenValidatorTests method validateTokenWhenKidValidationSucceedsInTheSecondAttempt.

@Test
void validateTokenWhenKidValidationSucceedsInTheSecondAttempt() throws Exception {
    PublisherProbe<Map<String, String>> fetchTokenKeys = PublisherProbe.of(Mono.just(VALID_KEYS));
    ReflectionTestUtils.setField(this.tokenValidator, "cachedTokenKeys", INVALID_KEYS);
    given(this.securityService.fetchTokenKeys()).willReturn(fetchTokenKeys.mono());
    given(this.securityService.getUaaUrl()).willReturn(Mono.just("http://localhost:8080/uaa"));
    String header = "{\"alg\": \"RS256\",  \"kid\": \"valid-key\",\"typ\": \"JWT\"}";
    String claims = "{\"exp\": 2147483647, \"iss\": \"http://localhost:8080/uaa/oauth/token\", \"scope\": [\"actuator.read\"]}";
    StepVerifier.create(this.tokenValidator.validate(new Token(getSignedToken(header.getBytes(), claims.getBytes())))).verifyComplete();
    assertThat(this.tokenValidator).hasFieldOrPropertyWithValue("cachedTokenKeys", VALID_KEYS);
    fetchTokenKeys.assertWasSubscribed();
}
Also used : Token(org.springframework.boot.actuate.autoconfigure.cloudfoundry.Token) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.jupiter.api.Test)

Aggregations

Token (org.springframework.boot.actuate.autoconfigure.cloudfoundry.Token)15 Test (org.junit.jupiter.api.Test)14 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 SecurityResponse (org.springframework.boot.actuate.autoconfigure.cloudfoundry.SecurityResponse)2 AccessLevel (org.springframework.boot.actuate.autoconfigure.cloudfoundry.AccessLevel)1 CloudFoundryAuthorizationException (org.springframework.boot.actuate.autoconfigure.cloudfoundry.CloudFoundryAuthorizationException)1