src/org/ohmage/service/AuthenticationService.java
public boolean authenticate(
		final UserRequest request,
		final boolean newAccountsAllowed)
		throws ServiceException {
		
		// If the user is already logged in, then they are already 
		// authenticated.
		if(request.getUser().isLoggedIn()) {
			return true;
		}
		
		// Get the user's information should the username and password be
		// correct.
		UserInformation userInformation;
		try {
			userInformation = authenticationQuery.execute(request);
		}
		catch(DataAccessException e) {
			request.setFailed();
			throw new ServiceException(e);
		}
		
		// A new keycloak user has been found. insert them into the db.
		if(KeycloakCache.isEnabled() && 
				(userInformation == null) &&
				(request.getUser() instanceof KeycloakUser)){
			try {
				KeycloakServices.createUser((KeycloakUser) request.getUser());
			}
			catch (ServiceException e) {
				request.setFailed(
						ErrorCode.AUTHENTICATION_FAILED,
						"A user account with this name already exists.");
				throw new ServiceException(e);
			}

			// the user has now been created, re-try auth query to
			// "authenticate" the user. 
			try {
				userInformation = authenticationQuery.execute(request);
			}
			catch(DataAccessException e) {
				request.setFailed();
				throw new ServiceException(e);
			}
		// An existing keycloak user has been found. Test for personal info
		// that may need to be updated.
		} else if (KeycloakCache.isEnabled() &&
				(userInformation != null) &&
				(request.getUser() instanceof KeycloakUser)){
			KeycloakServices.updateUser((KeycloakUser) request.getUser());
		}
			
		
		// If the username and/or password were incorrect, then null was 
		// returned. Therefore, return false.
		if(userInformation == null) {
			return false;
		}
		
		// If the account is disabled, update the annotator and set the request
		// as failed.
		if(! userInformation.getEnabled()) {
			request
				.setFailed(
					ErrorCode.AUTHENTICATION_ACCOUNT_DISABLED,
					"The account is disabled.");
			return false;
		}
		// If the user is a new user and we aren't allowing new users for this
		// call, update the annotator and set the request as failed.
		else if((! newAccountsAllowed) && userInformation.getNewAccount()) {
			request
				.setFailed(
					ErrorCode.AUTHENTICATION_NEW_ACCOUNT,
					"New accounts aren't allowed to use this service.");
			return false;
		}
		// Otherwise, the user is valid and should be logged in.
		else {
			request.getUser().isLoggedIn(true);
			return true;
		}
	}
/**
	 * Checks if the user in the request is already logged in. If so, returns
	 * true. If not, it attempts to get their information. If no information
	 * exists about them, then false is returned. If such information does
	 * exist, that information is checked to ensure that they are allowed to
	 * login.
	 * 
	 * @param newAccountsAllowed Whether or not new accounts are allowed.
	 * 
	 * @return Returns true if the user successfully logged in or was already
	 * 		   logged in; false, otherwise.
	 * 
	 * @throws ServiceException Thrown if there is an error.
	 */
Checks if the user in the request is already logged in. If so, returns
true. If not, it attempts to get their information. If no information
exists about them, then false is returned. If such information does
exist, that information is checked to ensure that they are allowed to
login.

@param newAccountsAllowed Whether or not new accounts are allowed.

@return Returns true if the user successfully logged in or was already
logged in; false, otherwise.

@throws ServiceException Thrown if there is an error.
Checks if the user in the request is already logged in . If so returns true . If not it attempts to get their information . If no information exists about them then false is returned . If such information does exist that information is checked to ensure that they are allowed to login . @param newAccountsAllowed Whether or not new accounts are allowed . @return Returns true if the user successfully logged in or was already logged in ; false otherwise . @throws ServiceException Thrown if there is an error .
public boolean authenticate(
		final UserRequest request,
		final boolean newAccountsAllowed)
		throws ServiceException {
		if(request.getUser().isLoggedIn()) {
			return true;
		}
		UserInformation userInformation;
		try {
			userInformation = authenticationQuery.execute(request);
		}
		catch(DataAccessException e) {
			request.setFailed();
			throw new ServiceException(e);
		}
		if(KeycloakCache.isEnabled() && 
				(userInformation == null) &&
				(request.getUser() instanceof KeycloakUser)){
			try {
				KeycloakServices.createUser((KeycloakUser) request.getUser());
			}
			catch (ServiceException e) {
				request.setFailed(
						ErrorCode.AUTHENTICATION_FAILED,
						"A user account with this name already exists.");
				throw new ServiceException(e);
			}
			try {
				userInformation = authenticationQuery.execute(request);
			}
			catch(DataAccessException e) {
				request.setFailed();
				throw new ServiceException(e);
			}
		} else if (KeycloakCache.isEnabled() &&
				(userInformation != null) &&
				(request.getUser() instanceof KeycloakUser)){
			KeycloakServices.updateUser((KeycloakUser) request.getUser());
		}
		if(userInformation == null) {
			return false;
		}
		if(! userInformation.getEnabled()) {
			request
				.setFailed(
					ErrorCode.AUTHENTICATION_ACCOUNT_DISABLED,
					"The account is disabled.");
			return false;
		}
		else if((! newAccountsAllowed) && userInformation.getNewAccount()) {
			request
				.setFailed(
					ErrorCode.AUTHENTICATION_NEW_ACCOUNT,
					"New accounts aren't allowed to use this service.");
			return false;
		}
		else {
			request.getUser().isLoggedIn(true);
			return true;
		}
	}
public boolean authenticate ( final UserRequest request , final boolean newAccountsAllowed ) throws ServiceException { if ( request . getUser ( ) . isLoggedIn ( ) ) { return true ; } UserInformation userInformation ; try { userInformation = authenticationQuery . execute ( request ) ; } catch ( DataAccessException e ) { request . setFailed ( ) ; throw new ServiceException ( e ) ; } if ( KeycloakCache . isEnabled ( ) && ( userInformation == null ) && ( request . getUser ( ) instanceof KeycloakUser ) ) { try { KeycloakServices . createUser ( ( KeycloakUser ) request . getUser ( ) ) ; } catch ( ServiceException e ) { request . setFailed ( ErrorCode . AUTHENTICATION_FAILED , "A user account with this name already exists." ) ; throw new ServiceException ( e ) ; } try { userInformation = authenticationQuery . execute ( request ) ; } catch ( DataAccessException e ) { request . setFailed ( ) ; throw new ServiceException ( e ) ; } } else if ( KeycloakCache . isEnabled ( ) && ( userInformation != null ) && ( request . getUser ( ) instanceof KeycloakUser ) ) { KeycloakServices . updateUser ( ( KeycloakUser ) request . getUser ( ) ) ; } if ( userInformation == null ) { return false ; } if ( ! userInformation . getEnabled ( ) ) { request . setFailed ( ErrorCode . AUTHENTICATION_ACCOUNT_DISABLED , "The account is disabled." ) ; return false ; } else if ( ( ! newAccountsAllowed ) && userInformation . getNewAccount ( ) ) { request . setFailed ( ErrorCode . AUTHENTICATION_NEW_ACCOUNT , "New accounts aren't allowed to use this service." ) ; return false ; } else { request . getUser ( ) . isLoggedIn ( true ) ; return true ; } }
Checks if the user in the request is already logged in.
Checks if the user in the request is already logged in .
// If the user is already logged in, then they are already  // authenticated. // Get the user's information should the username and password be // correct. // A new keycloak user has been found. insert them into the db. // the user has now been created, re-try auth query to // "authenticate" the user.  // An existing keycloak user has been found. Test for personal info // that may need to be updated. // If the username and/or password were incorrect, then null was  // returned. Therefore, return false. // If the account is disabled, update the annotator and set the request // as failed. // If the user is a new user and we aren't allowing new users for this // call, update the annotator and set the request as failed. // Otherwise, the user is valid and should be logged in.