Last active
February 9, 2016 13:08
-
-
Save dmulvi/eb3a13e0d4a669a6c4e5 to your computer and use it in GitHub Desktop.
SugarCRM 7 example of a noLoginRequired endpoint with simple client_id/access_key Authorization
This file contains 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
<?php | |
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); | |
class ExampleNonAuthApi extends SugarApi | |
{ | |
public function registerApiRest() { | |
return array( | |
'SampleEndpoint' => array( | |
'reqType' => 'POST', | |
'path' => array('example', 'get-info'), | |
'pathVars' => array('module'), | |
'method' => 'getInfo', | |
'shortHelp' => 'Get some sample data', | |
'longHelp' => '', | |
'noLoginRequired' => true | |
), | |
); | |
} | |
private function clientAuth($client_id, $access_key) { | |
if ($client_id == 'someAppName' && $access_key == '6YrnbOXmu9dVNTrJBOI8fVVuAtGR9pza') { | |
return true; | |
} | |
else { | |
throw new Exception('Unauthorized'); | |
} | |
} | |
public function getInfo($api, $args) { | |
// setup result object | |
$result = new stdClass(); | |
try { | |
$this->clientAuth($args['client_id'], $args['access_key']); | |
$result->error = 0; | |
$result->message = 'Authorization Successful'; | |
return $result; | |
} | |
catch (Exception $e) { | |
$result->error = 1; | |
$result->message = $e->getMessage(); | |
header('X-PHP-Response-Code: 401', true, 401); | |
return $result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment