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

LICENS-45 Add auth logic #16

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
LICENS-45 Resovle comments
  • Loading branch information
MlKilderkin committed Jul 17, 2023
commit 6091a4ed341d7260ab075a19f8e91298a9a582fc
4 changes: 2 additions & 2 deletions src/Uplink/API/Validation_Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -497,13 +497,13 @@ public function to_wp_format() {
if ( empty( $this->response->auth_required ) || $this->resource->has_valid_auth_token( (array) $this->response->origin ) ) {
$info->download_link = isset($this->response->download_url) ? $this->response->download_url . '&pu_get_download=1' : '';
} else {
$url = $this->response->origin->url;
$url = $this->response->origin->url;
$query_params = [
'callback_uri' => urlencode( sprintf( '%s/%s', get_site_url(), Namespaces::get_hook_name( 'connect', '%TEXTDOMAIN%' ) ) ),
Copy link
Contributor

Choose a reason for hiding this comment

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

This is hard to understand what's going on. I'm not sure I understand why the Namespaces class is necessary?

'refer' => urlencode( wp_get_referer() ),
];

$url = sprintf( '%s/%s?%s', $url, Namespaces::get_hook_name( 'oauth_connect/login' ) , http_build_query( $query_params ) );
$url = sprintf( '%s/%s?%s', $url, Namespaces::get_hook_name( 'oauth_connect/login' ) , http_build_query( $query_params ) );
$info->api_upgrade = sprintf(
esc_html__( 'Please <a href="%s">authenticate this plugin</a> to receive updates.', '%TEXTDOMAIN%' ),
$url
Expand Down
35 changes: 34 additions & 1 deletion src/Uplink/Admin/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,27 @@ class Actions {
const QUERY_VAR = 'stellarwp_action';

/**
* Register handle route for connect/disconnect
*
* @since 1.0.1
Copy link
Contributor

Choose a reason for hiding this comment

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

@borkweb how do we want to handle @since comments if the version hasn't been determined yet? We've used this in other libraries:

/**
 * Some really awesome method.
 * 
 * @since TBD
 * 
 * @return void
 */

*
* @return void
*
* @action init
*/
public function register_route() {
add_rewrite_endpoint( 'stellarwp', EP_ROOT, self::QUERY_VAR );
}

/**
* Handle auth connect and disconnect request
*
* @since 1.0.1
*
* @param \WP $wp
*
* @return void
*
* @action parse_request
*/
public function handle_auth_request( $wp ) {
Expand All @@ -41,7 +51,9 @@ public function handle_auth_request( $wp ) {
}

/**
* Remove auth tokens
* Remove auth tokens and redirect back to settings page
*
* @since 1.0.1
*/
public function handle_disconnect() {
$license = $this->get_license_object();
Expand All @@ -56,6 +68,13 @@ public function handle_disconnect() {
exit();
}

/**
* Save auth token and redirect back to referer URL
*
* @since 1.0.1
*
* @param array $args
*/
public function handle_connect( $args ) {
if ( empty( $args['token'] ) ) {
$url = $this->get_origin_url();
Expand Down Expand Up @@ -83,6 +102,13 @@ public function handle_connect( $args ) {
exit();
}

/**
* Retrieve origin URL from server
*
* @since 1.0.1
*
* @return string
*/
protected function get_origin_url() {
$license = $this->get_license_object();
$api = Config::get_container()->get( API\Client::class );
Expand All @@ -95,6 +121,13 @@ protected function get_origin_url() {
return '';
}

/**
* Retrieve License
*
* @since 1.0.1
*
* @return mixed
*/
protected function get_license_object() {
$collection = Config::get_container()->get( Collection::class );
$plugin = $collection->current();
Expand Down
7 changes: 7 additions & 0 deletions src/Uplink/Admin/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

class Auth {

/**
* Return auth button html
*
* @since 1.0.1
*
* @return mixed
*/
public static function do_auth_html() {
$collection = Config::get_container()->get( Collection::class );
$plugin = $collection->current();
Expand Down
4 changes: 3 additions & 1 deletion src/Uplink/Admin/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ public function filter_plugins_api( $result, $action, $args ) {
/**
* Handle auth and disconnect requests to origin
*
* @since 1.0.1
*
* @param \WP $wp
*/
public function auth_request( $wp ) {
if ( ! is_user_logged_in() ) {
if ( ! is_user_logged_in() || ! current_user_can( 'manage_options' ) ) {
return;
}
$this->container->get( Actions::class )->handle_auth_request( $wp );
Expand Down