electron-authentication-hatena
Version:
Hatena OAuth Window library.
120 lines • 5.55 kB
JavaScript
// LICENSE : MIT
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var assert_1 = __importDefault(require("assert"));
var electron_1 = require("electron");
var oauth_1 = require("oauth");
var hatenaOauth_1 = require("./hatenaOauth");
// ref: https://github.com/kymmt90/blog/blob/76fe9265df6f55b13d6ecd2d33168464926259bd/hatena_oauth.md
// http://qiita.com/n0bisuke/items/c12963e0bde614443adf
var OAuthProvider = {
requestTokenURL: "https://www.hatena.com/oauth/initiate",
accessTokenURL: "https://www.hatena.com/oauth/token",
authorizeURL: "https://www.hatena.ne.jp/oauth/authorize?oauth_token="
};
var AuthenticationHatena = /** @class */ (function () {
function AuthenticationHatena(_a) {
var key = _a.key, secret = _a.secret, _b = _a.scopes, scopes = _b === void 0 ? [] : _b, _c = _a.provider, provider = _c === void 0 ? OAuthProvider : _c, _d = _a.redirectURL, redirectURL = _d === void 0 ? "https://example.com/auth/callback" : _d;
assert_1.default(key, "OAuth Consumer Key is needed!");
assert_1.default(secret, "OAuth Consumer Secret is needed!");
var scopeQuery = scopes.length > 0 ? "?scope=" + hatenaOauth_1.buildScope(scopes) : "";
this.consumerKey = key;
this.consumerSecret = secret;
this.redirectURL = redirectURL;
this.OAuthProvider = {
requestTokenURL: "" + provider.requestTokenURL + scopeQuery,
accessTokenURL: provider.accessTokenURL,
authorizeURL: provider.authorizeURL
};
}
AuthenticationHatena.prototype.startRequest = function () {
var _this = this;
var oauth = new oauth_1.OAuth(this.OAuthProvider.requestTokenURL, this.OAuthProvider.accessTokenURL, this.consumerKey, this.consumerSecret, "1.0", this.redirectURL, "HMAC-SHA1");
var deferredPromise = new Promise(function (resolve, reject) {
var isResolved = false;
_this.resolve = function (value) {
if (isResolved) {
return;
}
isResolved = true;
resolve(value);
};
_this.reject = function (error) {
if (isResolved) {
return;
}
isResolved = true;
reject(error);
};
});
oauth.getOAuthRequestToken(function (error, oauthToken, oauthTokenSecret) {
if (error) {
return _this.reject(error);
}
var oauthRequestToken = oauthToken;
var oauthRequestTokenSecret = oauthTokenSecret;
var authorizeURL = _this.OAuthProvider.authorizeURL + oauthRequestToken;
_this.getAccessToken(oauth, oauthRequestToken, oauthRequestTokenSecret, authorizeURL);
});
return deferredPromise;
};
// ref. https://github.com/r7kamura/retro-twitter-client/blob/master/src/browser/authentication-window.js
// http://qiita.com/Quramy/items/fc79cad92bb287478076
AuthenticationHatena.prototype.getAccessToken = function (oauth, requestToken, requestTokenSecret, authorizeURL) {
var _this = this;
this.window = new electron_1.BrowserWindow({ width: 800, height: 600 });
this.window.on("close", function () {
_this.reject(new Error("the window is closed before complete the authentication."));
});
var onLoadURL = function (url, preventDefault) {
var matched;
// + pass decoded url as verifier
var decodedURL = decodeURIComponent(url);
if (matched = decodedURL.match(/\?oauth_token=([^&]*)&oauth_verifier=([^&]*)/)) {
// @ts-ignore
var _all = matched[0], _oauthToken = matched[1], oauthVerifier = matched[2];
oauth.getOAuthAccessToken(requestToken, requestTokenSecret, oauthVerifier, function (error, accessToken, accessTokenSecret) {
if (error) {
_this.reject(error);
setImmediate(function () {
_this.window.close();
});
return;
}
_this.resolve({
accessToken: accessToken,
accessTokenSecret: accessTokenSecret
});
setImmediate(function () {
_this.window.close();
});
});
if (preventDefault) {
preventDefault();
}
}
};
var filter = {
urls: [this.redirectURL + '*']
};
if (!electron_1.session.defaultSession) {
throw new Error("Not found default session on Electron Window");
}
electron_1.session.defaultSession.webRequest.onCompleted(filter, function (details) {
var url = details.url;
onLoadURL(url);
});
this.window.webContents.on('will-navigate', function (event, url) {
onLoadURL(url, function () {
event.preventDefault();
});
});
this.window.loadURL(authorizeURL);
};
return AuthenticationHatena;
}());
exports.AuthenticationHatena = AuthenticationHatena;
//# sourceMappingURL=AuthenticationHatena.js.map