Hi
I am sure some of you had done this before, to save me few hrs of head scratching, can someone guide me to a simple piece of agent code I can include at the beginning of any code that on activating the agent´s http address, ask for a password to be input and if correct then continue to do whatever, else display ΅wrong password" . Could be quite useful.
Thanks,
Ken NZ
Are you looking for an HTML page to be returned by the agent, or you just want to require an API key with each request?
Here is the HTML that I return for my garage door. It has a password text field, but doesn’t tell you if you entered the correct password. I actually have Twilio set to tell me if someone enters an incorrect code. The http.onrequest function will handle either a request for the HTML page, or just an incoming HTTP request with an API key. I use my Pitchfork iOS app to open and close the door.
Some of it isn’t great code, but it does work.
If you copy and paste, just remember that the forum tends to mess up some of the code, usually around double quotes, so check through everything.
`
const html = @"
<script src=""http://code.jquery.com/jquery-1.9.1.min.js""></script>
<script src=""http://code.jquery.com/jquery-migrate-1.2.1.min.js""></script>
<script src=""http://d2c5utp5fpfikz.cloudfront.net/2_3_1/js/bootstrap.min.js""></script>
<link href=""//d2c5utp5fpfikz.cloudfront.net/2_3_1/css/bootstrap.min.css"" rel=""stylesheet"">
<link href=""//d2c5utp5fpfikz.cloudfront.net/2_3_1/css/bootstrap-responsive.min.css"" rel=""stylesheet"">
<title>Garage Door</title>
</head>
<body>
<script type=""text/javascript"">
function sendToImp(value){
if (window.XMLHttpRequest) {devInfoReq=new XMLHttpRequest();}
else {devInfoReq=new ActiveXObject(""Microsoft.XMLHTTP"");}
try {
devInfoReq.open('POST', document.URL, false);
devInfoReq.send(value);
} catch (err) {
console.log('Error parsing device info from imp');
}
}
function toggle(){
sendToImp(document.getElementById('password').value);
}
</script>
<div class='container'>
<div class=''>
</div>
<div class='well' style='max-width: 320px; margin: 0 auto 10px; height:280px; font-size:22px;'>
<h1 class='text-center'>Garage Door</h1>
<h3 class='text-center'>Enter Authorization Code:</hr3>
<input id='password' type='text' name='password' style='width:94%;'>
<button style='width:100%; height:30%; margin-bottom:10px; margin-top:10px;' class='btn btn-primary btn-large btn-block' onclick='toggle()'><h1>Open/Close</h1></button>
</div>
</div>
</body>
";
//****************************END HTML******************************************
//*************************TWILIO***********************************************
const TWILIO_ACCOUNT_SID = ""
const TWILIO_AUTH_TOKEN = ""
const TWILIO_FROM_NUMBER = "" // your phone no goes here
const TWILIO_TO_NUMBER = "+" // destination phone no
function send_sms(number, message) {
local twilio_url = format(“https://api.twilio.com/2010-04-01/Accounts/%s/SMS/Messages.json”, TWILIO_ACCOUNT_SID);
local auth = “Basic " + http.base64encode(TWILIO_ACCOUNT_SID+”:"+TWILIO_AUTH_TOKEN);
local body = http.urlencode({From=TWILIO_FROM_NUMBER, To=number, Body=message});
local req = http.post(twilio_url, {Authorization=auth}, body);
local res = req.sendsync();
if(res.statuscode != 201) {
server.log("error sending message: "+res.body);
}
}
//END TWILIO**********
apiKey <- ""
doorState <- “unset”;
password <- “”;
// Respond to incoming HTTP commands
http.onrequest(function(request, response) {
server.log("Incoming request: " + request.body);
if (request.body == “”) {
response.send(200, html);
}
else if (request.body == password) {
device.send(“toggleDoor”, “Toggle Door.”);
device.on(“doorToggled”, function(data) {
doorState = data;
local json = “{ “status” : { “doorState” : “” + doorState + “” }}”;
server.log("Response: " + json);
response.send(200, json);
});
}
else {
try {
local data = http.jsondecode(request.body);
//server.log("Received: " + request.body);
if (“api-key” in request.headers && request.headers[“api-key”] == apiKey) {
//server.log(request.headers[“api-key”]);
if (data.action == “status”) {
local json = “{ “status” : { “doorState” : “” + doorState + “” }}”;
server.log("Response: " + json);
response.send(200, json);
}
else if (data.action == “toggle”) {
device.send(“toggleDoor”, data.action);
device.on(“doorToggled”, function(data) {
doorState = data;
local json = “{ “status” : { “doorState” : “” + doorState + “” }}”;
server.log("Response: " + json);
response.send(200, json);
});
}
else {
server.log(request.body);
local json = “{ “status” : { “doorState” : “Missing Data in Body” }}”;
server.log("Response: " + json);
response.send(500, json);
}
}
else {
local json = “{ “status” : { “doorState” : “Unauthorized” }}”;
response.send(401, json);
send_sms(TWILIO_TO_NUMBER, “Unauthorized access to Garage Door attempted.”);
}
}
catch (ex) {
response.send(500, "Internal Server Error: " + ex);
}
}
});
device.on(“doorState”, function(data) {
doorState = data;
});
`
Hi jwehr,
Thanks for that, I am looking for a simple html page returned by the agent:
- user enter agent url
- agent serve html page with message ‘input password’; user input password
- agent code check password
- if correct continue to rest of coding, else agent serve html page with message “wrong password”
Cheers,
Ken NZ
The idea is to use this as a header code for any model, so there is some sort of security for accessing the agent and imp in the first instance.
My example wouldn’t need much modification to add a “wrong password” response.
Thanks, indeed it wont be much modification, will work thru your cool example, which also has added bonus of notification from twilo if the pw is wrong.
Sorry to bring this up,but im looking for an HTML page that require an API key with each request. I hope im making sense, thank you