Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow authenticated users to generate tokens via the UI #1016

Open
wants to merge 1 commit into
base: 7.x-2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
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
27 changes: 27 additions & 0 deletions modules/restful_token_auth/restful_token_auth.module
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ function restful_token_auth_menu() {
'access arguments' => array('administer restful'),
'file' => 'restful_token_auth.admin.inc',
);
// Add an API tab to the user profile.
$items['user/%user/api'] = array(
'title' => 'API',
'description' => 'API details',
'page callback' => 'restful_token_auth_get_credentials',
'access callback' => 'user_view_access',
'access arguments' => array(1),
'page arguments' => array(1),
'file' => 'restful_token_auth.pages.inc',
'type' => MENU_LOCAL_TASK,
);
return $items;
}

Expand Down Expand Up @@ -147,3 +158,19 @@ function restful_token_auth_user_update(&$edit, $account, $category) {

entity_delete_multiple('restful_token_auth', array_keys($result['restful_token_auth']));
}

/**
* Implementation of hook_theme().
*/
function restful_token_auth_theme() {
$module_path = drupal_get_path('module', 'restful_token_auth');
return array(
'restful_token_auth_user_key' => array(
'variables' => array(
'token' => NULL,
),
'template' => 'user-key',
'path' => $module_path . '/templates',
)
);
}
92 changes: 92 additions & 0 deletions modules/restful_token_auth/restful_token_auth.pages.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

/**
* @file
* Page callbacks for restful_token_auth module.
*/

/**
* Generates and shows an API key.
*
* @param \stdClass $account
* A user account object.
*
* @return array
* A render array.
*/
function restful_token_auth_get_credentials($account) {
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', 'restful_token_auth')
->entityCondition('bundle', 'access_token')
->propertyCondition('uid', $account->uid)
->execute();

if (empty($result['restful_token_auth'])) {
return drupal_get_form('restful_token_auth_generate_token_form', $account->uid);
}
else {
$id = key($result['restful_token_auth']);
$auth_token = entity_load_single('restful_token_auth', $id);
return array(
'#theme' => 'restful_token_auth_user_key',
'#token' => $auth_token->token,
);
}
}

/**
* Form callback to generate an API token.
*
* @param array $form
* The form array.
* @param array $form_state
* The form state array.
* @param int $uid
* The user identifier.
*
* @return array
* The form as a render array.
*/
function restful_token_auth_generate_token_form($form, &$form_state, $uid) {
$form['description'] = array(
'#markup' => t('<p>You don\'t have an API key yet. Click below to generate one.</p>'),
Copy link
Member

Choose a reason for hiding this comment

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

Let's avoid markup in the translation string. Maybe consider #prefix and #suffix for this.

);
$form['uid'] = array(
'#type' => 'value',
'#value' => $uid,
);

$form['submit_button'] = array(
'#type' => 'submit',
'#value' => t('Generate token'),
);

return $form;
}

/**
* Form validator for generating a token.
*
* @param array $form
* The form array.
* @param array $form_state
* The form state array.
*/
function restful_token_auth_generate_token_form_validate($form, &$form_state) {
Copy link
Member

Choose a reason for hiding this comment

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

Do we need this?

}

/**
* Form submit handler for generating a token.
*
* @param array $form
* The form array.
* @param array $form_state
* The form state array.
*/
function restful_token_auth_generate_token_form_submit($form, &$form_state) {
/** @var \Drupal\restful_token_auth\Entity\RestfulTokenAuthController $controller */
$controller = entity_get_controller('restful_token_auth');
$controller->generateAccessToken($form_state['values']['uid']);
drupal_set_message(t('Token generated successfully.'), 'status');
}
23 changes: 23 additions & 0 deletions modules/restful_token_auth/templates/user-key.tpl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* @file
* Default theme implementation to present a user's API token plus an
* example with next steps.
*
* Available variables:
* - $token: The API token.
*
* @ingroup themeable
*/
?>
<p>Your authentication token is: <code><?php print $token; ?></code></p>

<p>Discover the API by running the following command in a terminal and replacing
[your-token] by the above string:</p>
Copy link
Member

Choose a reason for hiding this comment

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

I don't see [your-token] anywhere. Maybe it's a stale comment?


<p>
<code>
curl -H 'access-token:<?php print $token; ?>' <?php print $GLOBALS['base_url'] ?>/api?all=true
Copy link
Member

Choose a reason for hiding this comment

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

Maybe it's better to have a preprocess function that uses url(…) to set up a variable. Then we only need to print the variable here. The current code will fail if the site is installed in a subdir, or there is a language path prefix, etc.

</code>
</p>