You are here

function oauth2_server_form in OAuth2 Server 7

Generates the server editing form.

File

includes/oauth2_server.server_admin.inc, line 83
Admin UI for servers.

Code

function oauth2_server_form($form, &$form_state, $server, $op = 'edit') {

  // Make sure all needed settings keys are set, to prevent notices.
  $new_server = entity_create('oauth2_server', array());
  $server->settings += $new_server->settings;
  if ($op == 'clone') {
    $server->label .= ' (cloned)';
  }
  $form['#tree'] = TRUE;
  $form['label'] = array(
    '#title' => t('Label'),
    '#type' => 'textfield',
    '#default_value' => $server->label,
    '#description' => t('The human-readable name of this server.'),
    '#required' => TRUE,
  );
  $form['name'] = array(
    '#type' => 'machine_name',
    '#default_value' => $server->name,
    '#disabled' => entity_has_status('oauth2_server', $server, ENTITY_IN_CODE) || $op == 'edit',
    '#machine_name' => array(
      'exists' => 'oauth2_server_load',
      'source' => array(
        'label',
      ),
    ),
    '#description' => t('A unique machine-readable name for this server. It must only contain lowercase letters, numbers, and underscores.'),
  );
  field_attach_form('oauth2_server', $server, $form, $form_state);
  $form['settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Settings'),
  );
  $form['settings']['enforce_state'] = array(
    '#type' => 'value',
    '#value' => $server->settings['enforce_state'],
  );

  // The default scope is actually edited from the Scope UI to avoid showing
  // a select box with potentially thousands of options here.
  $form['settings']['default_scope'] = array(
    '#type' => 'value',
    '#value' => $server->settings['default_scope'],
  );
  $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($server->settings['allow_implicit']),
  );
  $form['settings']['use_openid_connect'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use OpenID Connect'),
    '#description' => t("Strongly recommended for login providers."),
    '#default_value' => !empty($server->settings['use_openid_connect']),
    '#access' => extension_loaded('openssl'),
  );
  $documentation_link = l('documentation', 'http://bshaffer.github.io/oauth2-server-php-docs/overview/jwt-access-tokens/');
  $form['settings']['use_crypto_tokens'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use JWT Access Tokens'),
    '#description' => t("Sends encrypted JWT access tokens that aren't stored in the database. See the !documentation for more details.", array(
      '!documentation' => $documentation_link,
    )),
    '#default_value' => !empty($server->settings['use_crypto_tokens']),
    '#access' => extension_loaded('openssl'),
  );
  $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' => $server->settings['grant_types'],
  );

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

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

    // Add the form elements.
    if (isset($grant_type['settings callback'])) {
      $dom_ids = array();
      $dom_ids[] = 'edit-settings-grant-types-' . str_replace('_', '-', $type);
      $form['settings'] += $grant_type['settings callback']($server->settings, $dom_ids);
    }
  }
  $form['settings']['advanced_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['settings']['advanced_settings']['access_lifetime'] = array(
    '#type' => 'textfield',
    '#title' => t('Access token lifetime'),
    '#description' => t('The number of seconds the access token will be valid for.'),
    '#default_value' => $server->settings['access_lifetime'],
    '#size' => 11,
  );
  $form['settings']['advanced_settings']['id_lifetime'] = array(
    '#type' => 'textfield',
    '#title' => t('ID token lifetime'),
    '#description' => t('The number of seconds the ID token will be valid for.'),
    '#default_value' => $server->settings['id_lifetime'],
    '#size' => 11,
    '#states' => array(
      'visible' => array(
        '#edit-settings-use-openid-connect' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $form['settings']['advanced_settings']['refresh_token_lifetime'] = array(
    '#type' => 'textfield',
    '#title' => t('Refresh token lifetime'),
    '#description' => t('The number of seconds the refresh token will be valid for. 0 for forever.'),
    '#default_value' => $server->settings['refresh_token_lifetime'],
    '#size' => 11,
  );
  $form['settings']['advanced_settings']['require_exact_redirect_uri'] = array(
    '#type' => 'checkbox',
    '#title' => t('Require exact redirect uri'),
    '#description' => t("Require the redirect url to be an exact match of the client's redirect url\n    If not enabled, the redirect url in the request can contain additional segments, such as a query string."),
    '#default_value' => isset($server->settings['require_exact_redirect_uri']) ? $server->settings['require_exact_redirect_uri'] : TRUE,
  );
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save server'),
    '#weight' => 40,
  );
  if (!entity_has_status('oauth2_server', $server, ENTITY_IN_CODE) && !in_array($op, array(
    'add',
    'clone',
  ))) {
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete server'),
      '#weight' => 45,
      '#limit_validation_errors' => array(),
      '#submit' => array(
        'oauth2_server_form_submit_delete',
      ),
    );
  }
  return $form;
}