unicon.matthews.security.auth.JwtAuthenticationToken   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 42
rs 10
eloc 29
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getPrincipal() 0 3 1
A JwtAuthenticationToken(UserContext,Collection) 0 5 1
A getCredentials() 0 3 1
A setAuthenticated(boolean) 0 7 2
A JwtAuthenticationToken(RawAccessJwtToken) 0 4 1
A eraseCredentials() 0 4 1
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 {
0 ignored issues
show
Bug introduced by
Override the "equals" method in this class.
Loading history...
20
    private static final long serialVersionUID = 2877954820905567501L;
21
22
    private RawAccessJwtToken rawAccessToken;
0 ignored issues
show
Bug Best Practice introduced by
Fields like rawAccessToken in a serializable class should either be transient or serializable.
Loading history...
23
    private UserContext userContext;
0 ignored issues
show
Bug Best Practice introduced by
Fields like userContext in a serializable class should either be transient or serializable.
Loading history...
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