supports(Class)   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
eloc 3
1
package unicon.matthews.admin;
2
3
import java.util.Collections;
4
import java.util.List;
5
6
import org.slf4j.Logger;
7
import org.slf4j.LoggerFactory;
8
import org.springframework.beans.factory.annotation.Autowired;
9
import org.springframework.security.authentication.AuthenticationProvider;
10
import org.springframework.security.core.Authentication;
11
import org.springframework.security.core.AuthenticationException;
12
import org.springframework.security.core.GrantedAuthority;
13
import org.springframework.security.core.authority.SimpleGrantedAuthority;
14
import org.springframework.stereotype.Component;
15
import org.springframework.util.Assert;
16
17
import unicon.matthews.admin.service.AdminUserService;
18
import unicon.matthews.oneroster.service.OrgService;
19
import unicon.matthews.security.model.UserContext;
20
21
@Component
22
public class AdminUserAuthenticationProvider implements AuthenticationProvider {
23
24
    private static Logger logger = LoggerFactory.getLogger(AdminUserAuthenticationProvider.class);
25
26
    @Autowired
27
    private AdminUserService adminUserService;
28
29
    @Autowired
30
    private OrgService orgService;
31
32
    /**
33
     * Authenticate admin user
34
     *
35
     * @param authentication
36
     * @return
37
     * @throws AuthenticationException
38
     */
39
    @Override
40
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
41
        Assert.notNull(authentication, "No authentication data provided");
42
        UserContext userContext = null;
43
        String username = (String) authentication.getPrincipal();
44
        String password = (String) authentication.getCredentials();
45
        try {
46
            AdminUser adminUser = adminUserService.authenticateUser(username, password);
47
            if (adminUser != null) {
48
                String tenantId = adminUser.getTenantId();
49
                String orgId = adminUser.getOrgId();
50
                if (adminUser.isSuperAdmin()) {
51
                    List<GrantedAuthority> authorities = Collections.singletonList(new SimpleGrantedAuthority("ROLE_SUPER_ADMIN"));
52
                    userContext = UserContext.create(tenantId, orgId, authorities);
53
                } else {
54
                    List<GrantedAuthority> authorities = Collections.singletonList(new SimpleGrantedAuthority("ROLE_ORG_ADMIN"));
55
                    userContext = UserContext.create(tenantId, orgId, authorities);
56
                }
57
            }
58
        } catch (AuthenticationException ex) {
59
            logger.error(String.format("Unable to authenticate user=%s. Invalid credentials supplied ", username) + ex.getMessage(), ex);
60
            throw ex;
61
        }
62
        return new AdminUserAuthenticationToken(userContext, null, userContext.getAuthorities());
0 ignored issues
show
Security Bug introduced by
A "NullPointerException" could be thrown; "userContext" is nullable here.
Loading history...
63
    }
64
65
    @Override
66
    public boolean supports(Class<?> authentication) {
67
        return (AdminUserAuthenticationToken.class.isAssignableFrom(authentication));
68
    }
69
}
70