Search in sources :

Example 1 with UserAuth

use of amu.zhcet.auth.UserAuth in project zhcet-web by zhcet-amu.

the class FirebaseAuthenticationProvider method authenticate.

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    if (!firebaseService.canProceed())
        // Firebase is disabled, so we cannot proceed
        return null;
    String token = authentication.getCredentials().toString();
    if (Strings.isNullOrEmpty(token))
        // Cannot parse empty token
        return null;
    try {
        FirebaseToken decodedToken = FirebaseService.getToken(token);
        log.debug("User Claims: {}", decodedToken.getClaims());
        UserDetails user = retrieveUser(decodedToken);
        if (user == null)
            throwBadCredentialsException();
        userDetailsChecker.check(user);
        if (user instanceof UserAuth) {
            firebaseAccountMergeService.mergeFirebaseDetails((UserAuth) user, decodedToken);
        } else {
            log.warn("User {} is not of UserAuth Type", user);
        }
        return createSuccessAuthentication(user, authentication);
    } catch (InterruptedException | ExecutionException e) {
        log.warn("Unable to decode Firebase token");
        throwBadCredentialsException();
    } catch (UsernameNotFoundException une) {
        throwBadCredentialsException();
    }
    return null;
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) FirebaseToken(com.google.firebase.auth.FirebaseToken) UserDetails(org.springframework.security.core.userdetails.UserDetails) UserAuth(amu.zhcet.auth.UserAuth) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with UserAuth

use of amu.zhcet.auth.UserAuth in project zhcet-web by zhcet-amu.

the class TokenGrantService method generateToken.

/**
 * Generates custom firebase token for authenticated user
 * Note: Only to be called from an authenticated endpoint
 * @return UserToken
 */
@Transactional
public UserToken generateToken() {
    if (!firebaseService.canProceed())
        return null;
    try {
        Optional<UserAuth> userOptional = Auditor.getLoggedInUser();
        if (!userOptional.isPresent())
            return UNAUTHENTICATED;
        UserAuth user = userOptional.get();
        Map<String, Object> claims = new HashMap<>();
        claims.put("type", user.getType().toString());
        claims.put("department", user.getDepartment().getName());
        claims.put("dean_admin", PermissionManager.hasPermission(user.getAuthorities(), Role.DEAN_ADMIN));
        String token = FirebaseAuth.getInstance().createCustomTokenAsync(user.getUsername(), claims).get();
        return fromUser(user, token);
    } catch (InterruptedException | ExecutionException e) {
        return UNAUTHENTICATED;
    }
}
Also used : HashMap(java.util.HashMap) UserAuth(amu.zhcet.auth.UserAuth) ExecutionException(java.util.concurrent.ExecutionException) Transactional(javax.transaction.Transactional)

Example 3 with UserAuth

use of amu.zhcet.auth.UserAuth in project zhcet-web by zhcet-amu.

the class PermissionManager method checkDepartment.

public boolean checkDepartment(Authentication user, String departmentCode) {
    if (hasPermission(user.getAuthorities(), Role.DEPARTMENT_SUPER_ADMIN))
        return true;
    if (!(user.getPrincipal() instanceof UserAuth))
        return false;
    UserAuth userAuth = (UserAuth) user.getPrincipal();
    boolean isDepartmentAdmin = hasPermission(user.getAuthorities(), Role.DEPARTMENT_ADMIN);
    if (departmentCode == null) {
        return isDepartmentAdmin;
    } else {
        return isDepartmentAdmin && userAuth.getDepartment().getCode().equals(departmentCode);
    }
}
Also used : UserAuth(amu.zhcet.auth.UserAuth)

Aggregations

UserAuth (amu.zhcet.auth.UserAuth)3 ExecutionException (java.util.concurrent.ExecutionException)2 FirebaseToken (com.google.firebase.auth.FirebaseToken)1 HashMap (java.util.HashMap)1 Transactional (javax.transaction.Transactional)1 UserDetails (org.springframework.security.core.userdetails.UserDetails)1 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)1