From 13925dcb54e9894503cacbd11d3a7a5214269711 Mon Sep 17 00:00:00 2001
From: Dan Morrison <dan@coders.co.nz>
Date: Wed, 1 Apr 2015 15:15:30 +1300
Subject: Issue #1285310 by Dan Morrison, dman, Sneakyvv, PatchRanger: HTTP
 basic authentication settings in the UI

---
 wsclient_ui/wsclient_ui.inc | 175 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 175 insertions(+)

diff --git a/wsclient_ui/wsclient_ui.inc b/wsclient_ui/wsclient_ui.inc
index 0263a5a..d26e11b 100644
--- a/wsclient_ui/wsclient_ui.inc
+++ b/wsclient_ui/wsclient_ui.inc
@@ -231,6 +231,181 @@ function wsclient_service_form($form, &$form_state, $service, $op = 'edit') {
       '#weight' => 60,
     );

+    $settings = $service->settings;
+    // Simply naming the settings here to reflect the internal entity
+    // structure is enough to mean we don't need to worry about serializing
+    // it ourself.
+    $form['settings'] = array(
+      '#tree' => TRUE,
+      '#weight' => 45,
+    );
+    // Input for Authentication options.
+    $form['settings']['authentication'] = array(
+      '#type' => 'fieldset',
+      '#title' => t('Authentication'),
+    );
+    $form['settings']['authentication']['type'] = array(
+      '#type' => 'select',
+      '#options' => array(
+        'none' => t("None"),
+        'basic' => t("Basic (htauth)"),
+        'wss' => t("WSS (Web Service Security)"),
+        'oauth2' => t("OAuth2"),
+      ),
+      '#default_value' => isset($settings['authentication']['type']) ? $settings['authentication']['type'] : 'none',
+    );
+
+    $xpath = ':input[name="settings[authentication][type]"]';
+
+    $basic_states = array(
+      '#states' => array(
+        'visible' => array(
+          $xpath => array('value' => 'basic'),
+        ),
+      ),
+    );
+
+    $basic_settings = isset($settings['authentication']['basic']) ? $settings['authentication']['basic'] : array();
+
+    $form['settings']['authentication']['basic'] = array(
+      '#type' => 'fieldset',
+      '#title' => t('Basic HTAuth'),
+      '#description' => t('HTAuth requires http_client.module > 7.x-2.4'),
+    ) + $basic_states;
+    $form['settings']['authentication']['basic']['username'] = array(
+      '#type' => 'textfield',
+      '#title' => 'username',
+      '#default_value' => isset($basic_settings['username'])? $basic_settings['username'] : '',
+    );
+    $form['settings']['authentication']['basic']['password'] = array(
+      '#type' => 'textfield',
+      '#title' => 'password',
+      '#default_value' => isset($basic_settings['password'])? $basic_settings['password'] : '',
+    );
+
+    $wss_settings = isset($settings['authentication']['wss']) ? $settings['authentication']['wss'] : array();
+
+    $form['settings']['authentication']['wss'] = array(
+        '#type' => 'fieldset',
+        '#title' => t('WSS (Web Services Security)'),
+        '#states' => array(
+          'visible' => array(
+            $xpath => array('value' => 'wss'),
+          ),
+        ),
+      );
+    $form['settings']['authentication']['wss']['username'] = array(
+      '#type' => 'textfield',
+      '#title' => 'username',
+      '#default_value' => isset($wss_settings['username'])? $wss_settings['username'] : '',
+    );
+    $form['settings']['authentication']['wss']['password'] = array(
+      '#type' => 'textfield',
+      '#title' => 'password',
+      '#default_value' => isset($wss_settings['password'])? $wss_settings['password'] : '',
+    );
+
+    $oauth2_states = array(
+      '#states' => array(
+        'visible' => array(
+          $xpath => array('value' => 'oauth2'),
+        ),
+      ),
+    );
+
+    $oauth2_states_server_side = array(
+      '#states' => array(
+        'visible' => array(
+          $xpath => array('value' => 'oauth2'),
+          ':input[name="settings[authentication][oauth2][auth_flow]"]' => array('value' => 'server-side'),
+        ),
+      ),
+    );
+
+    $oauth2_states_user_password = array(
+      '#states' => array(
+        'visible' => array(
+          $xpath => array('value' => 'oauth2'),
+          ':input[name="settings[authentication][oauth2][auth_flow]"]' => array('value' => 'user-password'),
+        ),
+      ),
+    );
+
+    $form['settings']['authentication']['oauth2'] = array(
+      '#type' => 'fieldset',
+      '#title' => t('OAuth2'),
+    ) + $oauth2_states;
+
+    $oauth_settings = isset($settings['authentication']['oauth2']) ? $settings['authentication']['oauth2'] : array();
+
+    $form['settings']['authentication']['oauth2']['auth_flow'] = array(
+      '#type' => 'select',
+      '#title' => 'auth_flow',
+      '#options' => array(
+        'client-credentials' => 'client-credentials',
+        'server-side' => 'server_side',
+        'user-password' => 'user_password',
+      ),
+      '#default_value' => isset($oauth_settings['auth_flow']) ? $oauth_settings['auth_flow'] : '',
+    );
+    $form['settings']['authentication']['oauth2']['token_endpoint'] = array(
+      '#type' => 'textfield',
+      '#title' => 'token_endpoint',
+      '#default_value' => isset($oauth_settings['token_endpoint']) ? $oauth_settings['token_endpoint'] : '',
+      '#description' => t('E.g. https://server.org/oauth2/token.'),
+      #'#element_validate' => array('wsclient_ui_element_url_validate'),
+    );
+
+    $form['settings']['authentication']['oauth2']['client_id'] = array(
+      '#type' => 'textfield',
+      '#title' => 'client_id',
+      '#default_value' => isset($oauth_settings['client_id']) ? $oauth_settings['client_id'] : '',
+      '#description' => t('The client ID as registered on the oauth2 server.'),
+    );
+    $form['settings']['authentication']['oauth2']['client_secret'] = array(
+      '#type' => 'textfield',
+      '#title' => 'client_secret',
+      '#default_value' => isset($oauth_settings['client_secret']) ? $oauth_settings['client_secret'] : '',
+      '#description' => t('The client secret as registered on the oauth2 server.'),
+    );
+    $form['settings']['authentication']['oauth2']['scope'] = array(
+      '#type' => 'textfield',
+      '#title' => 'scope',
+      '#default_value' => isset($oauth_settings['scope']) ? $oauth_settings['scope'] : '',
+      '#description' => t('Space separated list of scopes.'),
+    );
+    $form['settings']['authentication']['oauth2']['query_auth'] = array(
+      '#type' => 'select',
+      '#title' => 'query_auth',
+      '#options' => array(0 => t('No'), 1 => t('Yes')),
+      '#default_value' => isset($oauth_settings['query_auth']) ? $oauth_settings['query_auth'] : 0,
+      '#description' => t("Yes will make the access token be set on the query, otherwise it'll be set on the headers."),
+    );
+    $form['settings']['authentication']['oauth2']['redirect_uri'] = array(
+      '#type' => 'textfield',
+      '#title' => 'redirect_uri',
+      '#default_value' => isset($oauth_settings['redirect_uri']) ? $oauth_settings['redirect_uri'] : '',
+      '#description' => t('E.g. https://client.org/oauth2/authorized. Only needed for server-side auth_flow.'),
+    ) + $oauth2_states_server_side;
+    $form['settings']['authentication']['oauth2']['authorization_endpoint'] = array(
+      '#type' => 'textfield',
+      '#title' => 'authorization_endpoint',
+      '#default_value' => isset($oauth_settings['authorization_endpoint']) ? $oauth_settings['authorization_endpoint'] : '',
+      '#description' => t('E.g. https://server.org/oauth2/authorize. Only needed for server-side auth_flow.'),
+    ) + $oauth2_states_server_side;
+    $form['settings']['authentication']['oauth2']['username'] = array(
+      '#type' => 'textfield',
+      '#title' => 'username',
+      '#default_value' => isset($oauth_settings['username']) ? $oauth_settings['username'] : '',
+      '#description' => t('Username of resource owner on the oauth2 server. Only needed for user-password auth_flow.'),
+    ) + $oauth2_states_user_password;
+    $form['settings']['authentication']['oauth2']['password'] = array(
+      '#type' => 'textfield',
+      '#title' => 'password',
+      '#default_value' => isset($oauth_settings['password']) ? $oauth_settings['password'] : '',
+      '#description' => t('Password of resource owner on the oauth2 server. Only needed for user-password auth_flow.'),
+    ) + $oauth2_states_user_password;
+
     // Input for global service parameters.
     $form['global_parameters'] = array(
       '#tree' => TRUE,
--
1.9.5 (Apple Git-50.3)

