├── .gitignore ├── .npmignore ├── History.md ├── Makefile ├── Readme.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | support 2 | test 3 | examples 4 | *.sock 5 | -------------------------------------------------------------------------------- /History.md: -------------------------------------------------------------------------------- 1 | 2 | 1.0.4 / 2018-06-21 3 | ================== 4 | 5 | * clear interval when user manually closes oauth window 6 | 7 | 1.0.3 / 2016-02-20 8 | ================== 9 | 10 | * Clear interval after polling 11 | 12 | 1.0.2 / 2015-07-24 13 | ================== 14 | 15 | * fix package 16 | 17 | 1.0.1 / 2015-07-24 18 | ================== 19 | 20 | * oauth-popup => oauth-open 21 | 22 | 1.0.0 / 2010-01-03 23 | ================== 24 | 25 | * Initial release 26 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | test: 3 | @./node_modules/.bin/mocha \ 4 | --require should \ 5 | --reporter spec 6 | 7 | .PHONY: test -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | 2 | # oauth-open 3 | 4 | minimal popup for authenticating with oauth 5 | 6 | ## Installation 7 | 8 | ``` 9 | npm install oauth-open 10 | ``` 11 | 12 | ## Usage 13 | 14 | ```js 15 | var open = require('oauth-open'); 16 | 17 | open('https://www.facebook.com/v2.3/dialog/oauth?...', function(err, code) { 18 | if (err) throw err; 19 | console.log(code) 20 | }); 21 | ``` 22 | 23 | ## API 24 | 25 | ### `open(url, options, fn)` 26 | 27 | Open a popup and go to the following `url`, calling `fn` when the flow is complete. 28 | 29 | Available `options` include: 30 | 31 | - name: name of the window 32 | - width: width of the popup 33 | - height: height of the popup 34 | - left: position of the popup 35 | - top: position of the popup 36 | 37 | ## Credits 38 | 39 | Extracted from the [satellizer](https://github.com/sahat/satellizer) project. 40 | 41 | ## License 42 | 43 | (The MIT License) 44 | 45 | Copyright (c) 2015 Matthew Mueller <mattmuelle@gmail.com> 46 | 47 | Permission is hereby granted, free of charge, to any person obtaining 48 | a copy of this software and associated documentation files (the 49 | 'Software'), to deal in the Software without restriction, including 50 | without limitation the rights to use, copy, modify, merge, publish, 51 | distribute, sublicense, and/or sell copies of the Software, and to 52 | permit persons to whom the Software is furnished to do so, subject to 53 | the following conditions: 54 | 55 | The above copyright notice and this permission notice shall be 56 | included in all copies or substantial portions of the Software. 57 | 58 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 59 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 60 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 61 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 62 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 63 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 64 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 65 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Module dependencies 3 | */ 4 | 5 | var querystring = require('querystring'); 6 | var assign = require('object-assign'); 7 | var once = require('once'); 8 | 9 | /** 10 | * Export `open` 11 | */ 12 | 13 | module.exports = open; 14 | 15 | /** 16 | * Initialize `open` 17 | */ 18 | 19 | function open(url, options, fn) { 20 | options = options || {}; 21 | 22 | if (2 == arguments.length) { 23 | fn = options; 24 | options = {}; 25 | } 26 | 27 | var str = stringify(configure(options)); 28 | var popup = window.open(url, options.name || '', str); 29 | popup.focus(); 30 | poll(popup, fn); 31 | return popup; 32 | } 33 | 34 | /** 35 | * Poll 36 | */ 37 | 38 | function poll(popup, fn) { 39 | var done = once(fn); 40 | 41 | var intervalId = setInterval(function polling() { 42 | if (popup.closed) { 43 | clearInterval(intervalId); 44 | return; 45 | } 46 | try { 47 | var documentOrigin = document.location.host; 48 | var popupWindowOrigin = popup.location.host; 49 | } catch (e) {}; 50 | 51 | if (popupWindowOrigin === documentOrigin && (popup.location.search || popup.location.hash)) { 52 | var queryParams = popup.location.search.substring(1).replace(/\/$/, ''); 53 | var hashParams = popup.location.hash.substring(1).replace(/[\/$]/, ''); 54 | var hash = querystring.parse(hashParams); 55 | var qs = querystring.parse(queryParams); 56 | 57 | qs = assign(qs, hash); 58 | 59 | if (qs.error) { 60 | clearInterval(intervalId); 61 | popup.close(); 62 | done(new Error(qs.error)); 63 | } else { 64 | clearInterval(intervalId); 65 | popup.close(); 66 | done(null, qs); 67 | } 68 | } 69 | }, 35); 70 | } 71 | 72 | /** 73 | * Configure the popup 74 | */ 75 | 76 | function configure(options) { 77 | var width = options.width || 500; 78 | var height = options.height || 500; 79 | return assign({ 80 | width: width, 81 | height: height, 82 | left: window.screenX + ((window.outerWidth - width) / 2), 83 | top: window.screenY + ((window.outerHeight - height) / 2.5) 84 | }, options || {}); 85 | } 86 | 87 | /** 88 | * Stringify 89 | */ 90 | 91 | function stringify(obj) { 92 | var parts = []; 93 | for (var key in obj) { 94 | parts.push(key + '=' + obj[key]); 95 | } 96 | return parts.join(','); 97 | } 98 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "oauth-open", 3 | "version": "1.0.4", 4 | "description": "minimal popup for authenticating through oauth", 5 | "keywords": [ 6 | "oauth", 7 | "popup" 8 | ], 9 | "author": "Matthew Mueller ", 10 | "repository": { 11 | "type": "git", 12 | "url": "git://github.com/LapwingLabs/oauth-open.git" 13 | }, 14 | "dependencies": { 15 | "object-assign": "^3.0.0", 16 | "once": "^1.3.2", 17 | "querystring": "^0.2.0" 18 | }, 19 | "devDependencies": { 20 | "mocha": "*", 21 | "should": "*" 22 | }, 23 | "main": "index" 24 | } --------------------------------------------------------------------------------