Example usage for org.springframework.boot.actuate.autoconfigure.cloudfoundry Token Token

List of usage examples for org.springframework.boot.actuate.autoconfigure.cloudfoundry Token Token

Introduction

In this page you can find the example usage for org.springframework.boot.actuate.autoconfigure.cloudfoundry Token Token.

Prototype

public Token(String encoded) 

Source Link

Usage

From source file:org.springframework.boot.actuate.autoconfigure.cloudfoundry.reactive.ReactiveTokenValidatorTests.java

@Test
public void validateTokenWhenExpiredShouldThrowException() throws Exception {
    given(this.securityService.fetchTokenKeys()).willReturn(Mono.just(VALID_KEYS));
    given(this.securityService.getUaaUrl()).willReturn(Mono.just("http://localhost:8080/uaa"));
    String header = "{ \"alg\": \"RS256\",  \"kid\": \"valid-key\", \"typ\": \"JWT\"}";
    String claims = "{ \"jti\": \"0236399c350c47f3ae77e67a75e75e7d\", \"exp\": 1477509977, \"scope\": [\"actuator.read\"]}";
    StepVerifier// www.ja va 2 s . c  om
            .create(this.tokenValidator
                    .validate(new Token(getSignedToken(header.getBytes(), claims.getBytes()))))
            .consumeErrorWith((ex) -> {
                assertThat(ex).isExactlyInstanceOf(CloudFoundryAuthorizationException.class);
                assertThat(((CloudFoundryAuthorizationException) ex).getReason())
                        .isEqualTo(Reason.TOKEN_EXPIRED);
            }).verify();
}

From source file:org.springframework.boot.actuate.autoconfigure.cloudfoundry.reactive.ReactiveTokenValidatorTests.java

@Test
public void validateTokenWhenIssuerIsNotValidShouldThrowException() throws Exception {
    given(this.securityService.fetchTokenKeys()).willReturn(Mono.just(VALID_KEYS));
    given(this.securityService.getUaaUrl()).willReturn(Mono.just("http://other-uaa.com"));
    String header = "{ \"alg\": \"RS256\",  \"kid\": \"valid-key\", \"typ\": \"JWT\", \"scope\": [\"actuator.read\"]}";
    String claims = "{ \"exp\": 2147483647, \"iss\": \"http://localhost:8080/uaa/oauth/token\", \"scope\": [\"foo.bar\"]}";
    StepVerifier/*  w w  w  .j a  va2  s  .c  o m*/
            .create(this.tokenValidator
                    .validate(new Token(getSignedToken(header.getBytes(), claims.getBytes()))))
            .consumeErrorWith((ex) -> {
                assertThat(ex).isExactlyInstanceOf(CloudFoundryAuthorizationException.class);
                assertThat(((CloudFoundryAuthorizationException) ex).getReason())
                        .isEqualTo(Reason.INVALID_ISSUER);
            }).verify();
}

From source file:org.springframework.boot.actuate.autoconfigure.cloudfoundry.reactive.ReactiveTokenValidatorTests.java

@Test
public void validateTokenWhenAudienceIsNotValidShouldThrowException() throws Exception {
    given(this.securityService.fetchTokenKeys()).willReturn(Mono.just(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\": [\"foo.bar\"]}";
    StepVerifier//from   w w w  .ja v  a  2  s . co m
            .create(this.tokenValidator
                    .validate(new Token(getSignedToken(header.getBytes(), claims.getBytes()))))
            .consumeErrorWith((ex) -> {
                assertThat(ex).isExactlyInstanceOf(CloudFoundryAuthorizationException.class);
                assertThat(((CloudFoundryAuthorizationException) ex).getReason())
                        .isEqualTo(Reason.INVALID_AUDIENCE);
            }).verify();
}

From source file:org.springframework.boot.actuate.autoconfigure.cloudfoundry.servlet.CloudFoundrySecurityInterceptor.java

private Token getToken(HttpServletRequest request) {
    String authorization = request.getHeader("Authorization");
    String bearerPrefix = "bearer ";
    if (authorization == null || !authorization.toLowerCase(Locale.ENGLISH).startsWith(bearerPrefix)) {
        throw new CloudFoundryAuthorizationException(Reason.MISSING_AUTHORIZATION,
                "Authorization header is missing or invalid");
    }/*from w  w  w .  j a  v a2 s.  c o  m*/
    return new Token(authorization.substring(bearerPrefix.length()));
}

From source file:org.springframework.boot.actuate.autoconfigure.cloudfoundry.servlet.TokenValidatorTests.java

@Test
public void validateTokenWhenKidValidationFailsTwiceShouldThrowException() throws Exception {
    ReflectionTestUtils.setField(this.tokenValidator, "tokenKeys", INVALID_KEYS);
    given(this.securityService.fetchTokenKeys()).willReturn(INVALID_KEYS);
    String header = "{\"alg\": \"RS256\",  \"kid\": \"valid-key\",\"typ\": \"JWT\"}";
    String claims = "{\"exp\": 2147483647, \"iss\": \"http://localhost:8080/uaa/oauth/token\", \"scope\": [\"actuator.read\"]}";
    this.thrown.expect(AuthorizationExceptionMatcher.withReason(Reason.INVALID_KEY_ID));
    this.tokenValidator.validate(new Token(getSignedToken(header.getBytes(), claims.getBytes())));
}

From source file:org.springframework.boot.actuate.autoconfigure.cloudfoundry.servlet.TokenValidatorTests.java

@Test
public 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())));
    verify(this.securityService).fetchTokenKeys();
}

From source file:org.springframework.boot.actuate.autoconfigure.cloudfoundry.servlet.TokenValidatorTests.java

@Test
public void validateTokenShouldFetchTokenKeysIfNull() throws Exception {
    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())));
    verify(this.securityService).fetchTokenKeys();
}

From source file:org.springframework.boot.actuate.autoconfigure.cloudfoundry.servlet.TokenValidatorTests.java

@Test
public void validateTokenWhenValidShouldNotFetchTokenKeys() throws Exception {
    ReflectionTestUtils.setField(this.tokenValidator, "tokenKeys", 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())));
    verify(this.securityService, Mockito.never()).fetchTokenKeys();
}

From source file:org.springframework.boot.actuate.autoconfigure.cloudfoundry.servlet.TokenValidatorTests.java

@Test
public void validateTokenWhenSignatureInvalidShouldThrowException() throws Exception {
    ReflectionTestUtils.setField(this.tokenValidator, "tokenKeys",
            Collections.singletonMap("valid-key", INVALID_KEY));
    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.thrown.expect(AuthorizationExceptionMatcher.withReason(Reason.INVALID_SIGNATURE));
    this.tokenValidator.validate(new Token(getSignedToken(header.getBytes(), claims.getBytes())));
}

From source file:org.springframework.boot.actuate.autoconfigure.cloudfoundry.servlet.TokenValidatorTests.java

@Test
public void validateTokenWhenTokenAlgorithmIsNotRS256ShouldThrowException() throws Exception {
    given(this.securityService.fetchTokenKeys()).willReturn(VALID_KEYS);
    String header = "{ \"alg\": \"HS256\",  \"typ\": \"JWT\"}";
    String claims = "{ \"exp\": 2147483647, \"iss\": \"http://localhost:8080/uaa/oauth/token\", \"scope\": [\"actuator.read\"]}";
    this.thrown.expect(AuthorizationExceptionMatcher.withReason(Reason.UNSUPPORTED_TOKEN_SIGNING_ALGORITHM));
    this.tokenValidator.validate(new Token(getSignedToken(header.getBytes(), claims.getBytes())));
}