@Test public void testAuthWithNoMatchingAuthenticator() throws Throwable { AuthenticationInterceptor interceptor = new AuthenticationInterceptor(); interceptor.authenticators = new Authenticator[]{new TrueAuthenticator()}; Authenticated authenticated = mock(Authenticated.class); when(authenticated.value()).thenReturn("admin"); RequestContext ic = mock(RequestContext.class); Context ctx = mock(Context.class); Request request = mock(Request.class); when(ic.context()).thenReturn(ctx); when(ic.proceed()).thenReturn(Results.ok("authenticated")); when(ctx.request()).thenReturn(request); doAnswer(usernameAnswer).when(request).setUsername(anyString()); assertThat(interceptor.call(authenticated, ic).getStatusCode()).isEqualTo(401); assertThat(username).isNull(); }
@Override public Result call(Authenticated configuration, RequestContext context) throws Exception { Authenticator authenticator = getAuthenticator(context, configuration.value()); if (authenticator != null) { String username = authenticator.getUserName(context.context()); if (username == null) { context.context().request().setUsername(null); return authenticator.onUnauthorized(context.context()); } else { context.context().request().setUsername(username); return context.proceed(); } } else { context.context().request().setUsername(null); return Results.unauthorized(); } }
AuthenticationInterceptor extends Interceptor<Authenticated> { @Override public Result call(Authenticated configuration, RequestContext context) throws Exception { Authenticator authenticator = getAuthenticator(context, configuration.value()); if (authenticator != null) { String username = authenticator.getUserName(context.context()); if (username == null) { context.context().request().setUsername(null); return authenticator.onUnauthorized(context.context()); } else { context.context().request().setUsername(username); return context.proceed(); } } else { context.context().request().setUsername(null); return Results.unauthorized(); } } }
AuthenticationInterceptor extends Interceptor<Authenticated> { @Override public Result call(Authenticated configuration, RequestContext context) throws Exception { Authenticator authenticator = getAuthenticator(context, configuration.value()); if (authenticator != null) { String username = authenticator.getUserName(context.context()); if (username == null) { context.context().request().setUsername(null); return authenticator.onUnauthorized(context.context()); } else { context.context().request().setUsername(username); return context.proceed(); } } else { context.context().request().setUsername(null); return Results.unauthorized(); } }  }
AuthenticationInterceptor extends Interceptor<Authenticated> { @Override public Result call(Authenticated configuration, RequestContext context) throws Exception { Authenticator authenticator = getAuthenticator(context, configuration.value()); if (authenticator != null) { String username = authenticator.getUserName(context.context()); if (username == null) { context.context().request().setUsername(null); return authenticator.onUnauthorized(context.context()); } else { context.context().request().setUsername(username); return context.proceed(); } } else { context.context().request().setUsername(null); return Results.unauthorized(); } }  @Override Result call(Authenticated configuration, RequestContext context); @Override Class<Authenticated> annotation(); }
AuthenticationInterceptor extends Interceptor<Authenticated> { @Override public Result call(Authenticated configuration, RequestContext context) throws Exception { Authenticator authenticator = getAuthenticator(context, configuration.value()); if (authenticator != null) { String username = authenticator.getUserName(context.context()); if (username == null) { context.context().request().setUsername(null); return authenticator.onUnauthorized(context.context()); } else { context.context().request().setUsername(username); return context.proceed(); } } else { context.context().request().setUsername(null); return Results.unauthorized(); } }  @Override Result call(Authenticated configuration, RequestContext context); @Override Class<Authenticated> annotation();  }