Skip to content

Instantly share code, notes, and snippets.

@dmulvi
Last active February 9, 2016 13:08
Show Gist options
  • Save dmulvi/eb3a13e0d4a669a6c4e5 to your computer and use it in GitHub Desktop.
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
<?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