Clover icon

sunshower-core

  1. Project Clover database Fri Apr 6 2018 03:27:42 UTC
  2. Package io.sunshower.service.security

File AuthenticationSession.java

 

Coverage histogram

../../../../img/srcFileCovDistChart2.png
54% of files have more coverage

Code metrics

8
25
15
1
106
89
20
0.8
1.67
15
1.33

Classes

Class Line # Actions
AuthenticationSession 14 25 20
0.2083333320.8%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    package io.sunshower.service.security;
2   
3    import io.sunshower.common.Identifier;
4    import io.sunshower.model.core.auth.User;
5    import io.sunshower.persistence.core.Persistable;
6    import java.io.Serializable;
7    import java.util.Collection;
8    import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
9    import org.springframework.security.core.Authentication;
10    import org.springframework.security.core.GrantedAuthority;
11    import org.springframework.security.core.context.SecurityContextHolder;
12    import org.springframework.security.core.userdetails.UserDetails;
13   
 
14    public class AuthenticationSession implements Session {
15   
 
16  0 toggle @Override
17    @SuppressWarnings("unchecked")
18    public <T extends Serializable> T getId() {
19  0 Object principal = getPrincipal();
20  0 if (principal != null && principal instanceof User) {
21  0 return ((Persistable<T>) principal).getId();
22    }
23  0 return (T) Identifier.random();
24    }
25   
 
26  0 toggle @Override
27    public Collection<? extends GrantedAuthority> getAuthorities() {
28  0 return unwrap(UserDetails.class).getAuthorities();
29    }
30   
 
31  0 toggle @Override
32    public Object getCredentials() {
33  0 return unwrap(Authentication.class).getCredentials();
34    }
35   
 
36  0 toggle @Override
37    public Object getDetails() {
38  0 return unwrap(Authentication.class).getDetails();
39    }
40   
 
41  0 toggle @Override
42    public Object getPrincipal() {
43  0 final Authentication result = unwrap(Authentication.class);
44  0 if (result == null) {
45  0 throw new AuthenticationCredentialsNotFoundException("No session");
46    }
47  0 return result.getPrincipal();
48    }
49   
 
50  0 toggle @Override
51    public boolean isAuthenticated() {
52  0 return unwrap(Authentication.class).isAuthenticated();
53    }
54   
 
55  0 toggle @Override
56    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
57  0 unwrap(Authentication.class).setAuthenticated(isAuthenticated);
58    }
59   
 
60  0 toggle @Override
61    public String getPassword() {
62  0 return unwrap(UserDetails.class).getPassword();
63    }
64   
 
65  1 toggle @Override
66    public String getUsername() {
67  1 return unwrap(UserDetails.class).getUsername();
68    }
69   
 
70  0 toggle @Override
71    public boolean isAccountNonExpired() {
72  0 return unwrap(UserDetails.class).isAccountNonExpired();
73    }
74   
 
75  0 toggle @Override
76    public boolean isAccountNonLocked() {
77  0 return unwrap(UserDetails.class).isAccountNonLocked();
78    }
79   
 
80  0 toggle @Override
81    public boolean isCredentialsNonExpired() {
82  0 return unwrap(UserDetails.class).isCredentialsNonExpired();
83    }
84   
 
85  0 toggle @Override
86    public boolean isEnabled() {
87  0 return unwrap(UserDetails.class).isEnabled();
88    }
89   
 
90  2 toggle @Override
91    @SuppressWarnings("unchecked")
92    public <T> T unwrap(Class<T> type) {
93  2 if (Authentication.class.isAssignableFrom(type)) {
94  1 return (T) SecurityContextHolder.getContext().getAuthentication();
95    }
96  1 if (UserDetails.class.isAssignableFrom(type)) {
97  1 return (T) unwrap(Authentication.class).getPrincipal();
98    }
99  0 return null;
100    }
101   
 
102  0 toggle @Override
103    public String getName() {
104  0 return null;
105    }
106    }