"use strict";

/**
 * @param {String} strJWTCookieName
 */
function JWT(strJWTCookieName)
{
	JWT.strJWTCookieName=strJWTCookieName;
}




JWT.prototype={}


JWT.decode=function(strJWTCookie)
{
	if(!strJWTCookie)
	{
		return null;
	}

	var arrSegments=strJWTCookie.split(".");
	if(arrSegments.length!=JWT.nNumberOfSegments)
	{
		throw new Error("Wrong number of segments.");
	}

	var strHeader64=arrSegments.shift();
	var strBody64=arrSegments.shift();
	var strSignature=arrSegments.shift();
	var objHeader, objPayload;

	try
	{
		objHeader=JSON.parse(JWT.urlsafeB64Decode(strHeader64));
	}
	catch(error)
	{
		console.log(error.message);
		throw new Error("Failed to parse JSON: "+strHeader64+".");
	}


	if(objHeader==null)
	{
		throw new Error("Invalid header encoding.");
	}
	if(objHeader.typ!=JWT.HEADER_TOKEN_TYPE)
	{
		throw new Error("Invalid type of token.");
	}
	try
	{
		objPayload=JSON.parse(JWT.urlsafeB64Decode(strBody64));
	}
	catch(error)
	{
		console.log(error);
		throw new Error("Failed to parse JSON: "+strBody64+".");
	}

	return objPayload.sub;
}

JWT.urlsafeB64Decode=function(strInput)
{
	/*var remainder=strInput.length % 4;
	if(remainder)
	{
		var padlen=4-remainder;
		strInput+=Array(padlen).join("=")
	}*/

	//var parsedWordArray = CryptoJS.enc.Base64.parse(strInput);
	//var decoded = parsedWordArray.toString(CryptoJS.enc.Utf8);
	var decoded = atob(strInput).replace(/=/g, "");
	return decoded.replace("\u0000", "").trim();
}


/*
JWT.verifyMandatoryPayloadSubjectKeys=function(objPayload)
{
	var arrSubjectKeys=Object.keys(objPayload.sub);
	for(var strMandatoryKey in JWT._arrMandatoryPayloadSubjectKeys)
	{
		if (arrSubjectKeys.indexOf(strMandatoryKey)==-1)
		{
			throw new Error("The mandatory key "+strMandatoryKey+" is missing.");
		}
	}
	return true;
}


JWT.verifyAlgorithm=function(strAlgorithm)
{
	if(
		strAlgorithm===undefined
		|| strAlgorithm==null
		JWT._arrAlgorithms.indexOf(strAlgorithm)==-1
	)
	{
		throw new Error("Invalid signing algorithm given ".strAlgorithm.".");
	}
	return true;
}


JWT.setHeader=function(strAlgorithm)
{
	JWT.verifyAlgorithm(strAlgorithm);
	return {
		typ: JWT.HEADER_TOKEN_TYPE,
		alg: strAlgorithm
	};
}


JWT.setMandatoryPayloadSubjectKeys=function(arrPayloadSubjectKeys)
{
	if(!(arrPayloadSubjectKeys instanceof Array)
	{
		throw new Error("The mandatory payload subject keys must be an array.");
	}

	JWT._arrMandatoryPayloadSubjectKeys=arrPayloadSubjectKeys;
}



JWT.getMandatoryPayloadSubjectKeys=function()
{
	return JWT._arrMandatoryPayloadSubjectKeys;
}

*/
/**
*
*/
JWT._arrAlgorithms=[JWT.SIGN_HS256, JWT.SIGN_HS256];

/**
*
*/
JWT._arrMandatoryPayloadSubjectKeys=[];





/**
*
*/
JWT.SIGN_HS256="HS256";


/**
*
*/
JWT.SIGN_RS245="RS245";


/**
*
*/
JWT.EXPIRATION_TIME_CLAIM_KEY="exp";


/**
*
*/
JWT.SUBJECT_CLAIM_KEY="sub";


/**
*
*/
JWT.ISSUER_CLAIM_KEY="iss";


/**
*
*/
JWT.APP_VERSION_KEY="key";


/**
*
*/
JWT.IP_HASH_KEY="ip_hash";


/**
*
*/
JWT.nNumberOfSegments=3;


/**
*
*/
JWT.HEADER_TOKEN_TYPE="JWT";