keybase-proofs
Version:
Publicly-verifiable proofs of identity
414 lines (373 loc) • 15.6 kB
JavaScript
// Generated by IcedCoffeeScript 108.0.11
(function() {
var BaseBearerToken, BaseScraper, Lock, b64find, callbackify, constants, decode_sig, iced, parse_int_or_undefined, pkg, sncmp, space_normalize, urlmod, user_agent, v_codes, __iced_k, __iced_k_noop;
iced = require('iced-runtime');
__iced_k = __iced_k_noop = function() {};
constants = require('../constants').constants;
v_codes = constants.v_codes;
pkg = require('../../package.json');
decode_sig = require('kbpgp').ukm.decode_sig;
space_normalize = require('../util').space_normalize;
b64find = require('../b64extract').b64find;
urlmod = require('url');
callbackify = require('util').callbackify;
Lock = require('../util').Lock;
exports.user_agent = user_agent = constants.user_agent + pkg.version;
BaseScraper = (function() {
function BaseScraper(_arg) {
var log_level;
this.libs = _arg.libs, log_level = _arg.log_level, this.proxy = _arg.proxy, this.ca = _arg.ca;
this.log_level = log_level || "debug";
}
BaseScraper.prototype.hunt = function(username, proof_check_text, cb) {
return hunt2({
username: username,
proof_check_text: proof_check_text
}, cb);
};
BaseScraper.prototype.hunt2 = function(args, cb) {
return cb(new Error("unimplemented"));
};
BaseScraper.prototype.id_to_url = function(username, status_id) {};
BaseScraper.prototype.check_status = function(_arg, cb) {
var signature, status_id, url, username;
username = _arg.username, url = _arg.url, signature = _arg.signature, status_id = _arg.status_id;
return cb(new Error("check_status not implemented"), v_codes.NOT_FOUND);
};
BaseScraper.prototype._check_args = function() {
return new Error("unimplemented");
};
BaseScraper.prototype._check_api_url = function() {
return false;
};
BaseScraper.prototype.get_tor_error = function(args) {
return [null, v_codes.OK];
};
BaseScraper.prototype.logl = function(level, msg) {
var k;
if ((k = this.libs.log) != null) {
return k[level](msg);
}
};
BaseScraper.prototype.log = function(msg) {
var k;
if (((k = this.libs.log) != null) && (this.log_level != null)) {
return k[this.log_level](msg);
}
};
BaseScraper.prototype.validate = function(args, cb) {
var err, rc, ___iced_passed_deferral, __iced_deferrals, __iced_k;
__iced_k = __iced_k_noop;
___iced_passed_deferral = iced.findDeferral(arguments);
err = null;
rc = null;
if ((err = this._check_args(args))) {
} else if (!this._check_api_url(args)) {
err = new Error("check url failed for " + (JSON.stringify(args)));
} else {
err = this._validate_text_check(args);
}
(function(_this) {
return (function(__iced_k) {
if (err == null) {
(function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/Users/michal/SourceCode/keybase/go/src/github.com/keybase/proofs/src/scrapers/base.iced",
funcname: "BaseScraper.validate"
});
_this.check_status(args, __iced_deferrals.defer({
assign_fn: (function() {
return function() {
err = arguments[0];
return rc = arguments[1];
};
})(),
lineno: 54
}));
__iced_deferrals._fulfill();
})(__iced_k);
} else {
return __iced_k();
}
});
})(this)((function(_this) {
return function() {
return cb(err, rc);
};
})(this));
};
BaseScraper.prototype._validate_text_check = function(_arg) {
var err, msg, proof_text_check, signature, _ref;
signature = _arg.signature, proof_text_check = _arg.proof_text_check;
_ref = decode_sig({
armored: signature
}), err = _ref[0], msg = _ref[1];
if ((err == null) && ("\n\n" + msg.payload + "\n") !== proof_text_check && msg.payload !== proof_text_check) {
err = new Error("Bad payload text_check");
}
return err;
};
BaseScraper.prototype._stripr = function(m) {
return m.split('\r').join('');
};
BaseScraper.prototype._find_sig_in_raw = function(proof_text_check, raw) {
var ptc_buf;
ptc_buf = Buffer.from(proof_text_check, "base64");
return b64find(raw, ptc_buf);
};
BaseScraper.prototype._get_rate_limit_header = function(headers, name) {
var v;
if ((v = parse_int_or_undefined(headers.get("X-RateLimit-" + name))) || (v = parse_int_or_undefined(headers.get("X-Rate-Limit-" + name)))) {
return v;
} else {
return void 0;
}
};
BaseScraper.prototype._get_url_body = function(opts, cb) {
var body, err, f, rc, response, rl_limit, rl_remaining, rl_reset, ___iced_passed_deferral, __iced_deferrals, __iced_k, _base;
__iced_k = __iced_k_noop;
___iced_passed_deferral = iced.findDeferral(arguments);
/*
cb(err, status, body) only replies with body if status is 200
*/
if (opts.timeout == null) {
opts.timeout = constants.http_timeout;
}
opts.headers || (opts.headers = {});
if ((_base = opts.headers)["User-Agent"] == null) {
_base["User-Agent"] = opts.user_agent || user_agent;
}
(function(_this) {
return (function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/Users/michal/SourceCode/keybase/go/src/github.com/keybase/proofs/src/scrapers/base.iced",
funcname: "BaseScraper._get_url_body"
});
_this.libs.fetch(opts.url, opts, __iced_deferrals.defer({
assign_fn: (function() {
return function() {
err = arguments[0];
return response = arguments[1];
};
})(),
lineno: 96
}));
__iced_deferrals._fulfill();
});
})(this)((function(_this) {
return function() {
var _base1, _ref;
if (opts.log_ratelimit && (typeof response !== "undefined" && response !== null)) {
rl_limit = _this._get_rate_limit_header(response.headers, 'Limit');
rl_remaining = _this._get_rate_limit_header(response.headers, 'Remaining');
rl_reset = _this._get_rate_limit_header(response.headers, 'Reset');
if (rl_limit != null) {
_this.log("| ratelimit info limit=" + rl_limit + " remaining=" + rl_remaining + " reset=" + rl_reset);
}
if (typeof (_base1 = _this.libs).ratelimit_inform === "function") {
_base1.ratelimit_inform({
limit: rl_limit,
remaining: rl_remaining,
reset: rl_reset,
endpoint_name: opts.endpoint_name
});
}
}
rc = typeof err !== "undefined" && err !== null ? err.message.includes('network timeout') ? v_codes.TIMEOUT : v_codes.HOST_UNREACHABLE : ((_ref = response.status) === 401 || _ref === 403) ? v_codes.PERMISSION_DENIED : response.status === 200 ? v_codes.OK : response.status >= 500 ? v_codes.HTTP_500 : response.status >= 400 ? v_codes.HTTP_400 : response.status >= 300 ? v_codes.HTTP_300 : v_codes.HTTP_OTHER;
if (rc === v_codes.OK) {
(function(__iced_k) {
try {
if (opts.json) {
f = callbackify(response.json);
(function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/Users/michal/SourceCode/keybase/go/src/github.com/keybase/proofs/src/scrapers/base.iced",
funcname: "BaseScraper._get_url_body"
});
f.call(response, __iced_deferrals.defer({
assign_fn: (function() {
return function() {
err = arguments[0];
return body = arguments[1];
};
})(),
lineno: 122
}));
__iced_deferrals._fulfill();
})(function() {
if (err) {
_this.log("| _get_url_body response.json() failed with: " + (err.toString()));
rc = v_codes.CONTENT_FAILURE;
}
return __iced_k(cb(err, rc, body));
});
} else {
f = callbackify(response.text);
(function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/Users/michal/SourceCode/keybase/go/src/github.com/keybase/proofs/src/scrapers/base.iced",
funcname: "BaseScraper._get_url_body"
});
f.call(response, __iced_deferrals.defer({
assign_fn: (function() {
return function() {
err = arguments[0];
return body = arguments[1];
};
})(),
lineno: 129
}));
__iced_deferrals._fulfill();
})(function() {
if (err) {
_this.log("| _get_url_body response.text() failed with: " + (err.toString()));
rc = v_codes.CONTENT_FAILURE;
}
return __iced_k(cb(err, rc, body));
});
}
} catch (_error) {
err = _error;
return cb(err, rc, null);
}
})(__iced_k);
} else {
return __iced_k(cb(err, rc, null));
}
};
})(this));
};
return BaseScraper;
})();
exports.BaseScraper = BaseScraper;
exports.BaseBearerToken = BaseBearerToken = (function() {
function BaseBearerToken(_arg) {
this.name = _arg.name, this.base = _arg.base, this.access_token_url = _arg.access_token_url, this.scope = _arg.scope, this.user_agent = _arg.user_agent;
if (this.access_token_url == null) {
throw new Error("@access_token_url is required");
}
if ((this.scope != null) && !Array.isArray(this.scope)) {
throw new Error("@scope has to be an array if present");
}
this._tok = null;
this._created = 0;
this._lock = new Lock();
this.auth = this.base.auth;
}
BaseBearerToken.prototype.get = function(cb) {
var body, cred, e, err, now, opts, rc, req, res, tok, ___iced_passed_deferral, __iced_deferrals, __iced_k;
__iced_k = __iced_k_noop;
___iced_passed_deferral = iced.findDeferral(arguments);
(function(_this) {
return (function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/Users/michal/SourceCode/keybase/go/src/github.com/keybase/proofs/src/scrapers/base.iced",
funcname: "BaseBearerToken.get"
});
_this._lock.acquire(__iced_deferrals.defer({
lineno: 164
}));
__iced_deferrals._fulfill();
});
})(this)((function(_this) {
return function() {
err = null;
now = Math.floor(Date.now() / 1000);
(function(__iced_k) {
if (((res = _this._tok) == null) || (now - _this._created > _this.auth.lifespan)) {
_this.base.log("+ Request for bearer token");
cred = (Buffer.from([_this.auth.key, _this.auth.secret].join(":"))).toString('base64');
req = 'grant_type=client_credentials';
if (_this.scope) {
req += "&scope=" + (encodeURIComponent(_this.scope.join(' ')));
}
opts = {
url: _this.access_token_url,
headers: {
Authorization: "Basic " + cred,
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
'Content-Length': req.length
},
method: "post",
body: req
};
if (_this.user_agent) {
opts.user_agent = _this.user_agent;
}
(function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/Users/michal/SourceCode/keybase/go/src/github.com/keybase/proofs/src/scrapers/base.iced",
funcname: "BaseBearerToken.get"
});
_this.base._get_url_body(opts, __iced_deferrals.defer({
assign_fn: (function() {
return function() {
err = arguments[0];
rc = arguments[1];
return body = arguments[2];
};
})(),
lineno: 189
}));
__iced_deferrals._fulfill();
})(function() {
if (err != null) {
_this.base.logl('error', "In getting bearer_token: " + err.message);
} else if (rc !== v_codes.OK) {
_this.base.logl('error', "error in getting bearer token: " + rc);
err = new Error("error: " + rc);
} else {
try {
body = JSON.parse(body);
} catch (_error) {
e = _error;
_this.base.logl('warn', "Could not parse JSON reply: " + e);
err = e;
}
}
if (err != null) {
} else if ((tok = body.access_token) == null) {
_this.base.logl('warn', "No access token found in reply");
err = new Error("" + _this.name + " error: no access token");
} else {
_this._tok = tok;
_this._created = Math.floor(Date.now() / 1000);
res = _this._tok;
}
_this.base.log("- Request for bearer token for " + _this.name + " -> err: " + err);
return __iced_k(!err ? _this.base.log("Bearer token for " + _this.name + " is: " + (res != null ? res.substr(0, 5) : void 0) + "...") : void 0);
});
} else {
return __iced_k();
}
})(function() {
_this._lock.release();
return cb(err, res);
});
};
})(this));
};
return BaseBearerToken;
})();
parse_int_or_undefined = function(value) {
var out;
if ((out = Number(value)) === parseInt(value) && !isNaN(out)) {
return out;
}
};
exports.sncmp = sncmp = function(a, b) {
if ((a == null) || (b == null)) {
return false;
} else {
a = ("" + a).toLowerCase();
b = ("" + b).toLowerCase();
return a === b;
}
};
}).call(this);