|
1 | | -package com.naturalprogrammer.spring.lemon.security; |
2 | | - |
3 | | -import java.io.Serializable; |
4 | | -import java.nio.charset.StandardCharsets; |
5 | | -import java.util.ArrayList; |
6 | | -import java.util.Collections; |
7 | | -import java.util.List; |
8 | | -import java.util.Map; |
9 | | - |
10 | | -import org.apache.commons.lang3.exception.ExceptionUtils; |
11 | | -import org.apache.commons.logging.Log; |
12 | | -import org.apache.commons.logging.LogFactory; |
13 | | -import org.springframework.http.MediaType; |
14 | | -import org.springframework.http.converter.HttpMessageConverter; |
15 | | -import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; |
16 | | -import org.springframework.security.crypto.password.PasswordEncoder; |
17 | | -import org.springframework.security.oauth2.client.http.OAuth2ErrorResponseErrorHandler; |
18 | | -import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService; |
19 | | -import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest; |
20 | | -import org.springframework.security.oauth2.core.OAuth2AuthenticationException; |
21 | | -import org.springframework.security.oauth2.core.user.OAuth2User; |
22 | | -import org.springframework.util.MimeType; |
23 | | -import org.springframework.web.client.RestTemplate; |
24 | | - |
25 | | -import com.naturalprogrammer.spring.lemon.LemonService; |
26 | | -import com.naturalprogrammer.spring.lemon.commons.security.LemonPrincipal; |
27 | | -import com.naturalprogrammer.spring.lemon.commons.security.UserDto; |
28 | | -import com.naturalprogrammer.spring.lemon.commons.util.LecUtils; |
29 | | -import com.naturalprogrammer.spring.lemon.domain.AbstractUser; |
30 | | -import com.naturalprogrammer.spring.lemon.exceptions.util.LexUtils; |
31 | | - |
32 | | -/** |
33 | | - * Logs in or registers a user after OAuth2 SignIn/Up |
34 | | - */ |
35 | | -public class LemonOAuth2UserService<U extends AbstractUser<ID>, ID extends Serializable> extends DefaultOAuth2UserService { |
36 | | - |
37 | | - private static final Log log = LogFactory.getLog(LemonOAuth2UserService.class); |
38 | | - |
39 | | - private LemonUserDetailsService<U, ?> userDetailsService; |
40 | | - private LemonService<U, ?> lemonService; |
41 | | - private PasswordEncoder passwordEncoder; |
42 | | - |
43 | | - public LemonOAuth2UserService( |
44 | | - LemonUserDetailsService<U, ?> userDetailsService, |
45 | | - LemonService<U, ?> lemonService, |
46 | | - PasswordEncoder passwordEncoder) { |
47 | | - |
48 | | - this.userDetailsService = userDetailsService; |
49 | | - this.lemonService = lemonService; |
50 | | - this.passwordEncoder = passwordEncoder; |
51 | | - |
52 | | - replaceRestOperarions(); |
53 | | - log.info("Created"); |
54 | | - } |
55 | | - |
56 | | - protected void replaceRestOperarions() { |
57 | | - |
58 | | - RestTemplate restTemplate = new RestTemplate(); |
59 | | - restTemplate.setErrorHandler(new OAuth2ErrorResponseErrorHandler()); |
60 | | - restTemplate.setMessageConverters(makeMessageConverters()); |
61 | | - setRestOperations(restTemplate); |
62 | | - |
63 | | - log.info("Rest Operations replaced"); |
64 | | - } |
65 | | - |
66 | | - protected List<HttpMessageConverter<?>> makeMessageConverters() { |
67 | | - |
68 | | - log.info("Making message converters"); |
69 | | - |
70 | | - MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); |
71 | | - |
72 | | - List<MediaType> mediaTypes = new ArrayList<>(converter.getSupportedMediaTypes()); |
73 | | - mediaTypes.add(MediaType.asMediaType(new MimeType("text", "javascript", StandardCharsets.UTF_8))); // Facebook returns text/javascript |
74 | | - |
75 | | - converter.setSupportedMediaTypes(mediaTypes); |
76 | | - return Collections.singletonList(converter); |
77 | | - } |
78 | | - |
79 | | - @Override |
80 | | - public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException { |
81 | | - |
82 | | - OAuth2User oath2User = super.loadUser(userRequest); |
83 | | - return buildPrincipal(oath2User, userRequest.getClientRegistration().getRegistrationId()); |
84 | | - } |
85 | | - |
86 | | - /** |
87 | | - * Builds the security principal from the given userReqest. |
88 | | - * Registers the user if not already reqistered |
89 | | - */ |
90 | | - public LemonPrincipal buildPrincipal(OAuth2User oath2User, String registrationId) { |
91 | | - |
92 | | - Map<String, Object> attributes = oath2User.getAttributes(); |
93 | | - String email = lemonService.getOAuth2Email(registrationId, attributes); |
94 | | - LexUtils.validate(email != null, "com.naturalprogrammer.spring.oauth2EmailNeeded", registrationId).go(); |
95 | | - |
96 | | - boolean emailVerified = lemonService.getOAuth2AccountVerified(registrationId, attributes); |
97 | | - LexUtils.validate(emailVerified, "com.naturalprogrammer.spring.oauth2EmailNotVerified", registrationId).go(); |
98 | | - |
99 | | - U user = userDetailsService.findUserByUsername(email).orElseGet(() -> { |
100 | | - |
101 | | - // register a new user |
102 | | - U newUser = lemonService.newUser(); |
103 | | - newUser.setEmail(email); |
104 | | - newUser.setPassword(passwordEncoder.encode(LecUtils.uid())); |
105 | | - |
106 | | - lemonService.fillAdditionalFields(registrationId, newUser, attributes); |
107 | | - lemonService.save(newUser); |
108 | | - |
109 | | - try { |
110 | | - |
111 | | - lemonService.mailForgotPasswordLink(newUser); |
112 | | - |
113 | | - } catch (Throwable e) { |
114 | | - |
115 | | - // In case of exception, just log the error and keep silent |
116 | | - log.error(ExceptionUtils.getStackTrace(e)); |
117 | | - } |
118 | | - |
119 | | - return newUser; |
120 | | - }); |
121 | | - |
122 | | - UserDto userDto = user.toUserDto(); |
123 | | - LemonPrincipal principal = new LemonPrincipal(userDto); |
124 | | - principal.setAttributes(attributes); |
125 | | - principal.setName(oath2User.getName()); |
126 | | - |
127 | | - return principal; |
128 | | - } |
129 | | -} |
| 1 | +package com.naturalprogrammer.spring.lemon.security; |
| 2 | + |
| 3 | +import java.io.Serializable; |
| 4 | +import java.nio.charset.StandardCharsets; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Collections; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +import org.apache.commons.lang3.exception.ExceptionUtils; |
| 11 | +import org.apache.commons.logging.Log; |
| 12 | +import org.apache.commons.logging.LogFactory; |
| 13 | +import org.springframework.http.MediaType; |
| 14 | +import org.springframework.http.converter.HttpMessageConverter; |
| 15 | +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; |
| 16 | +import org.springframework.security.crypto.password.PasswordEncoder; |
| 17 | +import org.springframework.security.oauth2.client.http.OAuth2ErrorResponseErrorHandler; |
| 18 | +import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService; |
| 19 | +import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest; |
| 20 | +import org.springframework.security.oauth2.core.OAuth2AuthenticationException; |
| 21 | +import org.springframework.security.oauth2.core.user.OAuth2User; |
| 22 | +import org.springframework.util.MimeType; |
| 23 | +import org.springframework.web.client.RestTemplate; |
| 24 | + |
| 25 | +import com.naturalprogrammer.spring.lemon.LemonService; |
| 26 | +import com.naturalprogrammer.spring.lemon.commons.security.LemonPrincipal; |
| 27 | +import com.naturalprogrammer.spring.lemon.commons.security.UserDto; |
| 28 | +import com.naturalprogrammer.spring.lemon.commons.util.LecUtils; |
| 29 | +import com.naturalprogrammer.spring.lemon.domain.AbstractUser; |
| 30 | +import com.naturalprogrammer.spring.lemon.exceptions.util.LexUtils; |
| 31 | + |
| 32 | +/** |
| 33 | + * Logs in or registers a user after OAuth2 SignIn/Up |
| 34 | + */ |
| 35 | +public class LemonOAuth2UserService<U extends AbstractUser<ID>, ID extends Serializable> extends DefaultOAuth2UserService { |
| 36 | + |
| 37 | + private static final Log log = LogFactory.getLog(LemonOAuth2UserService.class); |
| 38 | + |
| 39 | + private LemonUserDetailsService<U, ?> userDetailsService; |
| 40 | + private LemonService<U, ?> lemonService; |
| 41 | + private PasswordEncoder passwordEncoder; |
| 42 | + |
| 43 | + public LemonOAuth2UserService( |
| 44 | + LemonUserDetailsService<U, ?> userDetailsService, |
| 45 | + LemonService<U, ?> lemonService, |
| 46 | + PasswordEncoder passwordEncoder) { |
| 47 | + |
| 48 | + this.userDetailsService = userDetailsService; |
| 49 | + this.lemonService = lemonService; |
| 50 | + this.passwordEncoder = passwordEncoder; |
| 51 | + |
| 52 | + replaceRestOperarions(); |
| 53 | + log.info("Created"); |
| 54 | + } |
| 55 | + |
| 56 | + protected void replaceRestOperarions() { |
| 57 | + |
| 58 | + RestTemplate restTemplate = new RestTemplate(); |
| 59 | + restTemplate.setErrorHandler(new OAuth2ErrorResponseErrorHandler()); |
| 60 | + restTemplate.setMessageConverters(makeMessageConverters()); |
| 61 | + setRestOperations(restTemplate); |
| 62 | + |
| 63 | + log.info("Rest Operations replaced"); |
| 64 | + } |
| 65 | + |
| 66 | + protected List<HttpMessageConverter<?>> makeMessageConverters() { |
| 67 | + |
| 68 | + log.info("Making message converters"); |
| 69 | + |
| 70 | + MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); |
| 71 | + |
| 72 | + List<MediaType> mediaTypes = new ArrayList<>(converter.getSupportedMediaTypes()); |
| 73 | + mediaTypes.add(MediaType.asMediaType(new MimeType("text", "javascript", StandardCharsets.UTF_8))); // Facebook returns text/javascript |
| 74 | + |
| 75 | + converter.setSupportedMediaTypes(mediaTypes); |
| 76 | + return Collections.singletonList(converter); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException { |
| 81 | + |
| 82 | + OAuth2User oath2User = super.loadUser(userRequest); |
| 83 | + return buildPrincipal(oath2User, userRequest.getClientRegistration().getRegistrationId()); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Builds the security principal from the given userReqest. |
| 88 | + * Registers the user if not already registered |
| 89 | + */ |
| 90 | + public LemonPrincipal buildPrincipal(OAuth2User oath2User, String registrationId) { |
| 91 | + |
| 92 | + Map<String, Object> attributes = oath2User.getAttributes(); |
| 93 | + String email = lemonService.getOAuth2Email(registrationId, attributes); |
| 94 | + LexUtils.validate(email != null, "com.naturalprogrammer.spring.oauth2EmailNeeded", registrationId).go(); |
| 95 | + |
| 96 | + boolean emailVerified = lemonService.getOAuth2AccountVerified(registrationId, attributes); |
| 97 | + LexUtils.validate(emailVerified, "com.naturalprogrammer.spring.oauth2EmailNotVerified", registrationId).go(); |
| 98 | + |
| 99 | + U user = userDetailsService.findUserByUsername(email).orElseGet(() -> { |
| 100 | + |
| 101 | + // register a new user |
| 102 | + U newUser = lemonService.newUser(); |
| 103 | + newUser.setEmail(email); |
| 104 | + newUser.setPassword(passwordEncoder.encode(LecUtils.uid())); |
| 105 | + |
| 106 | + lemonService.fillAdditionalFields(registrationId, newUser, attributes); |
| 107 | + lemonService.save(newUser); |
| 108 | + |
| 109 | + try { |
| 110 | + |
| 111 | + lemonService.mailForgotPasswordLink(newUser); |
| 112 | + |
| 113 | + } catch (Throwable e) { |
| 114 | + |
| 115 | + // In case of exception, just log the error and keep silent |
| 116 | + log.error(ExceptionUtils.getStackTrace(e)); |
| 117 | + } |
| 118 | + |
| 119 | + return newUser; |
| 120 | + }); |
| 121 | + |
| 122 | + UserDto userDto = user.toUserDto(); |
| 123 | + LemonPrincipal principal = new LemonPrincipal(userDto); |
| 124 | + principal.setAttributes(attributes); |
| 125 | + principal.setName(oath2User.getName()); |
| 126 | + |
| 127 | + return principal; |
| 128 | + } |
| 129 | +} |
0 commit comments