@Test public void createMailboxSessionShouldReturnEmptyWhenAuthHeaderIsInvalid() { Username username = Username.of("123456789"); MailboxSession fakeMailboxSession = mock(MailboxSession.class); when(mockedMailboxManager.createSystemSession(eq(username))) .thenReturn(fakeMailboxSession); UUID authHeader = UUID.randomUUID(); AccessToken accessToken = AccessToken.fromString(authHeader.toString()); when(mockedAccessTokenManager.getUsernameFromToken(accessToken)) .thenReturn(Mono.just(username)); when(mockedHeaders.get(AUTHORIZATION_HEADERS)) .thenReturn(authHeader.toString()); when(mockedAccessTokenManager.isValid(accessToken)) .thenReturn(Mono.just(false)); assertThat(testee.createMailboxSession(mockedRequest).blockOptional()) .isEmpty(); }
@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);  }