Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Barebones Hello World example that shows the simplicity and power of …

…Restler is added
  • Loading branch information...
commit d6a48e8eca7d93e4173ad6c46c421a875286f661 1 parent 7b7aa6a
Arul Arul- authored
31 examples/helloworld/README.txt
... ... @@ -0,0 +1,31 @@
  1 +UNZIP AND COPY THE FILES into your web root (including the .htaccess file)
  2 +Assuming that you are testing it on localhost try the following urls
  3 +
  4 + http://localhost/
  5 + should return "HelloWorld"
  6 +
  7 + http://localhost/sum
  8 + should return 0
  9 +
  10 + http://localhost/sum.json
  11 + should return 0
  12 +
  13 + http://localhost/sum.xml
  14 + should return the following xml (view source if required)
  15 + <?xml version="1.0"?>
  16 + <result>0</result>
  17 +
  18 + http://localhost/sum.json?num1=10&num2=4
  19 + should return 14
  20 +
  21 + http://localhost/multiply.json/4/2
  22 + should return 8
  23 +
  24 + http://localhost/protectedsum.json?n1=5&n2=4
  25 + should ask for username and password
  26 + give admin and mypass respectively
  27 + then it should return 9
  28 +
  29 +Now take a look at simpleservice.php to understand how the mapping is done
  30 +
  31 +Keep this as the starting point and build your own :)
85 examples/helloworld/digestauthentication.php
... ... @@ -0,0 +1,85 @@
  1 +<?php
  2 +/**
  3 + * Digest Authentication Scheme Example for Restler Framework
  4 + * @category Framework
  5 + * @package restler
  6 + * @subpackage auth
  7 + * @author R.Arul Kumaran <arul@luracast.com>
  8 + * @copyright 2010 Luracast
  9 + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  10 + * @link http://luracast.com/products/restler/
  11 + */
  12 +class DigestAuthentication implements iAuthenticate
  13 +{
  14 + public $realm = 'Restricted API';
  15 + public static $user;
  16 + public $restler;
  17 +
  18 + public function isAuthenticated()
  19 + {
  20 + //user => password hardcoded for convenience
  21 + $users = array('admin' => 'mypass', 'guest' => 'guest');
  22 +
  23 + if (empty($_SERVER['PHP_AUTH_DIGEST'])) {
  24 + header('HTTP/1.1 401 Unauthorized');
  25 + header('WWW-Authenticate: Digest realm="'.$this->realm.'",qop="auth",nonce="'.
  26 + uniqid().'",opaque="'.md5($this->realm).'"');
  27 + die('Digest Authorisation Required');
  28 + }
  29 +
  30 + // analyze the PHP_AUTH_DIGEST variable
  31 + if (!($data = DigestAuthentication::http_digest_parse($_SERVER['PHP_AUTH_DIGEST'])) ||
  32 + !isset($users[$data['username']]))
  33 + {
  34 + throw new RestException(401, 'Wrong Credentials!');
  35 + }
  36 +
  37 +
  38 + // generate the valid response
  39 + $A1 = md5($data['username'] . ':' . $this->realm . ':' . $users[$data['username']]);
  40 + $A2 = md5($_SERVER['REQUEST_METHOD'].':'.$data['uri']);
  41 + $valid_response = md5($A1.':'.$data['nonce'].':'.$data['nc'].':'.$data['cnonce'].':'.$data['qop'].':'.$A2);
  42 +
  43 + if ($data['response'] != $valid_response)
  44 + {
  45 + throw new RestException(401, 'Wrong Credentials!');
  46 + }
  47 + // ok, valid username & password
  48 + DigestAuthentication::$user=$data['username'];
  49 + return true;
  50 + }
  51 +
  52 + /**
  53 + * Logs user out of the digest authentication by bringing the login dialog again
  54 + * ignore the dialog to logout
  55 + *
  56 + * @url GET /user/login
  57 + * @url GET /user/logout
  58 + */
  59 + public function logout()
  60 + {
  61 + header('HTTP/1.1 401 Unauthorized');
  62 + header('WWW-Authenticate: Digest realm="'.$this->realm.'",qop="auth",nonce="'.
  63 + uniqid().'",opaque="'.md5($this->realm).'"');
  64 + die('Digest Authorisation Required');
  65 + }
  66 +
  67 +
  68 + // function to parse the http auth header
  69 + private function http_digest_parse($txt)
  70 + {
  71 + // protect against missing data
  72 + $needed_parts = array('nonce'=>1, 'nc'=>1, 'cnonce'=>1, 'qop'=>1, 'username'=>1, 'uri'=>1, 'response'=>1);
  73 + $data = array();
  74 + $keys = implode('|', array_keys($needed_parts));
  75 +
  76 + preg_match_all('@(' . $keys . ')=(?:([\'"])([^\2]+?)\2|([^\s,]+))@', $txt, $matches, PREG_SET_ORDER);
  77 +
  78 + foreach ($matches as $m) {
  79 + $data[$m[1]] = $m[3] ? $m[3] : $m[4];
  80 + unset($needed_parts[$m[1]]);
  81 + }
  82 +
  83 + return $needed_parts ? false : $data;
  84 + }
  85 +}
11 examples/helloworld/index.php
... ... @@ -0,0 +1,11 @@
  1 +<?php
  2 +//SIMPLE USAGE EXAMPLE
  3 +
  4 +//rester and related classes should be
  5 +//placed in include path
  6 +spl_autoload_register();
  7 +$restler = new Restler();
  8 +$restler->setSupportedFormats('JsonFormat', 'XmlFormat');
  9 +$restler->addAPIClass('SimpleService');
  10 +$restler->addAuthenticationClass('DigestAuthentication');
  11 +$restler->handle();
41 examples/helloworld/simpleservice.php
... ... @@ -0,0 +1,41 @@
  1 +<?php
  2 +class SimpleService {
  3 + /**
  4 + * HelloWorld
  5 + * @url GET /
  6 + * @url GET /index
  7 + */
  8 + function helloworld() {
  9 + return "HelloWorld";
  10 + }
  11 + /**
  12 + * Sums the given numbers
  13 + * @param integer $num1
  14 + * @param integer $num2
  15 + * @return integer
  16 + * @url GET /sum
  17 + */
  18 + function sum($num1, $num2){
  19 + return $num1+$num2;
  20 + }
  21 + /**
  22 + * Multiplies the given numbers
  23 + * @param integer $num1
  24 + * @param integer $num2
  25 + * @return integer
  26 + * @url GET /multiply/:num1/:num2
  27 + */
  28 + function multiply($num1, $num2){
  29 + return $num1*$num2;
  30 + }
  31 + /**
  32 + * Sums the given numbers
  33 + * @param integer $n1
  34 + * @param integer $n2
  35 + * @return integer
  36 + * @url GET /protectedsum
  37 + */
  38 + protected function protectedsum($n1, $n2){
  39 + return $n1+$n2;
  40 + }
  41 +}

0 comments on commit d6a48e8

Please sign in to comment.
Something went wrong with that request. Please try again.