From ad0c06225fb97aae1cde7cd220505f7fc941024a Mon Sep 17 00:00:00 2001 From: Nick6464 <49857434+Nick6464@users.noreply.github.com> Date: Sat, 11 May 2024 19:37:11 +1200 Subject: [PATCH] Added Ngrok key generation --- grantNgrokAuthtoken/function.json | 16 ++++++ grantNgrokAuthtoken/index.js | 90 +++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 grantNgrokAuthtoken/function.json create mode 100644 grantNgrokAuthtoken/index.js diff --git a/grantNgrokAuthtoken/function.json b/grantNgrokAuthtoken/function.json new file mode 100644 index 0000000..5f944bd --- /dev/null +++ b/grantNgrokAuthtoken/function.json @@ -0,0 +1,16 @@ +{ + "bindings": [ + { + "authLevel": "anonymous", + "type": "httpTrigger", + "direction": "in", + "name": "req", + "methods": ["post"] + }, + { + "type": "http", + "direction": "out", + "name": "res" + } + ] +} diff --git a/grantNgrokAuthtoken/index.js b/grantNgrokAuthtoken/index.js new file mode 100644 index 0000000..8295f65 --- /dev/null +++ b/grantNgrokAuthtoken/index.js @@ -0,0 +1,90 @@ +require('dotenv').config(); +const { createClient } = require('@supabase/supabase-js'); +const { default: axios } = require('axios'); + +const { SUPABASE_KEY, NGROK_API_KEY, PI_API_KEY } = process.env; + +const supabase = createClient( + 'https://itqtginklzbjrhusppwt.supabase.co', + SUPABASE_KEY +); + +module.exports = async function (context, req) { + const setErrorResponse = () => { + context.res = { + status: 500, + body: 'Error assigning token', + }; + }; + + try { + // If the Pi's hwid is not provided, return an error + if (!req.body || !req.body.hwid) { + console.error('No hwid provided'); + setErrorResponse(); + return; + } + + // If there is no API key, return an error + if (!req.headers.authorization) { + console.error('No API key provided'); + setErrorResponse(); + return; + } + + // Extract the API key from the Bearer token + const providedApiKey = req.headers.authorization.split(' ')[1]; + + // If the API key doesnt match the expected API key, return an error + if (providedApiKey !== PI_API_KEY) { + console.error('Invalid API key'); + setErrorResponse(); + return; + } + + // Verify the Pi is in the database + const { hwid } = req.body; + const { data, error } = await supabase + .from('garages') + .select('hwid') + .eq('hwid', hwid); + + if (error || !data || data.length === 0) { + console.error('Error:', error); + setErrorResponse(); + return; + } + + // Generate a ngrok authtoken for the Pi + const response = await generateNgrokAuthToken(hwid); + + // Extract the authtoken from the response + if (response.error || !response.data || !response.data.token) { + console.error('Error:', response.error); + setErrorResponse(); + return; + } + + // Return the authtoken to the Pi + context.res = { + status: 200, + body: { token: response.data.token }, + }; + } catch (error) { + console.error('Error:', error); + setErrorResponse(); + } +}; + +async function generateNgrokAuthToken(hwid) { + const headers = { + 'Content-Type': 'application/json', + Authorization: `Bearer ${NGROK_API_KEY}`, + 'Ngrok-Version': '2', + }; + + return await axios.post('https://api.ngrok.com/credentials', { + description: `Pi: ${hwid}`, + headers, + }); +}