From 973341baf8b91ced2f4a5287e3863e314b429e2e Mon Sep 17 00:00:00 2001 From: Tristan Wells Date: Thu, 6 Sep 2018 09:37:15 -0700 Subject: [PATCH] Changing functionality to allow Oauth if template auth does not exist. --- smartdocs_oauth_additions.module | 141 ++++++++++++++++++------------- 1 file changed, 80 insertions(+), 61 deletions(-) diff --git a/smartdocs_oauth_additions.module b/smartdocs_oauth_additions.module index 7aa0772..4290c4a 100644 --- a/smartdocs_oauth_additions.module +++ b/smartdocs_oauth_additions.module @@ -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; + } - } 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 !!")));