@Override public Optional<Principal> authenticate(BasicCredentials basicCredentials) throws AuthenticationException { final UserId id = new UserId(basicCredentials.getUsername()); return readonlyUserDAO .getUserCredentialsById(id) .filter(BasicAuthenticator::hasCorrectAuthType) .filter(credentials -> matchesTheCredentialsSuppliedByTheClient(credentials, basicCredentials)) .map(UserCredentials::getId) .map(UserId::toString) .map(PrincipalImpl::new); }
@Override public Optional<Principal> authenticate(BasicCredentials basicCredentials) throws AuthenticationException { if (basicCredentials.getUsername().equals(username) && basicCredentials.getPassword().equals(password)) { return Optional.of(new PrincipalImpl(username)); } else return Optional.empty(); }
@Override public void filter(final ContainerRequestContext ctx) throws IOException { if (nonNull(ctx.getHeaders().getFirst(HttpHeaders.AUTHORIZATION))) { throw new WebApplicationException(unauthorizedHandler.buildResponse(prefix, realm)); } final SecurityContext securityContext = ctx.getSecurityContext(); final boolean secure = securityContext != null && securityContext.isSecure(); ctx.setSecurityContext(new SecurityContext() { @Override public Principal getUserPrincipal() { return new PrincipalImpl(Trellis.AnonymousAgent.getIRIString()); } @Override public boolean isUserInRole(final String role) { return false; } @Override public boolean isSecure() { return secure; } @Override public String getAuthenticationScheme() { return "NONE"; } }); }
@Override public void run(Configuration configuration, Environment environment) { environment.jersey().register(new LoginResource()); environment.jersey().register(new UserResource()); environment.jersey().register(new AuthDynamicFeature( new OAuthCredentialAuthFilter.Builder<PrincipalImpl>() .setAuthenticator(new TestOAuthAuthenticator()).setPrefix("Bearer") .buildAuthFilter())); environment.jersey().register(RolesAllowedDynamicFeature.class); environment.jersey().register(new AuthValueFactoryProvider.Binder<>(PrincipalImpl.class)); //TODO move this cleanup into the tests environment.lifecycle().manage(new Managed() { @Override public void start() { } @Override public void stop() { flushRedis(); } private void flushRedis() { try (StatefulRedisConnection<String, String> connection = redisClient.connect()) { connection.sync().flushdb(); } redisClient.shutdownAsync(); } }); }
@Override public Optional<PrincipalImpl> authenticate(String credentials) { if ("secret".equals(credentials)) { return Optional.of(new PrincipalImpl("elliot")); } return Optional.empty(); }
public static Authenticator<BasicCredentials, Principal> getBasicAuthenticator(final List<String> validUsers) { return credentials -> { if (validUsers.contains(credentials.getUsername()) && "secret".equals(credentials.getPassword())) { return Optional.<Principal>of(new PrincipalImpl(credentials.getUsername())); } if ("bad-guy".equals(credentials.getUsername())) { throw new AuthenticationException("CRAP"); } return Optional.empty(); }; }
public static Authenticator<String, Principal> getSingleUserOAuthAuthenticator(final String presented, final String returned) { return user -> { if (presented.equals(user)) { return Optional.<Principal>of(new PrincipalImpl(returned)); } if ("bad-guy".equals(user)) { throw new AuthenticationException("CRAP"); } return Optional.empty(); }; }
public static Authenticator<String, Principal> getMultiplyUsersOAuthAuthenticator(final List<String> validUsers) { return credentials -> { if (validUsers.contains(credentials)) { return Optional.<Principal>of(new PrincipalImpl(credentials)); } if (credentials.equals("bad-guy")) { throw new AuthenticationException("CRAP"); } return Optional.empty(); }; }
@Test public void cachesTheFirstReturnedPrincipal() throws Exception { assertThat(cached.authenticate("credentials")).isEqualTo(Optional.<Principal>of(new PrincipalImpl("principal"))); assertThat(cached.authenticate("credentials")).isEqualTo(Optional.<Principal>of(new PrincipalImpl("principal"))); verify(underlying, times(1)).authenticate("credentials"); }
public GuestAuthenticator(String guestUserName) { this.p = Optional.of(new PrincipalImpl(guestUserName)); }
@Override public Optional<Principal> authenticate(final BasicCredentials credentials) throws AuthenticationException { return lookup(credentials).map(PrincipalImpl::new); }
@Override public Optional<Principal> authenticate(final String credentials) throws AuthenticationException { return of(new PrincipalImpl(Trellis.AnonymousAgent.getIRIString())); }
@GET @Path("/{id}/authenticated") @RateLimited(key = Key.AUTHENTICATED, rates = {@Rate(duration = 10, timeUnit = TimeUnit.HOURS, limit = 10)}) public Response getLimitedByAuthenticatedUser(@Auth PrincipalImpl principle, @PathParam("id") final Integer id) { return Response.ok().build(); }
@Before public void setUp() throws Exception { when(underlying.authenticate(anyString())).thenReturn(Optional.<Principal>of(new PrincipalImpl("principal"))); cached = new CachingAuthenticator<>(new MetricRegistry(), underlying, CacheBuilderSpec.parse("maximumSize=1")); }