Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 80 additions & 61 deletions smartdocs_oauth_additions.module
Original file line number Diff line number Diff line change
Expand Up @@ -124,75 +124,94 @@ function smartdocs_oauth_additions_form_generate_token($form, $form_state){
$security = new \Apigee\SmartDocs\Security($config, $form_state['values']['model'], $form_state['values']['revision']);
foreach($form_state['values']['security'] as $scheme_name => $grant_type) {
$scheme = $security->load($scheme_name);
if($scheme instanceof \Apigee\SmartDocs\Security\Oauth2Scheme) {
$template_auth = new \Apigee\SmartDocs\TemplateAuth($config, $form_state['values']['model']);
$template_auth_scheme = $template_auth->load($scheme_name);
if($template_auth_scheme instanceof \Apigee\SmartDocs\Security\Oauth2TemplateAuthScheme){
$oauth2Credentials = array();
$oauth2Credentials['ERRORCODE'] = '';
$oauth2Credentials['ERRORMESSAGE'] = '';
$oauth2Credentials['ACCESSTOKEN'] = '';
$oauth2Credentials['ACCESSTOKENTYPE'] = 'bearer';
$oauth2Credentials['ACCESSTOKENPARAMNAME'] = 'access_token';
$oauth2Credentials['PROXYURL'] = '';
if ($scheme instanceof \Apigee\SmartDocs\Security\Oauth2Scheme) {

// Don't use template auth unless it exists.
$template_auth_exists = FALSE;
$template_auth = new \Apigee\SmartDocs\TemplateAuth($config, $form_state['values']['model']);
try {
$template_auth_scheme = $template_auth->load($scheme_name);
}
catch (Exception $e) {
// Template auth doesn't exist, no need to throwing an exception.
}

// If template auth for model is created.
if ($template_auth_scheme instanceof \Apigee\SmartDocs\Security\Oauth2TemplateAuthScheme) {
$template_auth_exists = TRUE;
// Use model template auth client_id and client_secret.
$client_id = $template_auth_scheme->getClientId();
$client_secret = $template_auth_scheme->getClientSecret();
$postBody = array();
}

if($form_state['values']['user_app'] !== 'default' && user_is_logged_in()){
global $user;
$entity = entity_load('developer_app',array(), array('mail' => $user->mail, 'name' => $form_state['values']['user_app']));
$entity = reset($entity);
$client_id = $entity->consumerKey;
$client_secret = $entity->consumerSecret;
}
// Creating oauth2 request information.
$oauth2Credentials = array();
$oauth2Credentials['ERRORCODE'] = '';
$oauth2Credentials['ERRORMESSAGE'] = '';
$oauth2Credentials['ACCESSTOKEN'] = '';
$oauth2Credentials['ACCESSTOKENTYPE'] = 'bearer';
$oauth2Credentials['ACCESSTOKENPARAMNAME'] = 'access_token';
$oauth2Credentials['PROXYURL'] = '';

if($grant_type == 'client_credentials') {
$client_config = array(
'request.options' =>
array(
'auth' => array(
$client_id,
$client_secret,
'basic',
)
)
);
$postBody = array();

// If user app is selected and user is logged in, get client_id and client_secret from app.
if ($form_state['values']['user_app'] !== 'default' && user_is_logged_in() && !$template_auth_exists) {
global $user;
$entity = entity_load('developer_app', array(), array('mail' => $user->mail, 'name' => $form_state['values']['user_app']));
$entity = reset($entity);
$client_id = $entity->consumerKey;
$client_secret = $entity->consumerSecret;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get what you are trying to do. But if the key does not have the OAuth API Product the token would not be generated and the user would not know why this happens.

Do you think we can create an admin page to store default credentials to use (which administrators set) incase template auth don't exist?


} else if($grant_type == 'password') {
$postBody['client_id'] = $client_id;
$postBody['client_secret'] = $client_secret;
$client_config = array(
'request.options' => array(
'auth' => array(
$form_state['values']['client_username'],
$form_state['values']['client_password'],
'basic',
)
// Set up client_id and secret for basic authentication.
if ($grant_type == 'client_credentials') {
$client_config = array(
'request.options' =>
array(
'auth' => array(
$client_id,
$client_secret,
'basic',
)
)
);
}
$client = new \Guzzle\Http\Client($scheme->getAccessTokenUrl(), $client_config);
$request = $client->post('' ,array(), drupal_http_build_query($postBody));
try {
$response = $request->send();
$oauth_res = json_decode($response->getBody(true));
$oauth2Credentials['ACCESSTOKEN'] = $oauth_res->access_token;
}catch (\Guzzle\Http\Exception\BadResponseException $e) {
$response = $e->getResponse();
$error_res = json_decode($response->getBody(true));
$oauth2Credentials['ERRORCODE'] = $error_res->errorCode;
$oauth2Credentials['ERRORMESSAGE'] = $error_res->remediation ;
}catch(Exception $e){
$oauth2Credentials['ERRORCODE'] = $e->getCode();
$oauth2Credentials['ERRORMESSAGE'] = $e->getMessage();
}
return array('#type' => 'ajax', '#commands' => array(
array('command'=>'setAccessTokenAndLocation', 'data' => $oauth2Credentials),
ajax_command_invoke('[data-dismiss="modal"]', 'click', array()),
));
);
}
// Using password grant type.
else if ($grant_type == 'password') {
$postBody['client_id'] = $client_id;
$postBody['client_secret'] = $client_secret;
$client_config = array(
'request.options' => array(
'auth' => array(
$form_state['values']['client_username'],
$form_state['values']['client_password'],
'basic',
)
)
);
}
$client = new \Guzzle\Http\Client($scheme->getAccessTokenUrl(), $client_config);
$request = $client->post('', array('Content-Type' => 'application/x-www-form-urlencoded'), drupal_http_build_query($postBody));
try {
$response = $request->send();
$oauth_res = json_decode($response->getBody(TRUE));
$oauth2Credentials['ACCESSTOKEN'] = $oauth_res->access_token;
}
catch (\Guzzle\Http\Exception\BadResponseException $e) {
$response = $e->getResponse();
$error_res = json_decode($response->getBody(TRUE));
$oauth2Credentials['ERRORCODE'] = $error_res->errorCode;
$oauth2Credentials['ERRORMESSAGE'] = $error_res->remediation;
}
catch (Exception $e) {
$oauth2Credentials['ERRORCODE'] = $e->getCode();
$oauth2Credentials['ERRORMESSAGE'] = $e->getMessage();
}
return array('#type' => 'ajax', '#commands' => array(
array('command' => 'setAccessTokenAndLocation', 'data' => $oauth2Credentials),
ajax_command_invoke('[data-dismiss="modal"]', 'click', array()),
));
}
}
return array('#type' => 'ajax', '#commands' => array(ajax_command_alert("Something went wrong !!")));
Expand Down