Skip to content

Instantly share code, notes, and snippets.

View justingreerbbi's full-sized avatar

Justin Greer justingreerbbi

View GitHub Profile
@justingreerbbi
justingreerbbi / gist:720e47f0204d70fa6097e0d6f639df1b
Created September 8, 2021 15:59
Remove OAuth Access Tokens when a user logs out of WordPress
/**
* Remove all OAuth access tokens when a user logs out of the WordPress
* @param $user_id
*/
function wo_example_destroy_authorizations( $user_id ) {
global $wpdb;
$wpdb->delete( "{$wpdb->prefix}oauth_access_tokens", array( "user_id" => $user_id ) );
$wpdb->delete( "{$wpdb->prefix}oauth_refresh_tokens", array( "user_id" => $user_id ) );
}
@justingreerbbi
justingreerbbi / custom-redirect-trigger.php
Last active December 23, 2020 15:42
Simple custom redirect hook for WordPress with domain whitelisting. Simply download the file
<?php
/**
* Plugin Name: Custom Redirect Trigger
*
* This plugin allows all redirects to be processed. It can be dangerous if used incorrectly. This includes
* not adding a whitelist domain redirect. Having this wide open will all hackish redirects.
*/
class Custom_Redirect_Rewrites {
<p>Introspection Test</p>
<form action="your-site.com/oauth/introspection/" method="POST">
<input type="text" name="access_token" value=""/>
<button type="submit">Run Introspection Test</button>
</form>
function wp_oauth_server_debug_backtrace() {
$friendly = array();
$backtrace = debug_backtrace();
foreach ( $backtrace as $file ) {
$friendly[] = array(
'file' => $file['file'],
'function' => $file['function'],
'line' => $file['line']
);
}
@justingreerbbi
justingreerbbi / gist:6b9badadd7024ee90db7879d4a4dad30
Created February 9, 2020 14:12
WordPress OAuth Server Set Access Token Action
do_action( 'wo_set_access_token', array(
'access_token' => $access_token,
'client_id' => $client_id,
'user_id' => $user_id
) );
@justingreerbbi
justingreerbbi / gist:8587d270cab2328bcd45f717b11336f6
Created November 15, 2019 00:55
Check Before Authorizing a User in WordPress OAuth after using Authorization Code Grant Type.
add_action( 'wo_authorization_code_authorize', 'before_authorization_test' );
function before_authorization_test( $user_id ) {
$user_id = $user_id[0];
$authorized = true;
// Do user check and authorize if allowed
// Present error is the user is not authorized
@justingreerbbi
justingreerbbi / gist:d1d7e6af47796a9ff2a647fcc3cc1108
Created November 14, 2019 23:07
User Redirect after login to WordPress REST API
/**
* This can be used in conjuction https://gist.github.com/justingreerbbi/768f1effcca69b4098c9d0f7731deba0
* This code would go on the WP side.
*/
add_action('clear_auth_cookie', 'wp_oaut_server_logout_user_rediect_to_client_rest_api_endpoint');
function wp_oaut_server_logout_user_rediect_to_client_rest_api_endpoint(){
wp_redirect('https://site.com/wp-json/wpoauthserver/v1/logout/');
}
@justingreerbbi
justingreerbbi / gist:4d68b9823b65ed81ca1e627c0c3c35b4
Created October 8, 2019 11:15
Auto SSO with WP OAuth Server and Single Sign On Simple Client
add_filter( 'template_redirect', 'auto_sso_init', 11 );
function auto_sso_init() {
if ( ! is_user_logged_in() ) {
wp_safe_redirect( site_url( '?auth=sso' ) );
exit;
}
}
@justingreerbbi
justingreerbbi / gist:c8281d110588ea63dd39a4c1556868c4
Created August 23, 2019 13:13
Proper Custom Login WordPress Redirect Snippet
function _proper_custom_login_redirect_intercept( $template ) {
if ( ! is_user_logged_in() ) {
wp_safe_redirect( site_url( '/member-login/' ) );
exit;
}
return $template;
}
@justingreerbbi
justingreerbbi / gist:3f60aab604c70d2fc688d642e5d5c704
Created August 22, 2019 11:31
Do Action Hook for WP OAuth Server Client for User Creation or Login.
Plugin: Simple Single Sign On
Plugin File: /includes/callback.php
/*
* Action runs directly after a default user is created
*/
do_action( 'wpoc_user_created', $user_info, 1 );
/*
* Action runs directly before a user is logged in