@Override protected BasicCredentialAuthFilter<P> newInstance() { return new BasicCredentialAuthFilter<>(); } }
public GithubWebhookAuthenticator(String username, String password) { this.correctCredentials = new BasicCredentials(username, password); }
@Override public Optional<User> authenticate(BasicCredentials credentials) throws AuthenticationException { if (VALID_USERS.containsKey(credentials.getUsername()) && "secret".equals(credentials.getPassword())) { return Optional.of(new User(credentials.getUsername(), VALID_USERS.get(credentials.getUsername()))); } return Optional.empty(); } }
@Override public void run(HelloWorldConfiguration configuration, Environment environment) { final PersonDAO dao = new PersonDAO(hibernateBundle.getSessionFactory()); final Template template = configuration.buildTemplate(); environment.healthChecks().register("template", new TemplateHealthCheck(template)); environment.admin().addTask(new EchoTask()); environment.jersey().register(DateRequiredFeature.class); environment.jersey().register(new AuthDynamicFeature(new BasicCredentialAuthFilter.Builder<User>() .setAuthenticator(new ExampleAuthenticator()) .setAuthorizer(new ExampleAuthorizer()) .setRealm("SUPER SECRET STUFF") .buildAuthFilter())); environment.jersey().register(new AuthValueFactoryProvider.Binder<>(User.class)); environment.jersey().register(RolesAllowedDynamicFeature.class); environment.jersey().register(new HelloWorldResource(template)); environment.jersey().register(new ViewResource()); environment.jersey().register(new ProtectedResource()); environment.jersey().register(new PeopleResource(dao)); environment.jersey().register(new PersonResource(dao)); environment.jersey().register(new FilteredResource()); } }
@Override public void filter(ContainerRequestContext requestContext) throws IOException { final BasicCredentials credentials = getCredentials(requestContext.getHeaders().getFirst(HttpHeaders.AUTHORIZATION)); if (!authenticate(requestContext, credentials, SecurityContext.BASIC_AUTH)) { throw new WebApplicationException(unauthorizedHandler.buildResponse(prefix, realm)); } }
@Override public Optional<Authentication> authenticate(BasicCredentials clientCredentials) { if (correctCredentials.equals(clientCredentials)) { return Optional.of(new Authentication()); } else { return Optional.absent(); } } }
@Override public void run(BithubServerConfiguration config, Environment environment) throws Exception { String githubUser = config.getGithubConfiguration().getUser(); String githubToken = config.getGithubConfiguration().getToken(); String githubWebhookUser = config.getGithubConfiguration().getWebhookConfiguration().getUsername(); String githubWebhookPwd = config.getGithubConfiguration().getWebhookConfiguration().getPassword(); List<RepositoryConfiguration> githubRepositories = config.getGithubConfiguration().getRepositories(); BigDecimal payoutRate = config.getBithubConfiguration().getPayoutRate(); String organizationName = config.getOrganizationConfiguration().getName(); String donationUrl = config.getOrganizationConfiguration().getDonationUrl().toExternalForm(); String coinbaseApiKey = config.getCoinbaseConfiguration().getApiKey(); String coinbaseApiSecret = config.getCoinbaseConfiguration().getApiSecret(); GithubClient githubClient = new GithubClient(githubUser, githubToken); CoinbaseClient coinbaseClient = new CoinbaseClient(coinbaseApiKey, coinbaseApiSecret); CacheManager cacheManager = new CacheManager(coinbaseClient, githubClient, githubRepositories, payoutRate); environment.servlets().addFilter("CORS", CrossOriginFilter.class) .addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*"); environment.lifecycle().manage(cacheManager); environment.jersey().register(new GithubController(githubRepositories, githubClient, coinbaseClient, payoutRate)); environment.jersey().register(new StatusController(cacheManager, githubRepositories)); environment.jersey().register(new DashboardController(organizationName, donationUrl, cacheManager)); environment.jersey().register(new IOExceptionMapper()); environment.jersey().register(new UnauthorizedHookExceptionMapper()); environment.jersey().register(new BasicAuthProvider<>(new GithubWebhookAuthenticator(githubWebhookUser, githubWebhookPwd), GithubWebhookAuthenticator.REALM)); }
import com.google.common.base.Optional; import io.dropwizard.auth.AuthenticationException; import io.dropwizard.auth.Authenticator; import io.dropwizard.auth.basic.BasicCredentials; public class SimpleAuthenticator implements Authenticator<BasicCredentials, SimplePrincipal> { @Override public Optional<SimplePrincipal> authenticate(BasicCredentials credentials) throws AuthenticationException { // Note: this is horrible authentication. Normally we'd use some // service to identify the password from the user name. if (!"pass".equals(credentials.getPassword())) { throw new AuthenticationException("Boo Hooo!"); } // from some user service get the roles for this user // I am explicitly setting it just for simplicity SimplePrincipal prince = new SimplePrincipal(credentials.getUsername()); prince.getRoles().add(Roles.ADMIN); return Optional.fromNullable(prince); } }
return new BasicCredentials(username, password);
ProfileController profileController = new ProfileController(rateLimiters , accountsManager, config.getProfilesConfiguration()); environment.jersey().register(new AuthDynamicFeature(new BasicCredentialAuthFilter.Builder<Account>() .setAuthenticator(deviceAuthenticator) .buildAuthFilter())); environment.jersey().register(new AuthValueFactoryProvider.Binder<>(Account.class));
@Override protected BasicCredentialAuthFilter<P> newInstance() { return new BasicCredentialAuthFilter<>(); } }
@Override public Optional<Account> authenticate(BasicCredentials basicCredentials) throws AuthenticationException { try { AuthorizationHeader authorizationHeader = AuthorizationHeader.fromUserAndPassword(basicCredentials.getUsername(), basicCredentials.getPassword()); Optional<Account> account = accountsManager.get(authorizationHeader.getNumber()); if (!account.isPresent()) { return Optional.empty(); } Optional<Device> device = account.get().getDevice(authorizationHeader.getDeviceId()); if (!device.isPresent()) { return Optional.empty(); } if (!device.get().isMaster() && device.get().isIdleInactive()) { return Optional.empty(); } if (device.get().getAuthenticationCredentials().verify(basicCredentials.getPassword())) { authenticationSucceededMeter.mark(); account.get().setAuthenticatedDevice(device.get()); updateLastSeen(account.get(), device.get()); return account; } authenticationFailedMeter.mark(); return Optional.empty(); } catch (InvalidAuthorizationHeaderException iahe) { return Optional.empty(); } }
@Override public AuthenticationResult<Account> authenticate(UpgradeRequest request) throws AuthenticationException { try { Map<String, List<String>> parameters = request.getParameterMap(); List<String> usernames = parameters.get("login"); List<String> passwords = parameters.get("password"); if (usernames == null || usernames.size() == 0 || passwords == null || passwords.size() == 0) { return new AuthenticationResult<>(Optional.empty(), false); } BasicCredentials credentials = new BasicCredentials(usernames.get(0).replace(" ", "+"), passwords.get(0).replace(" ", "+")); return new AuthenticationResult<>(accountAuthenticator.authenticate(credentials), true); } catch (io.dropwizard.auth.AuthenticationException e) { throw new AuthenticationException(e); } }
/** * Creates a new BasicCredentials with the given username and password. * * @param credentials instance of BasicCredentials to wrap */ public SmartCosmosCredentials(BasicCredentials credentials) { super(credentials.getUsername(), credentials.getPassword()); }
@PropertySource("classpath:AwsCredentials.properties") public class AWSConfiguration { @Autowired private Environment env; @Bean public AwsCredentials credentials() { String secretKey = env.getRequiredProperty("name of property"); String accessKey = env.getRequiredProperty("name of property"); return new BasicCredentials(accessKey, secretKey); } }
public class AuthenticationService implements Authenticator<BasicCredentials, User>, Authorizer<User> { private final UserDAO userDAO; public AuthenticationService(UserDAO userDAO) { this.userDAO = userDAO; } public Optional<User> authenticate(BasicCredentials credentials) throws AuthenticationException { User user = userDAO.getByEmail(credentials.getUsername()); if (user != null && user.getPassword().equals(credentials.getPassword())) { return Optional.of(user); } return Optional.absent(); } public boolean authorize(User user, String role) { return user.hasRole(role); } }
@Override public Result check() throws AuthenticationException { if (ldapAuthenticator.authenticate(new BasicCredentials("", "")).isPresent()) { return Result.healthy(); } else { return Result.unhealthy("Cannot contact authentication service"); } } }
public class BasicAuthenticator implements Authenticator<BasicCredentials, Subject> { @Override public Optional<Subject> authenticate(BasicCredentials credentials) throws AuthenticationException { Subject subject = SecurityUtils.getSubject(); try { subject.login(new UsernamePasswordToken(credentials.getUsername(), credentials.getPassword(), false)); return Optional.of(subject); } catch (UnknownAccountException | IncorrectCredentialsException | LockedAccountException e) { } catch (AuthenticationException ae) { } return Optional.absent(); } }
@Override public Optional<HelloAccount> authenticate(BasicCredentials credentials) throws AuthenticationException { if (credentials.getUsername().equals("moxie") && credentials.getPassword().equals("insecure")) { return Optional.of(new HelloAccount("moxie")); } return Optional.empty(); } }
@Override public Optional<User> authenticate(BasicCredentials credentials) throws AuthenticationException { if ("secret".equals(credentials.getPassword())) { return Optional.of(new User(credentials.getUsername())); } return Optional.empty(); } }