text: function HttpSignatureError(e,n){Error.captureStackTrace&&Error.captureStackTrace(this,n||HttpSignatureError),this.message=e,this.name=n.name}function ExpiredRequestError(e){HttpSignatureError.call(this,e,ExpiredRequestError)}function InvalidHeaderError(e){HttpSignatureError.call(this,e,InvalidHeaderError)}function InvalidParamsError(e){HttpSignatureError.call(this,e,InvalidParamsError)}function MissingHeaderError(e){HttpSignatureError.call(this,e,MissingHeaderError)}var assert=require("assert-plus"),util=require("util"),Algorithms={"rsa-sha1":!0,"rsa-sha256":!0,"rsa-sha512":!0,"dsa-sha1":!0,"hmac-sha1":!0,"hmac-sha256":!0,"hmac-sha512":!0},State={New:0,Params:1},ParamsState={Name:0,Quote:1,Value:2,Comma:3};util.inherits(HttpSignatureError,Error),util.inherits(ExpiredRequestError,HttpSignatureError),util.inherits(InvalidHeaderError,HttpSignatureError),util.inherits(InvalidParamsError,HttpSignatureError),util.inherits(MissingHeaderError,HttpSignatureError),module.exports={parseRequest:function(e,n){if(assert.object(e,"request"),assert.object(e.headers,"request.headers"),void 0===n&&(n={}),void 0===n.headers&&(n.headers=[e.headers["x-date"]?"x-date":"date"]),assert.object(n,"options"),assert.arrayOfString(n.headers,"options.headers"),assert.optionalNumber(n.clockSkew,"options.clockSkew"),!e.headers.authorization)throw new MissingHeaderError("no authorization header present in the request");n.clockSkew=n.clockSkew||300;var t=0,r=State.New,a=ParamsState.Name,i="",s="",o={scheme:"",params:{},signingString:"",get algorithm(){return this.params.algorithm.toUpperCase()},get keyId(){return this.params.keyId}},l=e.headers.authorization;for(t=0;t<l.length;t++){var u=l.charAt(t);switch(Number(r)){case State.New:" "!==u?o.scheme+=u:r=State.Params;break;case State.Params:switch(Number(a)){case ParamsState.Name:var c=u.charCodeAt(0);if(c>=65&&90>=c||c>=97&&122>=c)i+=u;else{if("="!==u)throw new InvalidHeaderError("bad param format");if(0===i.length)throw new InvalidHeaderError("bad param format");a=ParamsState.Quote}break;case ParamsState.Quote:if('"'!==u)throw new InvalidHeaderError("bad param format");s="",a=ParamsState.Value;break;case ParamsState.Value:'"'===u?(o.params[i]=s,a=ParamsState.Comma):s+=u;break;case ParamsState.Comma:if(","!==u)throw new InvalidHeaderError("bad param format");i="",a=ParamsState.Name;break;default:throw new Error("Invalid substate")}break;default:throw new Error("Invalid substate")}}if(o.params.headers&&""!==o.params.headers?o.params.headers=o.params.headers.split(" "):e.headers["x-date"]?o.params.headers=["x-date"]:o.params.headers=["date"],!o.scheme||"Signature"!==o.scheme)throw new InvalidHeaderError('scheme was not "Signature"');if(!o.params.keyId)throw new InvalidHeaderError("keyId was not specified");if(!o.params.algorithm)throw new InvalidHeaderError("algorithm was not specified");if(!o.params.signature)throw new InvalidHeaderError("signature was not specified");if(o.params.algorithm=o.params.algorithm.toLowerCase(),!Algorithms[o.params.algorithm])throw new InvalidParamsError(o.params.algorithm+" is not supported");for(t=0;t<o.params.headers.length;t++){var f=o.params.headers[t].toLowerCase();if(o.params.headers[t]=f,"request-line"!==f){var d=e.headers[f];if(!d)throw new MissingHeaderError(f+" was not in the request");o.signingString+=f+": "+d}else o.signingString+=e.method+" "+e.url+" HTTP/"+e.httpVersion;t+1<o.params.headers.length&&(o.signingString+="\n")}var h;if(e.headers.date||e.headers["x-date"]){h=e.headers["x-date"]?new Date(e.headers["x-date"]):new Date(e.headers.date);var p=new Date,m=Math.abs(p.getTime()-h.getTime());if(m>1e3*n.clockSkew)throw new ExpiredRequestError("clock skew of "+m/1e3+"s was greater than "+n.clockSkew+"s")}if(n.headers.forEach(function(e){if(o.params.headers.indexOf(e)<0)throw new MissingHeaderError(e+" was not a signed header")}),n.algorithms&&-1===n.algorithms.indexOf(o.params.algorithm))throw new InvalidParamsError(o.params.algorithm+" is not a supported algorithm");return o}};