Clover icon

sunshower-core

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

File DefaultAuthenticationService.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
10% of files have more coverage

Code metrics

4
13
2
1
54
46
5
0.38
6.5
2
2.5

Classes

Class Line # Actions
DefaultAuthenticationService 18 13 5
0.894736889.5%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    package io.sunshower.service.security;
2   
3    import io.sunshower.core.security.AuthenticationService;
4    import io.sunshower.core.security.InvalidCredentialException;
5    import io.sunshower.core.security.UserService;
6    import io.sunshower.core.security.crypto.EncryptionService;
7    import io.sunshower.model.core.auth.Authentication;
8    import io.sunshower.model.core.auth.Token;
9    import io.sunshower.model.core.auth.User;
10    import java.util.Date;
11    import javax.inject.Inject;
12    import org.springframework.security.core.userdetails.UsernameNotFoundException;
13    import org.springframework.stereotype.Service;
14    import org.springframework.transaction.annotation.Transactional;
15   
16    @Service
17    @Transactional
 
18    public class DefaultAuthenticationService implements AuthenticationService {
19   
20    @Inject private UserService userService;
21   
22    @Inject private EncryptionService encryptionService;
23   
 
24  3 toggle @Override
25    @Transactional(noRollbackFor = InvalidCredentialException.class)
26    public Authentication authenticate(User user) {
27  3 final String username = user.getUsername();
28  3 final String password = user.getPassword();
29  3 try {
30  3 final User u = userService.findByUsername(username);
31  3 if (encryptionService.matches(password, u.getPassword())) {
32  2 if (!u.isEnabled()) {
33  1 throw new InvalidCredentialException(
34    "Account has not been activated yet. "
35    + "If you believe "
36    + "this is in error, please contact your system administrator");
37    }
38  1 final String token = encryptionService.createToken(u);
39  1 return new Authentication(u, new Token(token, new Date()));
40    }
41    } catch (UsernameNotFoundException ex) {
42    // throw new UsernameNotFoundException(user.getUsername());
43  0 throw new InvalidCredentialException(ex);
44    }
45    // throw new UsernameNotFoundException(user.getUsername());
46  1 throw new InvalidCredentialException("Username or password combination is invalid");
47    }
48   
 
49  1 toggle @Override
50    public Authentication validate(Token token) {
51  1 final User user = encryptionService.findByToken(token.getToken());
52  0 return new Authentication(user, token);
53    }
54    }