1 package cn.home1.oss.lib.security.internal;
2
3 import static cn.home1.oss.lib.security.api.GenericUser.isGenericUser;
4
5 import com.google.common.collect.ImmutableSet;
6
7 import cn.home1.oss.lib.security.api.GenericUser;
8 import cn.home1.oss.lib.webmvc.api.TypeSafeToken;
9
10 import org.springframework.security.core.Authentication;
11 import org.springframework.security.core.GrantedAuthority;
12 import org.springframework.security.core.context.SecurityContext;
13 import org.springframework.security.core.context.SecurityContextHolder;
14 import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;
15
16 import java.util.Collection;
17
18
19
20
21 @SuppressWarnings({"PMD.AbstractNaming", "PMD.AbstractClassWithoutAbstractMethod"})
22 public abstract class AuthenticationTokens {
23
24 private AuthenticationTokens() {
25 }
26
27 public static PreAuthenticatedAuthenticationToken authenticated(
28 final PreAuthenticatedAuthenticationToken authentication
29 ) {
30 final PreAuthenticatedAuthenticationToken result;
31
32 if (authentication == null) {
33 result = null;
34 } else {
35 authentication.setAuthenticated(true);
36 authentication.eraseCredentials();
37
38 final Object principal = authentication.getPrincipal();
39 final Object credentials = authentication.getCredentials();
40
41 final GenericUser genericUser = isGenericUser(principal) ? (GenericUser) principal : null;
42
43 if (genericUser == null) {
44 result = authentication;
45 } else {
46 genericUser.eraseCredentials();
47 final Collection<GrantedAuthority> authorities = genericUser.getAuthorities() != null ?
48 genericUser.getAuthorities() : ImmutableSet.of();
49 result = new PreAuthenticatedAuthenticationToken(principal, credentials, authorities);
50 }
51 }
52 return result;
53 }
54
55 public static String rawToken(final Authentication authentication, final TypeSafeToken<GenericUser> token) {
56
57
58 final String result;
59 if (authentication instanceof PreAuthenticatedAuthenticationToken) {
60 final Object principal = authentication.getPrincipal();
61 final GenericUser genericUser = isGenericUser(principal) ? (GenericUser) principal : null;
62 result = genericUser != null ? (String) authentication.getCredentials() : null;
63 } else {
64 final GenericUser genericUser = GenericUser.fromPrincipal(authentication);
65 result = token.toToken(genericUser);
66 }
67 return result;
68 }
69
70 public static Boolean tokenInSecurityContext() {
71 final SecurityContext securityContext = SecurityContextHolder.getContext();
72 final Authentication authentication = securityContext.getAuthentication();
73 return authentication != null && authentication.isAuthenticated();
74 }
75
76 public static String tokenFromSecurityContext(final TypeSafeToken<GenericUser> token) {
77 final SecurityContext securityContext = SecurityContextHolder.getContext();
78 final Authentication authentication = securityContext.getAuthentication();
79
80 final String result;
81 if (authentication != null && authentication.isAuthenticated()) {
82 result = AuthenticationTokens.rawToken(authentication, token);
83 } else {
84 result = null;
85 }
86
87 return result;
88 }
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105 }