From 5041bc0319f25412045d7494d90b5f94bed9f57a Mon Sep 17 00:00:00 2001 From: Joao Santos Date: Wed, 10 Jan 2024 12:01:25 -0300 Subject: [PATCH] working --- env.example | 7 +- index.js | 125 ++++++++++++++++++++--------- package-lock.json | 199 ++++++++++++++++++++++++++++++++++++++++++++-- package.json | 5 +- 4 files changed, 290 insertions(+), 46 deletions(-) diff --git a/env.example b/env.example index c035288..04cb1fa 100644 --- a/env.example +++ b/env.example @@ -1,7 +1,9 @@ +BASE_HOST_URL=http://localhost:3000 SIGNALWIRE_PROJECT_KEY=YOURPROJECTKEY SIGNALWIRE_TOKEN=YOURTOKEN SIGNALWIRE_SPACE=YOURSPACE.signalwire.com DEFAULT_DESTINATION=SOMETARGET +CLIENT_ID=SW_CLIENT_ID #Firebase Initilization Params FIREBASE_API_KEY= @@ -18,4 +20,7 @@ OAUTH_CLIENT_ID= OAUTH_SECRET= OAUTH_TOKEN_URI=https://id.fabric.swire.io/oauth/token OAUTH_AUTH_URI=https://id.fabric.swire.io/login/oauth/authorize -OAUTH_REDIRECT_URI=https://.ngrok-free.app/callback \ No newline at end of file +OAUTH_REDIRECT_URI=https://.ngrok-free.app/callback +OAUTH_USERINFO_URI=https://fabric.swire.io/subscriber/info + +AUTH_SECRET= //use "openssl rand -hex 32" to get one \ No newline at end of file diff --git a/index.js b/index.js index f203aae..b7527dc 100644 --- a/index.js +++ b/index.js @@ -1,46 +1,83 @@ require('dotenv').config(); const express = require('express'); const app = express(); +const cors = require('cors'); const axios = require('axios'); +const cookie_parser = require('cookie-parser') app.set('view engine', 'ejs'); app.use(express.urlencoded({ extended: true })); app.use(express.json()); +app.use(cors('*')) +app.use(cookie_parser()); app.use(express.static('public')); -app.set('trust proxy') - - -import('@auth/express').then(({ ExpressAuth }) => { - app.use('/api/auth/*', ExpressAuth({ - providers: [ - { - id: 'signalwire', - name: 'SignalWire', - type: 'oauth', - authorization: { - url: process.env.OAUTH_AUTH_URI, - params: { scope: 'email' } - }, - clientId: process.env.OAUTH_CLIENT_ID, - clientSecret: process.env.OAUTH_SECRET, - token: process.env.OAUTH_TOKEN_URI, - userinfo: process.env.OAUTH_REDIRECT_URI, - profile(profile) { - console.log('$$$$$$', profile); - return { - id: profile.id, - email: profile.email, - first_name: profile.first_name, - last_name: profile.last_name, - display_name: profile.display_name, - job_title: profile.job_title, - push_notification_key: profile.push_notification_key - }; +app.set('trust proxy', 1); + +const authConfig = { + providers: [ + { + id: 'signalwire', + name: 'SignalWire', + type: 'oauth', + authorization: { + url: process.env.OAUTH_AUTH_URI, + params: { scope: 'email' } + }, + clientId: process.env.OAUTH_CLIENT_ID, + clientSecret: process.env.OAUTH_SECRET, + token: process.env.OAUTH_TOKEN_URI, + userinfo: process.env.OAUTH_USERINFO_URI, + profile(profile) { + return { + id: profile.id, + email: profile.email, + first_name: profile.first_name, + last_name: profile.last_name, + display_name: profile.display_name, + job_title: profile.job_title, + push_notification_key: profile.push_notification_key + }; + } + } + ], + callbacks: { + jwt: ({ token, account, profile }) => { + if (account) { + token.accessToken = account.access_token + token.id = profile.id + token.pushNotificationKey = profile.push_notification_key + } + return token + }, + session({ session, token }) { + return { + ...session, + sat: token.accessToken, + pushNotificationKey: token.pushNotificationKey, + user: { + id: token.id, + ...session.user, + username: session.user.email } } - ] - })) + } + } +} + +let authGetSession + +import('@auth/express').then(({ ExpressAuth, getSession }) => { + authGetSession = getSession + async function authSession(req, res, next) { + res.locals.session = await getSession(req, authConfig) + next() + } + + + + app.use('/api/auth/*', ExpressAuth(authConfig)) + app.use(authSession) app.listen(process.env.PORT || 3000, () => { console.log("Server running on port 3000"); @@ -68,13 +105,16 @@ const token_request = { const host = process.env.RELAY_HOST +const authtentication = async (req, res, next) => { + const session = res.locals.session ?? (await authGetSession(req, authConfig)) -async function authtentication(req, res, next) { - console.log(JSON.stringify(req.params)) - console.log(JSON.stringify(req.cookies)) - console.log(JSON.stringify(req.body)) - console.log(JSON.stringify(req.headers)) - return res.redirect("/api/auth/signin") + if (!session?.user) { + callbackUrl = process.env.OAUTH_REDIRECT_URI ?? `${process.env.BASE_HOST_URL}/oauth` + res.redirect(`/api/auth/signing?callbackUrl=${callbackUrl`) + } else { + res.locals['session'] = session + next() + } } @@ -90,7 +130,7 @@ async function apiRequest(endpoint, payload = {}, method = 'POST') { return resp.data } -app.get('/', authtentication, async (req, res) => { +app.get('/', async (req, res) => { const response = await apiRequest('/api/fabric/subscribers/tokens', token_request) res.render('index', { host, @@ -111,7 +151,14 @@ app.get('/minimal', async (req, res) => { }); app.get('/oauth', authtentication, (req, res) => { - res.send(200) + const { session } = res.locals + + res.render('index', { + host, + token: session.sat, + destination: process.env.DEFAULT_DESTINATION, + firebaseConfig: FIREBASE_CONFIG, + }); }); // app.get('/callback', async (req, res) => { diff --git a/package-lock.json b/package-lock.json index c02e62c..c17a41a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,9 +12,12 @@ "@auth/express": "^0.1.2", "axios": "^1.5.0", "client-oauth2": "^4.3.3", + "cookie-parser": "^1.4.6", + "cors": "^2.8.5", "dotenv": "^16.3.1", "ejs": "^3.1.9", - "express": "^4.18.2" + "express": "^4.18.2", + "express-requests-logger": "^4.0.0" }, "devDependencies": { "jest": "^29.7.0", @@ -1560,6 +1563,23 @@ "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==", "dev": true }, + "node_modules/bunyan": { + "version": "1.8.15", + "resolved": "https://registry.npmjs.org/bunyan/-/bunyan-1.8.15.tgz", + "integrity": "sha512-0tECWShh6wUysgucJcBAoYegf3JJoZWibxdqhTm7OHPeT42qdjkZ29QCMcKwbgU1kiH+auSIasNRXMLWXafXig==", + "engines": [ + "node >=0.10.0" + ], + "bin": { + "bunyan": "bin/bunyan" + }, + "optionalDependencies": { + "dtrace-provider": "~0.8", + "moment": "^2.19.3", + "mv": "~2", + "safe-json-stringify": "~1" + } + }, "node_modules/byte-length": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/byte-length/-/byte-length-1.0.2.tgz", @@ -1803,11 +1823,43 @@ "node": ">= 0.6" } }, + "node_modules/cookie-parser": { + "version": "1.4.6", + "resolved": "https://registry.npmjs.org/cookie-parser/-/cookie-parser-1.4.6.tgz", + "integrity": "sha512-z3IzaNjdwUC2olLIB5/ITd0/setiaFMLYiZJle7xg5Fe9KWAceil7xszYfHHBtDFYLSgJduS2Ty0P1uJdPDJeA==", + "dependencies": { + "cookie": "0.4.1", + "cookie-signature": "1.0.6" + }, + "engines": { + "node": ">= 0.8.0" + } + }, + "node_modules/cookie-parser/node_modules/cookie": { + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.1.tgz", + "integrity": "sha512-ZwrFkGJxUR3EIoXtO+yVE69Eb7KlixbaeAWfBQB9vVsNn/o+Yw69gBWSSDK825hQNdN+wF8zELf3dFNl/kxkUA==", + "engines": { + "node": ">= 0.6" + } + }, "node_modules/cookie-signature": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", "integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==" }, + "node_modules/cors": { + "version": "2.8.5", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", + "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", + "dependencies": { + "object-assign": "^4", + "vary": "^1" + }, + "engines": { + "node": ">= 0.10" + } + }, "node_modules/create-jest": { "version": "29.7.0", "resolved": "https://registry.npmjs.org/create-jest/-/create-jest-29.7.0.tgz", @@ -1941,6 +1993,19 @@ "url": "https://github.com/motdotla/dotenv?sponsor=1" } }, + "node_modules/dtrace-provider": { + "version": "0.8.8", + "resolved": "https://registry.npmjs.org/dtrace-provider/-/dtrace-provider-0.8.8.tgz", + "integrity": "sha512-b7Z7cNtHPhH9EJhNNbbeqTcXB8LGFFZhq1PGgEvpeHlzd36bhbdTWoE/Ba/YguqpBSlAPKnARWhVlhunCMwfxg==", + "hasInstallScript": true, + "optional": true, + "dependencies": { + "nan": "^2.14.0" + }, + "engines": { + "node": ">=0.10" + } + }, "node_modules/ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -2134,6 +2199,16 @@ "node": ">= 0.10.0" } }, + "node_modules/express-requests-logger": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/express-requests-logger/-/express-requests-logger-4.0.0.tgz", + "integrity": "sha512-NHQptnDY0fceiTSWLnW0dbJSFlrvbFpCGHmY6LsTMmJLgkyO3x8qAJ+EsryQRMga20YH8Ynt/vnmg23QP07h1Q==", + "dependencies": { + "bunyan": "^1.8.14", + "flat": "^5.0.2", + "lodash": "^4.17.14" + } + }, "node_modules/fast-json-stable-stringify": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz", @@ -2218,6 +2293,14 @@ "node": ">=8" } }, + "node_modules/flat": { + "version": "5.0.2", + "resolved": "https://registry.npmjs.org/flat/-/flat-5.0.2.tgz", + "integrity": "sha512-b6suED+5/3rTpUBdG1gupIl8MPFCAMA0QXwmljLhvCUKcUvdE4gWky9zpuGCcXHOsz4J9wPGNWq6OKpmIzz3hQ==", + "bin": { + "flat": "cli.js" + } + }, "node_modules/follow-redirects": { "version": "1.15.4", "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.4.tgz", @@ -2536,7 +2619,7 @@ "version": "1.0.6", "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", "integrity": "sha512-k92I/b08q4wvFscXCLvqfsHCrjrF7yiXsQuIVvVE7N82W3+aqpzuUdBbfhWcy/FZR3/4IgflMgKLOsvPDrGCJA==", - "dev": true, + "devOptional": true, "dependencies": { "once": "^1.3.0", "wrappy": "1" @@ -3471,6 +3554,11 @@ "node": ">=8" } }, + "node_modules/lodash": { + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==" + }, "node_modules/lru-cache": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-5.1.1.tgz", @@ -3640,17 +3728,76 @@ "node": "*" } }, + "node_modules/minimist": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz", + "integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==", + "optional": true, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/mkdirp": { + "version": "0.5.6", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.6.tgz", + "integrity": "sha512-FP+p8RB8OWpF3YZBCrP5gtADmtXApB5AMLn+vdyA+PyxCjrCs00mjyUozssO33cwDeT3wNGdLxJ5M//YqtHAJw==", + "optional": true, + "dependencies": { + "minimist": "^1.2.6" + }, + "bin": { + "mkdirp": "bin/cmd.js" + } + }, + "node_modules/moment": { + "version": "2.30.1", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.30.1.tgz", + "integrity": "sha512-uEmtNhbDOrWPFS+hdjFCBfy9f2YoyzRpwcl+DqpC6taX21FzsTLQVbMV/W7PzNSX6x/bhC1zA3c2UQ5NzH6how==", + "optional": true, + "engines": { + "node": "*" + } + }, "node_modules/ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==" }, + "node_modules/mv": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/mv/-/mv-2.1.1.tgz", + "integrity": "sha512-at/ZndSy3xEGJ8i0ygALh8ru9qy7gWW1cmkaqBN29JmMlIvM//MEO9y1sk/avxuwnPcfhkejkLsuPxH81BrkSg==", + "optional": true, + "dependencies": { + "mkdirp": "~0.5.1", + "ncp": "~2.0.0", + "rimraf": "~2.4.0" + }, + "engines": { + "node": ">=0.8.0" + } + }, + "node_modules/nan": { + "version": "2.18.0", + "resolved": "https://registry.npmjs.org/nan/-/nan-2.18.0.tgz", + "integrity": "sha512-W7tfG7vMOGtD30sHoZSSc/JVYiyDPEyQVso/Zz+/uQd0B0L46gtC+pHha5FFMRpil6fm/AoEcRWyOVi4+E/f8w==", + "optional": true + }, "node_modules/natural-compare": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz", "integrity": "sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==", "dev": true }, + "node_modules/ncp": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ncp/-/ncp-2.0.0.tgz", + "integrity": "sha512-zIdGUrPRFTUELUvr3Gmc7KZ2Sw/h1PiVM0Af/oHB6zgnV1ikqSfRk+TOufi79aHYCW3NiOXmr1BP5nWbzojLaA==", + "optional": true, + "bin": { + "ncp": "bin/ncp" + } + }, "node_modules/negotiator": { "version": "0.6.3", "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.3.tgz", @@ -3820,6 +3967,14 @@ "url": "https://github.com/sponsors/panva" } }, + "node_modules/object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/object-inspect": { "version": "1.13.1", "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.1.tgz", @@ -3843,7 +3998,7 @@ "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", "integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==", - "dev": true, + "devOptional": true, "dependencies": { "wrappy": "1" } @@ -3953,7 +4108,7 @@ "version": "1.0.1", "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", "integrity": "sha512-AVbw3UJ2e9bq64vSaS9Am0fje1Pa8pbGqTTsmXfaIiMpnr5DlDhfJOuLj9Sf95ZPVDAUerDfEk88MPmPe7UCQg==", - "dev": true, + "devOptional": true, "engines": { "node": ">=0.10.0" } @@ -4329,6 +4484,34 @@ "node": ">=10" } }, + "node_modules/rimraf": { + "version": "2.4.5", + "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.4.5.tgz", + "integrity": "sha512-J5xnxTyqaiw06JjMftq7L9ouA448dw/E7dKghkP9WpKNuwmARNNg+Gk8/u5ryb9N/Yo2+z3MCwuqFK/+qPOPfQ==", + "optional": true, + "dependencies": { + "glob": "^6.0.1" + }, + "bin": { + "rimraf": "bin.js" + } + }, + "node_modules/rimraf/node_modules/glob": { + "version": "6.0.4", + "resolved": "https://registry.npmjs.org/glob/-/glob-6.0.4.tgz", + "integrity": "sha512-MKZeRNyYZAVVVG1oZeLaWie1uweH40m9AZwIwxyPbTSX4hHrVYSzLg0Ro5Z5R7XKkIX+Cc6oD1rqeDJnwsB8/A==", + "optional": true, + "dependencies": { + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "2 || 3", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + }, + "engines": { + "node": "*" + } + }, "node_modules/safe-buffer": { "version": "5.2.1", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", @@ -4348,6 +4531,12 @@ } ] }, + "node_modules/safe-json-stringify": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/safe-json-stringify/-/safe-json-stringify-1.2.0.tgz", + "integrity": "sha512-gH8eh2nZudPQO6TytOvbxnuhYBOvDBBLW52tz5q6X58lJcd/tkmqFR+5Z9adS8aJtURSXWThWy/xJtJwixErvg==", + "optional": true + }, "node_modules/safer-buffer": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", @@ -4930,7 +5119,7 @@ "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==", - "dev": true + "devOptional": true }, "node_modules/write-file-atomic": { "version": "4.0.2", diff --git a/package.json b/package.json index 627d2a2..121ebea 100644 --- a/package.json +++ b/package.json @@ -15,9 +15,12 @@ "@auth/express": "^0.1.2", "axios": "^1.5.0", "client-oauth2": "^4.3.3", + "cookie-parser": "^1.4.6", + "cors": "^2.8.5", "dotenv": "^16.3.1", "ejs": "^3.1.9", - "express": "^4.18.2" + "express": "^4.18.2", + "express-requests-logger": "^4.0.0" }, "devDependencies": { "jest": "^29.7.0",