2012-02-12 06:55:42 +01:00
|
|
|
/*
|
2012-10-05 01:48:01 +02:00
|
|
|
* Copyright [2012] [wisemapping]
|
2012-02-12 06:55:42 +01:00
|
|
|
*
|
|
|
|
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
|
|
|
|
* It is basically the Apache License, Version 2.0 (the "License") plus the
|
|
|
|
* "powered by wisemapping" text requirement on every single page;
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the license at
|
|
|
|
*
|
|
|
|
* http://www.wisemapping.org/license
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.wisemapping.security;
|
|
|
|
|
2013-02-18 01:00:08 +01:00
|
|
|
|
2013-03-10 23:07:52 +01:00
|
|
|
import com.wisemapping.exceptions.WiseMappingException;
|
2013-03-17 22:51:33 +01:00
|
|
|
import com.wisemapping.model.AuthenticationSchema;
|
2012-06-23 21:15:59 +02:00
|
|
|
import com.wisemapping.model.User;
|
|
|
|
import com.wisemapping.service.UserService;
|
2012-02-12 06:55:42 +01:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
2012-02-21 20:36:19 +01:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2013-02-18 01:00:08 +01:00
|
|
|
|
2012-02-12 06:55:42 +01:00
|
|
|
import org.springframework.dao.DataAccessException;
|
|
|
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
2013-03-10 23:07:52 +01:00
|
|
|
import org.springframework.security.openid.OpenIDAttribute;
|
|
|
|
import org.springframework.security.openid.OpenIDAuthenticationToken;
|
|
|
|
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.List;
|
2012-02-12 06:55:42 +01:00
|
|
|
|
2012-02-13 01:57:11 +01:00
|
|
|
|
2012-02-21 20:36:19 +01:00
|
|
|
public class UserDetailsService
|
2013-03-10 23:07:52 +01:00
|
|
|
implements org.springframework.security.core.userdetails.UserDetailsService, org.springframework.security.core.userdetails.AuthenticationUserDetailsService<OpenIDAuthenticationToken> {
|
2012-06-23 21:15:59 +02:00
|
|
|
private UserService userService;
|
2012-02-21 20:36:19 +01:00
|
|
|
private String adminUser;
|
2012-02-12 06:55:42 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public UserDetails loadUserByUsername(@NotNull String email) throws UsernameNotFoundException, DataAccessException {
|
2012-06-23 21:15:59 +02:00
|
|
|
final User user = userService.getUserBy(email);
|
2013-03-10 23:07:52 +01:00
|
|
|
|
2012-06-23 21:15:59 +02:00
|
|
|
if (user != null) {
|
|
|
|
return new UserDetails(user, isAdmin(email));
|
2012-02-12 06:55:42 +01:00
|
|
|
} else {
|
|
|
|
throw new UsernameNotFoundException(email);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-10 23:07:52 +01:00
|
|
|
@Override
|
|
|
|
@NotNull
|
|
|
|
public UserDetails loadUserDetails(@NotNull OpenIDAuthenticationToken token) throws UsernameNotFoundException {
|
|
|
|
|
|
|
|
final User tUser = buildUserFromToken(token);
|
|
|
|
final User dbUser = userService.getUserBy(tUser.getEmail());
|
|
|
|
|
|
|
|
final User result;
|
|
|
|
if (dbUser != null) {
|
|
|
|
result = dbUser;
|
|
|
|
} else {
|
|
|
|
try {
|
2013-03-17 22:51:33 +01:00
|
|
|
tUser.setAuthenticationSchema(AuthenticationSchema.OPENID);
|
2013-03-10 23:07:52 +01:00
|
|
|
result = userService.createUser(tUser, false, false);
|
|
|
|
} catch (WiseMappingException e) {
|
|
|
|
throw new IllegalStateException(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return new UserDetails(result, isAdmin(result.getEmail()));
|
|
|
|
}
|
|
|
|
|
|
|
|
@NotNull
|
|
|
|
private User buildUserFromToken(@NotNull OpenIDAuthenticationToken token) {
|
|
|
|
final User result = new User();
|
|
|
|
|
|
|
|
final List<OpenIDAttribute> attributes = token.getAttributes();
|
|
|
|
for (OpenIDAttribute attribute : attributes) {
|
|
|
|
if (attribute.getName().equals("email")) {
|
|
|
|
final String email = attribute.getValues().get(0);
|
|
|
|
result.setEmail(email);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attribute.getName().equals("firstname")) {
|
|
|
|
final String firstName = attribute.getValues().get(0);
|
|
|
|
result.setFirstname(firstName);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attribute.getName().equals("lastname")) {
|
|
|
|
final String lastName = attribute.getValues().get(0);
|
|
|
|
result.setLastname(lastName);
|
|
|
|
}
|
|
|
|
result.setPassword("");
|
|
|
|
}
|
|
|
|
|
|
|
|
final Calendar now = Calendar.getInstance();
|
|
|
|
result.setActivationDate(now);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-02-21 20:36:19 +01:00
|
|
|
private boolean isAdmin(@Nullable String email) {
|
|
|
|
return email != null && adminUser != null && email.trim().endsWith(adminUser);
|
|
|
|
}
|
|
|
|
|
2012-06-23 21:15:59 +02:00
|
|
|
public UserService getUserService() {
|
|
|
|
return userService;
|
2012-02-12 06:55:42 +01:00
|
|
|
}
|
|
|
|
|
2012-06-23 21:15:59 +02:00
|
|
|
public void setUserService(UserService userManager) {
|
|
|
|
this.userService = userManager;
|
2012-02-12 06:55:42 +01:00
|
|
|
}
|
|
|
|
|
2012-02-21 20:36:19 +01:00
|
|
|
public String getAdminUser() {
|
|
|
|
return adminUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setAdminUser(String adminUser) {
|
|
|
|
this.adminUser = adminUser;
|
|
|
|
}
|
2013-03-10 23:07:52 +01:00
|
|
|
|
2012-02-12 06:55:42 +01:00
|
|
|
}
|