@Test public void createMailboxSessionShouldThrowWhenAuthHeaderIsNotAnUUID() { when(mockedHeaders.get(AUTHORIZATION_HEADERS)) .thenReturn("bad"); assertThatThrownBy(() -> testee.createMailboxSession(mockedRequest).block()) .isExactlyInstanceOf(UnauthorizedException.class); }
@Override public Mono<MailboxSession> createMailboxSession(HttpServerRequest httpRequest) { return Mono.fromCallable(() -> authHeaders(httpRequest)) .filter(tokenString -> !tokenString.startsWith("Bearer")) .map(AccessToken::fromString) .filterWhen(accessTokenManager::isValid) .flatMap(item -> Mono.from(accessTokenManager.getUsernameFromToken(item))) .map(mailboxManager::createSystemSession) .onErrorResume(InvalidAccessToken.class, error -> Mono.error(new UnauthorizedException("Invalid access token", error))) .onErrorResume(NotAnAccessTokenException.class, error -> Mono.error(new UnauthorizedException("Not an access token", error))); }
AccessTokenAuthenticationStrategy implements AuthenticationStrategy { @Override public Mono<MailboxSession> createMailboxSession(HttpServerRequest httpRequest) { return Mono.fromCallable(() -> authHeaders(httpRequest)) .filter(tokenString -> !tokenString.startsWith("Bearer")) .map(AccessToken::fromString) .filterWhen(accessTokenManager::isValid) .flatMap(item -> Mono.from(accessTokenManager.getUsernameFromToken(item))) .map(mailboxManager::createSystemSession) .onErrorResume(InvalidAccessToken.class, error -> Mono.error(new UnauthorizedException("Invalid access token", error))) .onErrorResume(NotAnAccessTokenException.class, error -> Mono.error(new UnauthorizedException("Not an access token", error))); } }
AccessTokenAuthenticationStrategy implements AuthenticationStrategy { @Override public Mono<MailboxSession> createMailboxSession(HttpServerRequest httpRequest) { return Mono.fromCallable(() -> authHeaders(httpRequest)) .filter(tokenString -> !tokenString.startsWith("Bearer")) .map(AccessToken::fromString) .filterWhen(accessTokenManager::isValid) .flatMap(item -> Mono.from(accessTokenManager.getUsernameFromToken(item))) .map(mailboxManager::createSystemSession) .onErrorResume(InvalidAccessToken.class, error -> Mono.error(new UnauthorizedException("Invalid access token", error))) .onErrorResume(NotAnAccessTokenException.class, error -> Mono.error(new UnauthorizedException("Not an access token", error))); } @Inject @VisibleForTesting  AccessTokenAuthenticationStrategy(AccessTokenManager accessTokenManager, MailboxManager mailboxManager); }
AccessTokenAuthenticationStrategy implements AuthenticationStrategy { @Override public Mono<MailboxSession> createMailboxSession(HttpServerRequest httpRequest) { return Mono.fromCallable(() -> authHeaders(httpRequest)) .filter(tokenString -> !tokenString.startsWith("Bearer")) .map(AccessToken::fromString) .filterWhen(accessTokenManager::isValid) .flatMap(item -> Mono.from(accessTokenManager.getUsernameFromToken(item))) .map(mailboxManager::createSystemSession) .onErrorResume(InvalidAccessToken.class, error -> Mono.error(new UnauthorizedException("Invalid access token", error))) .onErrorResume(NotAnAccessTokenException.class, error -> Mono.error(new UnauthorizedException("Not an access token", error))); } @Inject @VisibleForTesting  AccessTokenAuthenticationStrategy(AccessTokenManager accessTokenManager, MailboxManager mailboxManager); @Override Mono<MailboxSession> createMailboxSession(HttpServerRequest httpRequest); }
AccessTokenAuthenticationStrategy implements AuthenticationStrategy { @Override public Mono<MailboxSession> createMailboxSession(HttpServerRequest httpRequest) { return Mono.fromCallable(() -> authHeaders(httpRequest)) .filter(tokenString -> !tokenString.startsWith("Bearer")) .map(AccessToken::fromString) .filterWhen(accessTokenManager::isValid) .flatMap(item -> Mono.from(accessTokenManager.getUsernameFromToken(item))) .map(mailboxManager::createSystemSession) .onErrorResume(InvalidAccessToken.class, error -> Mono.error(new UnauthorizedException("Invalid access token", error))) .onErrorResume(NotAnAccessTokenException.class, error -> Mono.error(new UnauthorizedException("Not an access token", error))); } @Inject @VisibleForTesting  AccessTokenAuthenticationStrategy(AccessTokenManager accessTokenManager, MailboxManager mailboxManager); @Override Mono<MailboxSession> createMailboxSession(HttpServerRequest httpRequest);  }