Skip to content

Commit

Permalink
working
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsantosbh committed Jan 10, 2024
1 parent a7e227a commit 5041bc0
Show file tree
Hide file tree
Showing 4 changed files with 290 additions and 46 deletions.
7 changes: 6 additions & 1 deletion env.example
Original file line number Diff line number Diff line change
@@ -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=<foo>
Expand All @@ -18,4 +20,7 @@ OAUTH_CLIENT_ID=<foo>
OAUTH_SECRET=<foo>
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://<foo>.ngrok-free.app/callback
OAUTH_REDIRECT_URI=https://<foo>.ngrok-free.app/callback
OAUTH_USERINFO_URI=https://fabric.swire.io/subscriber/info

AUTH_SECRET= //use "openssl rand -hex 32" to get one
125 changes: 86 additions & 39 deletions index.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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()
}
}
Expand All @@ -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,
Expand All @@ -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) => {
Expand Down
Loading

0 comments on commit 5041bc0

Please sign in to comment.