diff --git a/pom.xml b/pom.xml index 215a470a..5a13e178 100644 --- a/pom.xml +++ b/pom.xml @@ -80,6 +80,11 @@ spring-security-ldap ${org.springframework-version} + + org.springframework.security + spring-security-openid + ${org.springframework-version} + org.springframework spring-test diff --git a/src/main/java/info/bukova/isspst/services/users/GmailUserService.java b/src/main/java/info/bukova/isspst/services/users/GmailUserService.java new file mode 100644 index 00000000..0a81c17e --- /dev/null +++ b/src/main/java/info/bukova/isspst/services/users/GmailUserService.java @@ -0,0 +1,93 @@ +package info.bukova.isspst.services.users; + +import info.bukova.isspst.Constants; +import info.bukova.isspst.data.Role; +import info.bukova.isspst.data.User; + +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.security.core.userdetails.AuthenticationUserDetailsService; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.security.openid.OpenIDAttribute; +import org.springframework.security.openid.OpenIDAuthenticationToken; + +public class GmailUserService implements AuthenticationUserDetailsService { + + private static final Logger logger = LoggerFactory.getLogger(GmailUserService.class); + + private UserService userService; + private RoleService roleService; + private String restrictDomain; + + public GmailUserService(UserService userService, RoleService roleService) { + this.userService = userService; + this.roleService = roleService; + } + + @Override + public UserDetails loadUserDetails(OpenIDAuthenticationToken token) + throws UsernameNotFoundException { + + String email = null; + String firstName = null; + String lastName = null; + List attributes = token.getAttributes(); + + for (OpenIDAttribute attribute : attributes) { + if (attribute.getName().equals("email")) { + email = attribute.getValues().get(0); + } + if (attribute.getName().equals("firstName")) { + firstName = attribute.getValues().get(0); + } + if (attribute.getName().equals("lastName")) { + lastName = attribute.getValues().get(0); + } + } + + String userAndDomain[] = email.split("@"); + String username = userAndDomain[0]; + String domain = userAndDomain[1]; + + if (restrictDomain != null && !restrictDomain.isEmpty() && !restrictDomain.equals(domain)) { + logger.warn("Try to login from foreign domain"); + + throw new UsernameNotFoundException("Email from foreign domain"); + } + + UserDetails user; + + try { + user = userService.loadUserByUsername(username); + } catch (UsernameNotFoundException e) { + logger.info("Username not found in database. Creating one"); + + User usr = new User(); + usr.setUsername(username); + usr.setFirstName(firstName); + usr.setLastName(lastName); + usr.setEmail(email); + usr.setEnabled(true); + usr.setNotify(true); + + Role role = roleService.getRoleByAuthority(Constants.ROLE_USER); + usr.addAuthority(role); + + userService.grantAdmin(); + userService.add(usr); + userService.removeAccess(); + + user = userService.loadUserByUsername(username); + } + + return user; + } + + public void setRestrictDomain(String restrictDomain) { + this.restrictDomain = restrictDomain; + } + +} diff --git a/src/main/webapp/WEB-INF/gmail.properties b/src/main/webapp/WEB-INF/gmail.properties new file mode 100644 index 00000000..27ba5836 --- /dev/null +++ b/src/main/webapp/WEB-INF/gmail.properties @@ -0,0 +1 @@ +gmail.restrictDomain= \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/locales/zk-label.properties b/src/main/webapp/WEB-INF/locales/zk-label.properties index 5ce002d2..692dbcdc 100644 --- a/src/main/webapp/WEB-INF/locales/zk-label.properties +++ b/src/main/webapp/WEB-INF/locales/zk-label.properties @@ -272,6 +272,7 @@ Login=Přihlásit Loggingin=Přihlášení Logout=Odhlásit WrongNameOrPassword=Špatné jméno nebo heslo +LoginViaGoogle=Přihlásit účtem Google DateFormat=dd. MM. yyyy diff --git a/src/main/webapp/WEB-INF/spring/gmail-auth.xml b/src/main/webapp/WEB-INF/spring/gmail-auth.xml new file mode 100644 index 00000000..0f41a618 --- /dev/null +++ b/src/main/webapp/WEB-INF/spring/gmail-auth.xml @@ -0,0 +1,14 @@ + + + + + + + + + + diff --git a/src/main/webapp/WEB-INF/spring/ldap-auth.xml b/src/main/webapp/WEB-INF/spring/ldap-auth.xml index 5670d09f..1d3f7f06 100644 --- a/src/main/webapp/WEB-INF/spring/ldap-auth.xml +++ b/src/main/webapp/WEB-INF/spring/ldap-auth.xml @@ -19,12 +19,8 @@ - - - - ${ldap.userDNPattern} - - + + diff --git a/src/main/webapp/WEB-INF/spring/root-context.xml b/src/main/webapp/WEB-INF/spring/root-context.xml index 62af5814..181e3060 100644 --- a/src/main/webapp/WEB-INF/spring/root-context.xml +++ b/src/main/webapp/WEB-INF/spring/root-context.xml @@ -27,6 +27,7 @@ /WEB-INF/jdbc.properties /WEB-INF/ldap.properties /WEB-INF/mail.properties + /WEB-INF/gmail.properties @@ -36,7 +37,10 @@ class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" - p:password="${jdbc.password}"> + p:password="${jdbc.password}"> + + + @@ -81,16 +85,28 @@ - + + + + + + + + + + - + diff --git a/src/main/webapp/img/google.png b/src/main/webapp/img/google.png new file mode 100644 index 00000000..c911ded1 Binary files /dev/null and b/src/main/webapp/img/google.png differ diff --git a/src/main/webapp/login-gmail.zhtml b/src/main/webapp/login-gmail.zhtml new file mode 100644 index 00000000..b4c34243 --- /dev/null +++ b/src/main/webapp/login-gmail.zhtml @@ -0,0 +1,21 @@ + + + ${labels.Loggingin} + + +
+
+ +
+ Google +
+ + +
+
+ + +