UNPKG

anvil-connect-sdk

Version:
158 lines (124 loc) 3.72 kB
/** * Server Dependencies */ var fs = require('fs') , redis = require('redis') , client = redis.createClient() , express = require('express') , server = express() , passport = require('passport') , cookieParser = require('cookie-parser') , bodyParser = require('body-parser') , session = require('express-session') , RedisStore = require('connect-redis')(session) , sessionStore = new RedisStore({ client: client }) ; /** * Server configuration */ server.use(cookieParser('secret')); server.use(bodyParser()); server.use(session({ store: sessionStore, secret: 'othersecret' })); server.use(passport.initialize()); server.use(passport.session()); passport.serializeUser(function (accessToken, done) { done(null, accessToken); }); passport.deserializeUser(function (accessToken, done) { anvil.userInfo(accessToken, function (err, info) { done(err, info); }); }); passport.use(new OpenIDConnectStrategy({ authorizationURL: '', tokenURL: '', userInfoURI: '', clientID: '', clientSecret: '', callbackURL: '' }, function (token, done) { })); /** * 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="/signin">Signin</a>'); }); /** * Passport authenticate route */ server.get('/signin', passport.authenticate('openidconnect')); /** * Handle Anvil Connect Auth Flow Response */ server.get('/callback', passport.authenticate('openidconnect', { successRedirect: '/' })); //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 // }); // }); // }); //}); /** * Error Handler */ server.use(function (err, req, res, next) { console.log('ERROR', err) res.json(err); }) /** * Start the server */ server.listen(3001);