-
Notifications
You must be signed in to change notification settings - Fork 49
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
[WIP] Add Nextdoor Keyring Service #113
Open
kevinchai0
wants to merge
2
commits into
beaulebens:trunk
Choose a base branch
from
kevinchai0:trunk
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
<?php | ||
|
||
/** | ||
* Nextdoor service definition. | ||
*/ | ||
|
||
class Keyring_Service_Nextdoor extends Keyring_Service_OAuth2 { | ||
const NAME = 'nextdoor'; | ||
const LABEL = 'Nextdoor'; | ||
|
||
|
||
function __construct() { | ||
parent::__construct(); | ||
|
||
// Enable "basic" UI for entering key/secret | ||
if ( ! KEYRING__HEADLESS_MODE ) { | ||
add_action( 'keyring_nextdoor_manage_ui', array( $this, 'basic_ui' ) ); | ||
add_filter( 'keyring_nextdoor_basic_ui_intro', array( $this, 'basic_ui_intro' ) ); | ||
} | ||
|
||
$this->set_endpoint("access_token", "https://auth.nextdoor.com/v2/token", "POST" ); | ||
$this->set_endpoint("authorize", "https://nextdoor.com/v3/authorize/?scope=openid%20post:write&", "POST"); | ||
|
||
$creds = $this->get_credentials(); | ||
if ( is_array( $creds ) ) { | ||
$this->app_id = $creds['app_id']; | ||
$this->key = $creds['key']; | ||
$this->secret = $creds['secret']; | ||
} | ||
$admin_url = Keyring_Util::admin_url(); | ||
$this->redirect_uri = substr( $admin_url, 0, strpos( $admin_url, '?' ) ); | ||
|
||
// Send authorization via header | ||
$this->authorization_header = 'Bearer'; | ||
add_filter( 'keyring_nextdoor_request_token_params', array( $this, 'request_token_params' ) ); | ||
add_filter( 'keyring_nextdoor_verify_token_params', array( $this, 'verify_token_params' ) ); | ||
add_filter( 'keyring_nextdoor_verify_token_post_params', array( $this, 'verify_token_post_params' ) ); | ||
add_filter( 'keyring_access_token', array( $this, 'fix_access_token_meta' ), 10, 2 ); | ||
} | ||
|
||
function basic_ui_intro() { | ||
/* translators: url */ | ||
echo '<p>' . sprintf( __( 'To use the Nextdoor service you need to be manually approved. Please reach out to get your client id and client secret.', 'keyring' )) . '</p>'; | ||
} | ||
|
||
function request_token_params( $params ) { | ||
$url_components = parse_url( $params['redirect_uri'] ); | ||
parse_str( $url_components['query'], $redirect_state ); | ||
$redirect_state['state'] = $params['state']; | ||
$params['state'] = Keyring_Util::get_hashed_parameters( $redirect_state ); | ||
$params['redirect_uri'] = $this->redirect_uri; | ||
|
||
return $params; | ||
} | ||
|
||
function verify_token_params( $params ) { | ||
unset( $params['client_id'] ); | ||
unset( $params['client_secret'] ); | ||
$params['redirect_uri'] = $this->redirect_uri; | ||
// $params[''] = ($params['code'] . '='); | ||
return $params; | ||
} | ||
|
||
function verify_token_post_params( $params ) { | ||
$params['headers'] = $this->get_basic_auth(); | ||
|
||
return $params; | ||
} | ||
|
||
function fix_access_token_meta( $access_token, $token ) { | ||
if ( 'nextdoor' !== $access_token->get_name() ) { | ||
return $access_token; | ||
} | ||
|
||
return new Keyring_Access_Token( | ||
$this->get_name(), | ||
$token['access_token'], | ||
array_merge( $access_token->get_meta(), $this->get_token()->get_meta() ) // refresh_token has been updated, and we want to make sure we store it | ||
); | ||
} | ||
|
||
function get_basic_auth() { | ||
return array( 'Authorization' => 'Basic ' . base64_encode( $this->key . ':' . $this->secret ) ); | ||
} | ||
|
||
function build_token_meta( $token ) { | ||
$split_token = explode('.', $token['id_token']); | ||
if ($token['id_token'] && count($split_token) > 2) { | ||
$decoded_id_token = json_decode(base64_decode(explode('.', $token['id_token'])[1])); | ||
$meta = array( | ||
'user_id' => $decoded_id_token->sub, | ||
'name' => 'Nextdoor Neighbor', | ||
'expires' => time() + $token['expires_in'], | ||
); | ||
} | ||
else { | ||
$meta = array(); | ||
} | ||
$this->set_token( | ||
new Keyring_Access_Token( | ||
$this->get_name(), | ||
$token['access_token'], | ||
$meta | ||
) | ||
); | ||
return apply_filters( 'keyring_access_token_meta', $meta, $this->get_name(), $token, $this ); | ||
} | ||
|
||
function get_display( Keyring_Access_Token $token ) { | ||
return $token->get_meta( 'name' ); | ||
} | ||
|
||
function refresh_token() { | ||
Keyring_Util::debug( 'Nextdoor::refresh_token()' ); | ||
// Request a new token, using the refresh_token | ||
$token = $this->get_token(); | ||
$meta = $token->get_meta(); | ||
if ( empty( $meta['refresh_token'] ) ) { | ||
return false; | ||
} | ||
|
||
// Don't bother if this token is valid for a while | ||
if ( ! $token->is_expired( 20 ) ) { | ||
return; | ||
} | ||
|
||
// Refresh our access token | ||
$response = wp_remote_post( | ||
$this->refresh_url, | ||
array( | ||
'method' => $this->refresh_method, | ||
'headers' => $this->get_basic_auth(), | ||
'body' => array( | ||
'grant_type' => 'refresh_token', | ||
'refresh_token' => $meta['refresh_token'], | ||
), | ||
) | ||
); | ||
Keyring_Util::debug( $response ); | ||
|
||
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { | ||
return false; | ||
} | ||
|
||
$return = json_decode( wp_remote_retrieve_body( $response ) ); | ||
$meta['refresh_token'] = $return->refresh_token; | ||
$meta['expires'] = time() + $return->expires_in; | ||
$access_token = new Keyring_Access_Token( | ||
$this->get_name(), | ||
$return->access_token, | ||
$meta, | ||
$token->get_uniq_id() | ||
); | ||
|
||
// Update store, and switch to new token | ||
$this->store->update( $access_token ); | ||
$this->set_token( $access_token ); | ||
} | ||
|
||
// Need to potentially refresh token before each request | ||
pablinos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
function request( $url, array $params = array() ) { | ||
$this->refresh_token(); | ||
return parent::request( $url, $params ); | ||
} | ||
|
||
function test_connection() { | ||
Keyring_Util::debug( 'Nextdoor::test_connection()' ); | ||
$response = $this->request( $this->profile_url, array( 'method' => $this->profile_method ) ); | ||
if ( ! Keyring_Util::is_error( $response ) ) { | ||
return true; | ||
} | ||
|
||
return $response; | ||
} | ||
} | ||
|
||
add_action( 'keyring_load_services', array( 'Keyring_Service_Nextdoor', 'init' ) ); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The end user doesn't need to provide client id and secret, which is stored in the WordPress server.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anybody using this service will need a client ID and secret. We should probably provide a link to a support doc or something where they can request credentials.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't have a full on support doc yet and I'm not sure if we will be creating one soon. Do you think leaving an email to contact would be fine?