All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.untzuntz.ustackserverapi.auth.UserNamePasswordAuth Maven / Gradle / Ivy

There is a newer version: 2.1.115
Show newest version
package com.untzuntz.ustackserverapi.auth;

import java.util.ArrayList;
import java.util.List;

import org.jboss.netty.handler.codec.http.HttpRequest;

import com.untzuntz.ustack.aaa.Authentication;
import com.untzuntz.ustack.aaa.Authorization;
import com.untzuntz.ustack.data.UserAccount;
import com.untzuntz.ustack.exceptions.AuthenticationException;
import com.untzuntz.ustack.exceptions.AuthorizationException;
import com.untzuntz.ustackserverapi.APIException;
import com.untzuntz.ustackserverapi.CallParameters;
import com.untzuntz.ustackserverapi.MethodDefinition;
import com.untzuntz.ustackserverapi.params.ParamNames;
import com.untzuntz.ustackserverapi.params.types.ParameterDefinitionInt;

/**
 * Authentication method for username/password combo in HTTP request params
 * 
 * @author jdanner
 *
 */
public class UserNamePasswordAuth implements AuthenticationInt {

    public String getAuthenticationDescription() {
    	return "Username and password combo (in request parameters) required.";
    }

    public boolean isBasicAuth() {
    	return false;
    }

    public List> getAuthenticationParameters() {
    	
    	List> ret = new ArrayList>();
    	
    	ret.add(ParamNames.username);
    	ret.add(ParamNames.password);
    	
    	return ret;
    }

	@Override
	public UserAccount authenticate(MethodDefinition method, HttpRequest req, CallParameters params) throws APIException {
		
		UserAccount user = null;
		try {
			user = Authentication.authenticateUser(params.get(ParamNames.username), params.get(ParamNames.password));
		} catch (AuthenticationException e) {
			throw new APIAuthenticationException(e.getMessage());
		}
		
		if (method.getAuthenticationGroup() != null)
		{
			try {
				Authorization.authorizeUser(user, "*", null, method.getAuthenticationGroup());
			} catch (AuthorizationException e) {
				throw new APIAuthorizationException("Not Authorized");
			}
		}
		
		return user;
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy