Created
July 31, 2018 13:12
-
-
Save dexterlabora/d82b9c664cfcfa4df0651a18f8cc9b6f to your computer and use it in GitHub Desktop.
This Firebase Function will provide a `/splashAuth` route to authenticate a wireless client on a Meraki Network.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This Firebase Function will provide a `/splashAuth` route to authenticate a wireless client on a Meraki Network. | |
* Example | |
* [POST] https://merakicaptiveportal-vuejs.firebaseapp.com/splashAuth/ | |
* Body: | |
{"clientMac":"24:18:1d:ae:4c:71","nodeMac": "88:15:44:60:1c:1a"} | |
* Returns: | |
{ | |
"ssids": { | |
"4": { | |
"isAuthorized": true, | |
"authorizedAt": "2018-07-31 12:12:42 UTC", | |
"expiresAt": "2018-08-01 12:12:42 UTC" | |
} | |
} | |
} | |
* | |
* The confgis.ts file will contain the required parameters for these calls to function | |
* | |
* configs.ts: | |
export const configs = { | |
apiKey: "123456789012345678901234567890", // Enter Your API Key as server default | |
apiUrl: "https://n143.meraki.com/api/v0", // API URL using shard number (i.e. n143) | |
orgId: "306267", | |
ssidNum: 4 | |
}; | |
*/ | |
import * as functions from "firebase-functions"; | |
// External Configuration File | |
import { configs } from "./configs"; | |
import axios from "axios"; | |
/** | |
* Meraki API Handlers | |
*/ | |
const meraki = axios.create({ | |
baseURL: configs.apiUrl, | |
headers: { | |
"X-Cisco-Meraki-API-Key": configs.apiKey, | |
"Content-Type": "application/json" | |
} | |
}); | |
function getOrgDevices(orgId) { | |
return meraki | |
.get("/organizations/" + orgId + "/deviceStatuses") | |
.then(res => { | |
return res.data; | |
}) | |
.catch(e => { | |
console.log("getOrgDevices error", e); | |
}); | |
} | |
function updateSplashAuth(netId, clientMac, data) { | |
return meraki | |
.put( | |
"/networks/" + | |
netId + | |
"/clients/" + | |
clientMac + | |
"/splashAuthorizationStatus", | |
data | |
) | |
.then(res => res.data) | |
.catch(e => { | |
console.log("updateSplashAuth error", e); | |
}); | |
} | |
/** | |
* Main Script to Authenticate Client on SSID | |
*/ | |
async function splashAuth(orgId, clientMac, nodeMac) { | |
// Find Network ID via Device Search | |
const devices = await getOrgDevices(orgId) | |
console.log("getOrgDevices devices", devices); | |
// Find Device by filtering on the Node MAC | |
const device = devices.filter(d => { | |
if (d.mac == nodeMac) { | |
return d; | |
} | |
})[0]; | |
if (!device["networkId"]) { | |
console.log("device not found"); | |
return "device not found"; | |
} | |
console.log("device found!", device); | |
// Authenticate Client -- manual from config settings | |
const authBody = { | |
ssids: { [configs.ssidNum]: { isAuthorized: true } } | |
}; | |
console.log("authBody", authBody); | |
const clientAuthRes = await updateSplashAuth( | |
device["networkId"], | |
clientMac, | |
authBody | |
); | |
console.log("clientAuthRes", clientAuthRes); | |
return clientAuthRes; | |
} | |
/** | |
* Function Export for Client App | |
*/ | |
export const authClient = functions.https.onRequest((req, res) => { | |
console.log("API request url", req.url); | |
console.log("API request headers", req.headers); | |
console.log("request body, ", req.body); | |
const clientMac = req.body.clientMac; | |
const nodeMac = req.body.nodeMac; | |
const orgId = configs.orgId; | |
splashAuth(orgId, clientMac, nodeMac) | |
.then(data => { | |
res.send(data); | |
}) | |
.catch(e => { | |
res.send(e); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment