UNPKG

anvil-connect-sdk

Version:
124 lines (94 loc) 2.83 kB
/** * Server Dependencies */ var fs = require('fs') , express = require('express') , anvil = require('anvil-connect-nodejs') , server = express() ; /** * Authorization Server Public Key */ var publicKey = '/Users/smith/GitHub/christiansmith/anvil-connect/' + 'test/lib/keys/public.pem' ; /** * Client Access Token (JWT) */ var clientToken = 'eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJodHRwczovL2FjY291bnRz' + 'LmFudmlsLmlvIiwic3ViIjoiNzhjMzE3MzktYmFmMC00OGQyLTkxND' + 'ktMzg5Y2RiZWU3MWNkIiwiYXVkIjoiNzhjMzE3MzktYmFmMC00OGQyL' + 'TkxNDktMzg5Y2RiZWU3MWNkIiwiaWF0IjoxNDAwMDk0NTA0NzYwLCJ' + 'zY29wZSI6ImNsaWVudCJ9.dEp3bHdDY2hKRnJvRnhHNHVYMWVDREQz' + 'YXVvajhVS2JBYWVjeV9wa2R2eFpuVmxuVlFCdUR6cHpNYUVnU3o5Mz' + 'ZXQnJqeFJVZ3FUWGg2WExjUzB2dTgxeUJ3ekJzdTRMbEZxX0hLVHFh' + 'NU84NGhVdDdPWnFWQVkyX240VDRXTTUzREQ2bUtpMXZHcUY0ejhGVE' + 'xad1pBTWF3azBKY2h4b0xHNXk0NW1ZYm54bmotanJ6R1hWSW1MNFNN' + 'N2txNTNmRlFpa2RxaDRqdFQ4RVFpLWU5NnF4RUg2XzhWcGR0SXVnRm' + 'gyZnVGRnh6X3ZZa3c5cXc0QXdrWWsxeE1VaEtwN1VLWkJReEJnT1lLZ' + 'kxTUTlxWDgzS1pRazZibC1LY3F4M2k5QjIwNVN3YmZzTjE3b3RvdEd' + 'Oem5VM3AxSmJ1b0t2N1VkNWV2UW4yY09nbW9WSEs2SEFB' ; /** * Configure Anvil Connect Client */ anvil.configure({ provider: { uri: 'http://localhost:3000', key: fs.readFileSync(publicKey).toString('ascii') }, client: { id: '78c31739-baf0-48d2-9149-389cdbee71cd', token: clientToken }, params: { responseType: 'code', redirectUri: 'http://127.0.0.1:3001/callback', scope: 'openid profile' } }); /** * View with signin link */ server.get('/', function (req, res) { res.send('<a href="' + anvil.uri() + '">Signin</a>'); }); /** * Handle Anvil Connect Auth Flow Response */ server.get('/callback', function (req, res, next) { anvil.callback(req.url, function (err, authorization) { if (err) { return next(err); } anvil.userInfo(authorization.tokens.access_token, function (err, userInfo) { if (err) { return next(err); } res.json({ tokens: authorization.tokens, identity: authorization.identity, userInfo: userInfo }); }); }); }); /** * Protected Resource Requires Access Token */ server.get('/protected', // generate middleware that requires "profile" scope anvil.verify({ scope: 'profile' }), // if authenticated, handle the request function (req, res, next) { res.send("I'm protected ;-)"); }); /** * Error Handler */ server.use(function (err, req, res, next) { console.log('ERROR', err) res.json(err); }) /** * Start the server */ server.listen(3001);