├── .github └── workflows │ └── run-conventional-commits-check.yml ├── .gitignore ├── package.json ├── README.md ├── LICENSE ├── test.js └── index.js /.github/workflows/run-conventional-commits-check.yml: -------------------------------------------------------------------------------- 1 | name: ☢️ Conventional Commits Check 2 | 3 | on: 4 | pull_request: 5 | types: [opened, edited] 6 | 7 | jobs: 8 | run-conventional-commits-check: 9 | uses: artsy/duchamp/.github/workflows/conventional-commits-check.yml@main 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # node-waf configuration 20 | .lock-wscript 21 | 22 | # Compiled binary addons (http://nodejs.org/api/addons.html) 23 | build/Release 24 | 25 | # Dependency directory 26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 27 | node_modules 28 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@artsy/xapp", 3 | "version": "2.0.0", 4 | "description": "Tiny lib to fetch and refresh an xapp token from Artsy's API.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "NODE_ENV=staging mocha -r should" 8 | }, 9 | "engines": { 10 | "node": ">=18.0.0" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/artsy/artsy-xapp.git" 15 | }, 16 | "keywords": [ 17 | "artsy", 18 | "xapp" 19 | ], 20 | "author": "Craig Spaeth", 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/artsy/artsy-xapp/issues" 24 | }, 25 | "homepage": "https://github.com/artsy/artsy-xapp", 26 | "devDependencies": { 27 | "express": "^4.12.3", 28 | "mocha": "^2.2.4", 29 | "should": "^6.0.1" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # artsy-xapp 2 | Tiny lib to fetch, store, and refresh an xapp token from Artsy's API. 3 | 4 | ## Example 5 | 6 | ````javascript 7 | var artsyXapp = require('artsy-xapp'); 8 | artsyXapp.init({ 9 | url: 'https://api.artsy.net', // defaults to process.env.ARTSY_URL 10 | id: '31f31ffds', // defaults to process.env.ARTSY_ID 11 | secret: '32rf1fds' // defaults to process.env.ARTSY_SECRET 12 | }, function() { 13 | app.locals.xappToken = artsyXapp.token 14 | }); 15 | artsyXapp.on('error', process.exit); 16 | ```` 17 | 18 | ## How it works 19 | 20 | ArtsyXapp will fetch an xapp token on `init`, store the token in `artsyXapp.token` and refresh the token in the background. If everything goes to hell (e.g. the token is expiring and Artsy's API is down) it will emit an error and null the token—you probably want to crash the server at this point if your app depends deeply on Artsy's API. 21 | 22 | ## License 23 | 24 | MIT -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Artsy 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var artsyXapp = require('./'), 2 | express = require('express'); 3 | 4 | var app = express(); 5 | var requested = 0; 6 | var lastReq; 7 | var timeOffset = 0; 8 | app.get('/api/v1/xapp_token', function(req, res, next) { 9 | requested++ 10 | res.send({ 11 | "xapp_token": "foo-token" + requested, 12 | "expires_in": new Date(Date.now() + timeOffset).toISOString(), 13 | }); 14 | lastReq = req; 15 | }); 16 | 17 | 18 | describe('artsyXapp', function() { 19 | 20 | var server, token, xappRes; 21 | 22 | before(function(done) { 23 | server = app.listen(7000, done); 24 | }); 25 | 26 | beforeEach(function () { 27 | requested = 0; 28 | timeOffset = 2000; 29 | }) 30 | 31 | it('gets an access token on init', function(done) { 32 | artsyXapp.init({ 33 | url: 'http://localhost:7000', 34 | id: 'foo', 35 | secret: 'bar' 36 | }, function() { 37 | artsyXapp.token.should.equal('foo-token1'); 38 | done(); 39 | }); 40 | }); 41 | 42 | it('refreshes the access token before it expires', function(done) { 43 | timeOffset = 1250 44 | var refreshes = 1; 45 | artsyXapp.init({ 46 | url: 'http://localhost:7000', 47 | id: 'foo', 48 | secret: 'bar' 49 | }, function() { 50 | artsyXapp.token.should.equal('foo-token' + refreshes++); 51 | setTimeout(function() { 52 | artsyXapp.token.should.not.equal('foo-token1'); 53 | artsyXapp.token.should.containEql('foo-token2'); 54 | done(); 55 | }, 275); 56 | }); 57 | }); 58 | 59 | it('sends client id and secret', function(done) { 60 | artsyXapp.init({ 61 | url: 'http://localhost:7000', 62 | id: 'foo', 63 | secret: 'bar' 64 | }, function() { 65 | lastReq.query.client_id.should.equal('foo'); 66 | lastReq.query.client_secret.should.equal('bar'); 67 | done(); 68 | }); 69 | }); 70 | 71 | xit('emits an error if after failed to get an access token', function() { 72 | }); 73 | }); 74 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var Emitter = require("events").EventEmitter; 2 | var MAX_NODE_TIMEOUT = 2147483647; 3 | var MIN_NODE_TIMEOUT = 1; 4 | var timeoutHandles = []; 5 | 6 | module.exports = new Emitter(); 7 | 8 | module.exports.init = function (options, callback) { 9 | // Do a solid and just callback for test env 10 | if (process.env.NODE_ENV == "test") return callback(); 11 | 12 | // Make sure there aren't any lingering timeouts. 13 | clearTimeouts(); 14 | 15 | // Setup defaults 16 | if (typeof options == "function") { 17 | callback = options; 18 | var options = {}; 19 | } 20 | if (!options.url) options.url = process.env.ARTSY_URL; 21 | if (!options.id) options.id = process.env.ARTSY_ID; 22 | if (!options.secret) options.secret = process.env.ARTSY_SECRET; 23 | 24 | // Fetch the xapp token, cache, and refresh 25 | fetchAndCacheToken(options, callback); 26 | }; 27 | 28 | function clearTimeouts() { 29 | for (var timeoutHandle of timeoutHandles) { 30 | clearTimeout(timeoutHandle); 31 | } 32 | timeoutHandles = []; 33 | } 34 | 35 | var fetchAndCacheToken = function (options, callback) { 36 | // Get the token 37 | fetch( 38 | options.url + 39 | `/api/v1/xapp_token?client_id=${options.id}&client_secret=${options.secret}` 40 | ) 41 | .then((data) => data.json()) 42 | .then((res) => { 43 | // Set and callback w/ token (useful for client middleware). 44 | module.exports.token = res.xapp_token; 45 | if (callback) callback(null, module.exports.token); 46 | 47 | // Recurse this function to refresh the token it before it expires 48 | var expiresAt = new Date(res.expires_in).getTime(); 49 | var timeout = expiresAt - 1000 - Date.now(); 50 | var timeoutHandle = setTimeout( 51 | function () { 52 | var index = timeoutHandles.indexOf(timeoutHandle); 53 | timeoutHandles.splice(index, 1); 54 | fetchAndCacheToken(options, callback); 55 | }, 56 | timeout > MAX_NODE_TIMEOUT 57 | ? MAX_NODE_TIMEOUT 58 | : timeout < MIN_NODE_TIMEOUT 59 | ? MIN_NODE_TIMEOUT 60 | : timeout 61 | ); 62 | timeoutHandles.push(timeoutHandle); 63 | }) 64 | .catch((err) => { 65 | module.exports.emit("error", err); 66 | if (callback) callback(err); 67 | }); 68 | }; 69 | --------------------------------------------------------------------------------