|
1
|
|
|
package unicon.matthews.security.auth; |
|
2
|
|
|
|
|
3
|
|
|
import java.util.Collection; |
|
4
|
|
|
|
|
5
|
|
|
import org.springframework.security.authentication.AbstractAuthenticationToken; |
|
6
|
|
|
import org.springframework.security.core.GrantedAuthority; |
|
7
|
|
|
|
|
8
|
|
|
import unicon.matthews.security.model.UserContext; |
|
9
|
|
|
import unicon.matthews.security.model.token.RawAccessJwtToken; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* An {@link org.springframework.security.core.Authentication} implementation |
|
13
|
|
|
* that is designed for simple presentation of JwtToken. |
|
14
|
|
|
* |
|
15
|
|
|
* @author vladimir.stankovic |
|
16
|
|
|
* |
|
17
|
|
|
* May 23, 2016 |
|
18
|
|
|
*/ |
|
19
|
|
|
public class JwtAuthenticationToken extends AbstractAuthenticationToken { |
|
|
|
|
|
|
20
|
|
|
private static final long serialVersionUID = 2877954820905567501L; |
|
21
|
|
|
|
|
22
|
|
|
private RawAccessJwtToken rawAccessToken; |
|
|
|
|
|
|
23
|
|
|
private UserContext userContext; |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
public JwtAuthenticationToken(RawAccessJwtToken unsafeToken) { |
|
26
|
|
|
super(null); |
|
27
|
|
|
this.rawAccessToken = unsafeToken; |
|
28
|
|
|
this.setAuthenticated(false); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public JwtAuthenticationToken(UserContext userContext, Collection<? extends GrantedAuthority> authorities) { |
|
32
|
|
|
super(authorities); |
|
33
|
|
|
this.eraseCredentials(); |
|
34
|
|
|
this.userContext = userContext; |
|
35
|
|
|
super.setAuthenticated(true); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
@Override |
|
39
|
|
|
public void setAuthenticated(boolean authenticated) { |
|
40
|
|
|
if (authenticated) { |
|
41
|
|
|
throw new IllegalArgumentException( |
|
42
|
|
|
"Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead"); |
|
43
|
|
|
} |
|
44
|
|
|
super.setAuthenticated(false); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
@Override |
|
48
|
|
|
public Object getCredentials() { |
|
49
|
|
|
return rawAccessToken; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
@Override |
|
53
|
|
|
public Object getPrincipal() { |
|
54
|
|
|
return this.userContext; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
@Override |
|
58
|
|
|
public void eraseCredentials() { |
|
59
|
|
|
super.eraseCredentials(); |
|
60
|
|
|
this.rawAccessToken = null; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|