GoogleProvider implements Provider<GooglePluginSettings> { @Override public boolean authorize(GooglePluginSettings pluginSettings, User user) { String username = user.getUsername(); if (isNotBlank(pluginSettings.getAllowedDomains())) { String allowedDomains = pluginSettings.getAllowedDomains(); String[] domains = allowedDomains.split(","); for (String domain : domains) { if (username.endsWith("@" + domain.trim())) { return true; } } return false; } if (isNotBlank(pluginSettings.getUsernameRegex())) { try { return username.matches(pluginSettings.getUsernameRegex().trim()); } catch (Exception e) { return false; } } return true; }  @Override String getPluginId(); @Override String getName(); @Override String getImageURL(); @Override String getProviderName(); @Override Permission getAuthPermission(); @Override User getUser(Profile profile); @Override List<User> searchUser(GooglePluginSettings pluginSettings, String searchTerm); @Override boolean authorize(GooglePluginSettings pluginSettings, User user); @Override Properties configure(GooglePluginSettings pluginSettings); @Override GooglePluginSettings pluginSettings(Map<String, String> responseBodyMap); @Override Map<String, Object> handleGetPluginSettings();  }
@Test public void authorizationWithAllowedDomains() throws Exception { GooglePluginSettings googlePluginSettings = new GooglePluginSettings(); googlePluginSettings.setAllowedDomains("foo.com, bar.com"); assertFalse(new GoogleProvider().authorize(googlePluginSettings, new User("", null, null))); assertFalse(new GoogleProvider().authorize(googlePluginSettings, new User("bob@foo.com@example.com", null, null))); assertTrue(new GoogleProvider().authorize(googlePluginSettings, new User("bob@foo.com", null, null))); assertTrue(new GoogleProvider().authorize(googlePluginSettings, new User("bob@bar.com", null, null))); }
@Test public void authorizationWithRegex() throws Exception { GooglePluginSettings googlePluginSettings = new GooglePluginSettings(); googlePluginSettings.setUsernameRegex(".*@(foo.com|bar.com)$"); assertFalse(new GoogleProvider().authorize(googlePluginSettings, new User("", null, null))); assertFalse(new GoogleProvider().authorize(googlePluginSettings, new User("bob@foo.com@example.com", null, null))); assertTrue(new GoogleProvider().authorize(googlePluginSettings, new User("bob@foo.com", null, null))); assertTrue(new GoogleProvider().authorize(googlePluginSettings, new User("bob@bar.com", null, null))); }