authenticate serial number over HTTP

Documentation, Reference material and Tutorials for Photoshop Scripting

Moderators: Kukurykus, Tom, xbytor

Patrick

authenticate serial number over HTTP

Post by Patrick »

A week or so ago Mike and I worked on a small project for an idea he had. The concept was to have a script submit a request to a PHP page over HTTP and have that remote request authenticate the information sent and return it to the script. The idea is to have a serial number in the script, first thing on run is submit it to the server which will authenticate it and return weather or not is is valid. The script could then continue or terminate based on the response it gets.

We got a working example setup, right now it is all in e-mails. This is just a placeholder for me to post the code later today when I get a chance.

Patrick

authenticate serial number over HTTP

Post by Patrick »

The JavaScript below is heaviliy borrowed from Jeff Tranberrys openImageFromWeb.jsx script.

JavaScript (what you run in photoshop to contact the server)
Code: Select all/////////////////////////
// SETUP
/////////////////////////

var socket = new Socket;
var domain = "www.8liners.net";
var port = 80;
var page = "/MikeHale/validate.php";




/////////////////////////
// MAIN
/////////////////////////

// this is our test validation
// the PHP file should accept 'mike' and reject any other name
var user = prompt('enter your name...');


if (socket.open(domain + ":" + port, "binary")){
      // alert("GET " + sImg +" HTTP/1.0\n\n");
      socket.write("GET http://" + domain + page + "?name=" + user +
" HTTP/1.0\n\n"); // get the file
      var binary = socket.read(9999999);
      binary = removeHeaders(binary);
      alert(binary);
socket.close();
}

/////////////////////////
// FUNCTIONS
/////////////////////////

// Remove header lines from HTTP response
function removeHeaders(binary){
      var bContinue = true ; // flag for finding end of header
      var line = "";
      var nFirst = 0;
      var count = 0;
      while (bContinue) {
      line = getLine(binary) ; // each header line
      bContinue = line.length >= 2 ; // blank header == end of header
      nFirst = line.length + 1 ;
      binary = binary.substr(nFirst) ;
      }
      return binary;
}


// Get a response line from the HTML
function getLine(html){
      var line = "" ;
      for (var i = 0; html.charCodeAt(i) != 10; i++){ // finding line end
      line += html ;
      }
      return line ;
}


PHP file - this resides on a webserver and authenticates the request
Code: Select all<?php
   // get the name passed in the url
   $user = $_GET['name'];

   // if the user is mike
   if ($user == 'mike') {
       // return valid
       print 'valid';
   } else {
       // return invalid for everyone else
       print 'invalid';
   }
?>

This is a very basic example. In actual use your PHP script would probly lookup the request in a database to determine if it is valid. Also, the JavaScript would probly not prompt the user - it would have the serial number embedded in the code so it couldn't be changed.

Mike had some idea's about using JSXBIN with this, I'll let him speak on that.

Patrick