Skip to content

Commit d654673

Browse files
committed
Add token list to profile
1 parent ce83515 commit d654673

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

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+
}

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)