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

com.textrecruit.ustackserverapi.auth.GuestAccessTokenAuth Maven / Gradle / Ivy

There is a newer version: 1.0.31
Show newest version
package com.textrecruit.ustackserverapi.auth;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.textrecruit.ustackserverapi.params.ParamNames;
import com.textrecruit.ustackserverapi.params.types.ParameterDefinitionInt;
import org.apache.log4j.Logger;
import org.jboss.netty.handler.codec.http.HttpRequest;

import com.textrecruit.ustack.data.AccessToken;
import com.textrecruit.ustack.data.AccessToken.AccessTokenDetails;
import com.textrecruit.ustackserverapi.APIException;
import com.textrecruit.ustackserverapi.CallParameters;
import com.textrecruit.ustackserverapi.MethodDefinition;

public class GuestAccessTokenAuth implements AuthenticationInt {

    static Logger           		logger               	= Logger.getLogger(GuestAccessTokenAuth.class);
 
    public String getAuthenticationDescription() {
    	return "Access token in request parameters required.";
    }

    public boolean isBasicAuth() {
    	return false;
    }

    /**
     * 
     */
    public List> getAuthenticationParameters() {
    	
    	List> ret = new ArrayList>();
    	
    	ret.add(ParamNames.token);
    	
    	return ret;
    }
    
	@Override
	public String authenticate(MethodDefinition method, HttpRequest req, CallParameters params) throws APIException {

		//AuthTypes.ClientKey.authenticate(method, req, params);
		AccessTokenDetails details = AccessToken.decode( params.get(ParamNames.token) );
		
		if (details == null)
		{
			logger.warn(String.format("Invalid Token => '%s' (%s)", params.get(ParamNames.token), method.getPath()));
			throw new APIAuthenticationException("Invalid Token");
		}

		LogoutUtil.checkTokenLogout(details);

		long tokenLife = details.getExpirationAge() - System.currentTimeMillis();
		if (tokenLife < 0)
		{
			logger.info(String.format("%s/%s | Access Token expired at '%s'", details.getUserName(), details.getClientId(), new SimpleDateFormat("MM/dd/yyyy h:mm:ss a").format(new Date(details.getExpirationAge()))));
			throw new APIAuthenticationException("Token has expired");
		}

//		logger.info(String.format("%s -> Expiration %d -> Now: %d", details.userName, details.expirationAge, System.currentTimeMillis()));
		
		params.setParameterValue(ParamNames.client_id.getName(), details.getClientId());
		params.setTokenTTL(tokenLife);
		
		return details.getUserName();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy