Skip to content

Commit ac30389

Browse files
authored
Merge pull request #22 from WP-API/add-profile-list
Add list of valid tokens to user profile
2 parents 0047e2a + 93de2e4 commit ac30389

File tree

4 files changed

+157
-101
lines changed

4 files changed

+157
-101
lines changed

admin.php

Lines changed: 0 additions & 100 deletions
This file was deleted.

inc/admin/profile/namespace.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
/**
3+
* Administration UI and utilities
4+
*/
5+
6+
namespace WP\OAuth2\Admin\Profile;
7+
8+
use WP\OAuth2\Tokens\Access_Token;
9+
use WP_User;
10+
11+
/**
12+
* Bootstrap actions for the profile screen.
13+
*/
14+
function bootstrap() {
15+
add_action( 'personal_options', __NAMESPACE__ . '\\render_profile_section', 50 );
16+
add_action( 'all_admin_notices', __NAMESPACE__ . '\\output_profile_messages' );
17+
add_action( 'personal_options_update', __NAMESPACE__ . '\\handle_revocation', 10, 1 );
18+
add_action( 'edit_user_profile_update', __NAMESPACE__ . '\\handle_revocation', 10, 1 );
19+
}
20+
21+
/**
22+
* Render current tokens for a user.
23+
*
24+
* @param WP_User $user User whose profile is being rendered.
25+
*/
26+
function render_profile_section( WP_User $user ) {
27+
$tokens = Access_Token::get_for_user( $user );
28+
?>
29+
<table class="form-table">
30+
<tbody>
31+
<tr>
32+
<th scope="row"><?php _e( 'Authorized Applications', 'rest_oauth1' ) ?></th>
33+
<td>
34+
<?php if ( ! empty( $tokens ) ): ?>
35+
<table class="widefat">
36+
<thead>
37+
<tr>
38+
<th style="padding-left:10px;"><?php esc_html_e( 'Application Name', 'rest_oauth1' ); ?></th>
39+
<th></th>
40+
</tr>
41+
</thead>
42+
<tbody>
43+
<?php foreach ( $tokens as $token ): ?>
44+
<?php
45+
/** @var Access_Token $token */
46+
$client = $token->get_client();
47+
?>
48+
<tr>
49+
<td><?php echo $client->get_name() ?></td>
50+
<td><button class="button" name="oauth2_revoke" value="<?php echo esc_attr( $token->get_key() ) ?>"><?php esc_html_e( 'Revoke', 'rest_oauth1' ) ?></button>
51+
</tr>
52+
53+
<?php endforeach ?>
54+
</tbody>
55+
</table>
56+
<?php else: ?>
57+
<p class="description"><?php esc_html_e( 'No applications authorized.', 'rest_oauth1' ) ?></p>
58+
<?php endif ?>
59+
</td>
60+
</tr>
61+
</tbody>
62+
</table>
63+
<?php
64+
}
65+
66+
/**
67+
* Output messages based on previous actions.
68+
*/
69+
function output_profile_messages() {
70+
global $pagenow;
71+
if ( $pagenow !== 'profile.php' && $pagenow !== 'user-edit.php' ) {
72+
return;
73+
}
74+
75+
if ( ! empty( $_GET['oauth2_revoked'] ) ) {
76+
echo '<div id="message" class="updated"><p>' . __( 'Token revoked.', 'oauth2' ) . '</p></div>';
77+
}
78+
if ( ! empty( $_GET['oauth2_revocation_failed'] ) ) {
79+
echo '<div id="message" class="updated"><p>' . __( 'Unable to revoke token.', 'oauth2' ) . '</p></div>';
80+
}
81+
}
82+
83+
/**
84+
* Handle a revocation.
85+
*
86+
* @param int $user_id
87+
*/
88+
function handle_revocation( $user_id ) {
89+
if ( empty( $_POST['oauth2_revoke'] ) ) {
90+
return;
91+
}
92+
93+
$key = wp_unslash( $_POST['oauth2_revoke'] );
94+
$token = Access_Token::get_by_id( $key );
95+
if ( empty( $token ) ) {
96+
var_dump( $key, $token );
97+
wp_safe_redirect( add_query_arg( 'oauth2_revocation_failed', true, get_edit_user_link( $user_id ) ) );
98+
exit;
99+
}
100+
101+
// Check it's for the right user.
102+
if ( $token->get_user_id() !== $user_id ) {
103+
wp_die();
104+
}
105+
106+
$result = $token->revoke();
107+
if ( is_wp_error( $result ) ) {
108+
wp_safe_redirect( add_query_arg( 'oauth2_revocation_failed', true, get_edit_user_link( $user_id ) ) );
109+
exit;
110+
}
111+
112+
// Success, redirect and tell the user.
113+
wp_safe_redirect( add_query_arg( 'oauth2_revoked', $key, get_edit_user_link( $user_id ) ) );
114+
exit;
115+
}

inc/tokens/class-access-token.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,25 @@ public function get_client() {
2727
return Client::get_by_id( $this->value['client'] );
2828
}
2929

30+
/**
31+
* Revoke the token.
32+
*
33+
* @internal This may return other error codes in the future, as we may
34+
* need to also revoke refresh tokens.
35+
* @return bool|WP_Error True if succeeded, error otherwise.
36+
*/
37+
public function revoke() {
38+
$success = delete_user_meta( $this->get_user_id(), $this->get_meta_key() );
39+
if ( ! $success ) {
40+
return new WP_Error(
41+
'oauth2.tokens.access_token.revoke.could_not_revoke',
42+
__( 'Could not revoke the token.', 'oauth2' )
43+
);
44+
}
45+
46+
return true;
47+
}
48+
3049
/**
3150
* Get a token by ID.
3251
*
@@ -57,7 +76,27 @@ public static function get_by_id( $id ) {
5776
return null;
5877
}
5978

60-
return new static( $user, $key, $value[0] );
79+
return new static( $user, $id, $value[0] );
80+
}
81+
82+
/**
83+
* Get all tokens for the specified user.
84+
*
85+
* @return static[] List of tokens.
86+
*/
87+
public static function get_for_user( WP_User $user ) {
88+
$meta = get_user_meta( $user->ID );
89+
$tokens = [];
90+
foreach ( $meta as $key => $values ) {
91+
if ( strpos( $key, static::META_PREFIX ) !== 0 ) {
92+
continue;
93+
}
94+
95+
$real_key = substr( $key, strlen( static::META_PREFIX ) );
96+
$value = maybe_unserialize( $values[0] );
97+
$tokens[] = new static( $user, $real_key, $value );
98+
}
99+
return $tokens;
61100
}
62101

63102
/**

plugin.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function bootstrap() {
3131
// Admin-related.
3232
add_action( 'init', __NAMESPACE__ . '\\rest_oauth2_load_authorize_page' );
3333
add_action( 'admin_menu', array( __NAMESPACE__ . '\\admin\\Admin', 'register' ) );
34+
Admin\Profile\bootstrap();
3435
}
3536

3637
function load() {
@@ -48,6 +49,7 @@ function load() {
4849
require __DIR__ . '/inc/types/class-authorization-code.php';
4950
require __DIR__ . '/inc/types/class-implicit.php';
5051
require __DIR__ . '/inc/admin/class-admin.php';
52+
require __DIR__ . '/inc/admin/profile/namespace.php';
5153
}
5254

5355
/**

0 commit comments

Comments
 (0)