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;
}
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;
}
}
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);
}
}
Aggregations