You are here

function oauth2_server_client_form in OAuth2 Server 7

Generates the client editing form.

File

includes/oauth2_server.client_admin.inc, line 121
Admin UI for clients.

Code

function oauth2_server_client_form($form, &$form_state, $client, $op = 'edit') {

  // Update old clients that didn't have a settings property.
  if (!is_array($client->settings)) {
    $new_client = entity_create('oauth2_server_client', array());
    $client->settings = $new_client->settings;
  }

  // Make sure the parent server is present in form state.
  if (!isset($form_state['server'])) {
    $server = oauth2_server_load(arg(4));
    if (!$server) {
      return $form;
    }
    $form_state['server'] = $server;
  }

  // Set the server on new client entities, since it serves as the bundle.
  if (empty($client->server)) {
    $client->server = $form_state['server']->name;
  }

  // entity_form_field_validate() builds a fake entity from
  // $form_state['values'], so the bundle needs to be in there.
  $form['server'] = array(
    '#type' => 'value',
    '#value' => $form_state['server']->name,
  );
  $form['#tree'] = TRUE;
  $form['label'] = array(
    '#title' => t('Label'),
    '#type' => 'textfield',
    '#default_value' => $client->label,
    '#description' => t('The human-readable name of this client.'),
    '#required' => TRUE,
    '#weight' => -50,
  );
  $form['client_key'] = array(
    '#title' => t('Client ID'),
    '#type' => 'textfield',
    '#default_value' => $client->client_key,
    '#required' => TRUE,
    '#weight' => -40,
  );
  $form['require_client_secret'] = array(
    '#type' => 'checkbox',
    '#title' => t('Require a client secret'),
    '#default_value' => !empty($client->is_new) || !empty($client->client_secret),
    '#weight' => -35,
  );
  $grant_types = array_filter($form_state['server']->settings['grant_types']);
  $jwt_bearer_enabled = isset($grant_types['urn:ietf:params:oauth:grant-type:jwt-bearer']);
  $form['client_secret'] = array(
    '#title' => t('Client secret'),
    '#type' => 'password',
    '#weight' => -30,
    // Hide this field if only JWT bearer is enabled, since it doesn't use it.
    '#access' => count($grant_types) != 1 || !$jwt_bearer_enabled,
    '#states' => array(
      'required' => array(
        'input[name="require_client_secret"]' => array(
          'checked' => TRUE,
        ),
      ),
      'visible' => array(
        'input[name="require_client_secret"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  if (!empty($client->client_secret)) {
    $form['client_secret']['#description'] = t('Leave this blank, and leave "Require a client secret" checked, to use the previously saved secret.');
    unset($form['client_secret']['#states']['required']);
  }
  $form['public_key'] = array(
    '#title' => t('Public key'),
    '#type' => 'textarea',
    '#default_value' => $client->public_key,
    '#required' => TRUE,
    '#description' => t('Used to decode the JWT when the %JWT grant type is used.', array(
      '%JWT' => t('JWT bearer'),
    )),
    '#weight' => -20,
    // Show the field if JWT bearer is enabled, other grant types don't use it.
    '#access' => $jwt_bearer_enabled,
  );
  $form['redirect_uri'] = array(
    '#title' => t('Redirect URIs'),
    '#type' => 'textarea',
    '#default_value' => $client->redirect_uri,
    '#description' => t('The absolute URIs to validate against. Enter one value per line.'),
    '#required' => TRUE,
    '#weight' => -10,
  );
  field_attach_form('oauth2_server_client', $client, $form, $form_state);
  $form['automatic_authorization'] = array(
    '#title' => t('Automatically authorize this client'),
    '#type' => 'checkbox',
    '#default_value' => $client->automatic_authorization,
    '#description' => t('This will cause the authorization form to be skipped. <b>Warning:</b> Give to trusted clients only!'),
    '#weight' => 39,
  );
  $form['settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced settings'),
    '#collapsible' => TRUE,
    '#weight' => 40,
  );
  $form['settings']['override_grant_types'] = array(
    '#title' => t('Override available grant types'),
    '#type' => 'checkbox',
    '#default_value' => !empty($client->settings['override_grant_types']),
  );
  $form['settings']['allow_implicit'] = array(
    '#type' => 'checkbox',
    '#title' => t('Allow the implicit flow'),
    '#description' => t('Allows clients to receive an access token without the need for an authorization request token.'),
    '#default_value' => !empty($client->settings['allow_implicit']),
    '#states' => array(
      'visible' => array(
        '#edit-settings-override-grant-types' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $grant_types = oauth2_server_grant_types();

  // Prepare a list of available grant types.
  $grant_type_options = array();
  foreach ($grant_types as $type => $grant_type) {
    $grant_type_options[$type] = $grant_type['name'];
  }
  $form['settings']['grant_types'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Enabled grant types'),
    '#options' => $grant_type_options,
    '#default_value' => $client->settings['grant_types'],
    '#states' => array(
      'visible' => array(
        '#edit-settings-override-grant-types' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );

  // Add any grant type specific settings.
  foreach ($grant_types as $type => $grant_type) {

    // Merge-in any provided defaults.
    if (isset($grant_type['default settings'])) {
      $client->settings += $grant_type['default settings'];
    }

    // Add the form elements.
    if (isset($grant_type['settings callback'])) {
      $dom_ids = array();
      $dom_ids[] = 'edit-settings-override-grant-types';
      $dom_ids[] = 'edit-settings-grant-types-' . str_replace('_', '-', $type);
      $form['settings'] += $grant_type['settings callback']($client->settings, $dom_ids);
    }
  }
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save client'),
    '#weight' => 40,
  );
  if ($op != 'add') {
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete client'),
      '#weight' => 45,
      '#limit_validation_errors' => array(),
      '#submit' => array(
        'oauth2_server_client_form_submit_delete',
      ),
    );
  }
  return $form;
}