From 65051b75a861baada8c8ce659a9e04788a38ca47 Mon Sep 17 00:00:00 2001 From: Tim Nolte Date: Tue, 11 Aug 2020 11:23:35 -0400 Subject: [PATCH 1/2] Add Redirect & Override Attributes Support to Shortcodes. * Adds support for setting the redirect cookie when shortcodes are used. * Adds support for overriding some attributes for authentication URLs and login buttons when using the shortcodes. * Fixes code formatting. --- .../openid-connect-generic-client-wrapper.php | 15 +++++++++++++-- includes/openid-connect-generic-client.php | 18 +++++++++++++----- includes/openid-connect-generic-login-form.php | 14 +++++++++++--- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/includes/openid-connect-generic-client-wrapper.php b/includes/openid-connect-generic-client-wrapper.php index b44fb66..ac9cb1e 100644 --- a/includes/openid-connect-generic-client-wrapper.php +++ b/includes/openid-connect-generic-client-wrapper.php @@ -97,10 +97,21 @@ class OpenID_Connect_Generic_Client_Wrapper { /** * Get the authentication url from the client * + * @param array $atts The optional attributes array when called via a shortcode. + * * @return string */ - function get_authentication_url(){ - return $this->client->make_authentication_url(); + function get_authentication_url( $atts = array() ){ + + if ( ! empty( $atts['redirect_to'] ) ) { + // Set the request query parameter used to set the cookie redirect. + $_REQUEST['redirect_to'] = $atts['redirect_to']; + $login_form = new OpenID_Connect_Generic_Login_Form( $this->settings, $this ); + $login_form->handle_redirect_cookie(); + } + + return $this->client->make_authentication_url( $atts ); + } /** diff --git a/includes/openid-connect-generic-client.php b/includes/openid-connect-generic-client.php index 9b547a0..20c8707 100644 --- a/includes/openid-connect-generic-client.php +++ b/includes/openid-connect-generic-client.php @@ -45,20 +45,28 @@ class OpenID_Connect_Generic_Client { /** * Create a single use authentication url * + * @param array $atts An optional array of override/feature attributes. + * * @return string */ - function make_authentication_url() { + function make_authentication_url( $atts = array() ) { + + $endpoint_login = ( ! empty( $atts['endpoint_login'] ) ) ? $atts['endpoint_login'] : $this->endpoint_login; + $scope = ( ! empty( $atts['scope'] ) ) ? $atts['scope'] : $this->scope; + $client_id = ( ! empty( $atts['client_id'] ) ) ? $atts['client_id'] : $this->client_id; + $redirect_uri = ( ! empty( $atts['redirect_uri'] ) ) ? $atts['redirect_uri'] : $this->redirect_uri; + $separator = '?'; if ( stripos( $this->endpoint_login, '?' ) !== FALSE ) { $separator = '&'; } $url = sprintf( '%1$s%2$sresponse_type=code&scope=%3$s&client_id=%4$s&state=%5$s&redirect_uri=%6$s', - $this->endpoint_login, + $endpoint_login, $separator, - rawurlencode( $this->scope ), - rawurlencode( $this->client_id ), + rawurlencode( $scope ), + rawurlencode( $client_id ), $this->new_state(), - rawurlencode( $this->redirect_uri ) + rawurlencode( $redirect_uri ) ); $this->logger->log( apply_filters( 'openid-connect-generic-auth-url', $url ), 'make_authentication_url' ); diff --git a/includes/openid-connect-generic-login-form.php b/includes/openid-connect-generic-login-form.php index db9b7ca..6474549 100644 --- a/includes/openid-connect-generic-login-form.php +++ b/includes/openid-connect-generic-login-form.php @@ -128,12 +128,20 @@ class OpenID_Connect_Generic_Login_Form { /** * Create a login button (link) + * + * @param array $atts Array of optional attributes to override login buton + * functionality when used by shortcode. * * @return string */ - function make_login_button() { - $text = apply_filters( 'openid-connect-generic-login-button-text', __( 'Login with OpenID Connect' ) ); - $href = $this->client_wrapper->get_authentication_url(); + function make_login_button( $atts = array() ) { + $button_text = __( 'Login with OpenID Connect' ); + if ( ! empty( $atts['button_text'] ) ) { + $button_text = $atts['button_text']; + } + + $text = apply_filters( 'openid-connect-generic-login-button-text', $button_text ); + $href = $this->client_wrapper->get_authentication_url( $atts ); ob_start(); ?> From 107a066bd5de1d1d42bc5190be010ba4b5e40307 Mon Sep 17 00:00:00 2001 From: Tim Nolte Date: Tue, 11 Aug 2020 11:23:35 -0400 Subject: [PATCH 2/2] Add Redirect & Override Attributes Support to Shortcodes. * Adds support for setting the redirect cookie when shortcodes are used. * Adds support for overriding some attributes for authentication URLs and login buttons when using the shortcodes. * Fixes code formatting. --- .../openid-connect-generic-client-wrapper.php | 15 +++++++++++++-- includes/openid-connect-generic-client.php | 18 +++++++++++++----- includes/openid-connect-generic-login-form.php | 14 +++++++++++--- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/includes/openid-connect-generic-client-wrapper.php b/includes/openid-connect-generic-client-wrapper.php index 6941dcb..b662a77 100644 --- a/includes/openid-connect-generic-client-wrapper.php +++ b/includes/openid-connect-generic-client-wrapper.php @@ -95,11 +95,22 @@ class OpenID_Connect_Generic_Client_Wrapper { /** * Get the authentication url from the client + * + * @param array $atts The optional attributes array when called via a shortcode. * * @return string */ - function get_authentication_url(){ - return $this->client->make_authentication_url(); + function get_authentication_url( $atts = array() ){ + + if ( ! empty( $atts['redirect_to'] ) ) { + // Set the request query parameter used to set the cookie redirect. + $_REQUEST['redirect_to'] = $atts['redirect_to']; + $login_form = new OpenID_Connect_Generic_Login_Form( $this->settings, $this ); + $login_form->handle_redirect_cookie(); + } + + return $this->client->make_authentication_url( $atts ); + } /** diff --git a/includes/openid-connect-generic-client.php b/includes/openid-connect-generic-client.php index 100d3a1..0c4bcac 100644 --- a/includes/openid-connect-generic-client.php +++ b/includes/openid-connect-generic-client.php @@ -45,20 +45,28 @@ class OpenID_Connect_Generic_Client { /** * Create a single use authentication url * + * @param array $atts An optional array of override/feature attributes. + * * @return string */ - function make_authentication_url() { + function make_authentication_url( $atts = array() ) { + + $endpoint_login = ( ! empty( $atts['endpoint_login'] ) ) ? $atts['endpoint_login'] : $this->endpoint_login; + $scope = ( ! empty( $atts['scope'] ) ) ? $atts['scope'] : $this->scope; + $client_id = ( ! empty( $atts['client_id'] ) ) ? $atts['client_id'] : $this->client_id; + $redirect_uri = ( ! empty( $atts['redirect_uri'] ) ) ? $atts['redirect_uri'] : $this->redirect_uri; + $separator = '?'; if ( stripos( $this->endpoint_login, '?' ) !== FALSE ) { $separator = '&'; } $url = sprintf( '%1$s%2$sresponse_type=code&scope=%3$s&client_id=%4$s&state=%5$s&redirect_uri=%6$s', - $this->endpoint_login, + $endpoint_login, $separator, - rawurlencode( $this->scope ), - rawurlencode( $this->client_id ), + rawurlencode( $scope ), + rawurlencode( $client_id ), $this->new_state(), - rawurlencode( $this->redirect_uri ) + rawurlencode( $redirect_uri ) ); $this->logger->log( apply_filters( 'openid-connect-generic-auth-url', $url ), 'make_authentication_url' ); diff --git a/includes/openid-connect-generic-login-form.php b/includes/openid-connect-generic-login-form.php index 6ef9174..6474549 100644 --- a/includes/openid-connect-generic-login-form.php +++ b/includes/openid-connect-generic-login-form.php @@ -128,12 +128,20 @@ class OpenID_Connect_Generic_Login_Form { /** * Create a login button (link) + * + * @param array $atts Array of optional attributes to override login buton + * functionality when used by shortcode. * * @return string */ - function make_login_button() { - $text = apply_filters( 'openid-connect-generic-login-button-text', __( 'Login with OpenID Connect' ) ); - $href = apply_filters( 'openid-connect-generic-login-button-url', $this->client_wrapper->get_authentication_url() ); + function make_login_button( $atts = array() ) { + $button_text = __( 'Login with OpenID Connect' ); + if ( ! empty( $atts['button_text'] ) ) { + $button_text = $atts['button_text']; + } + + $text = apply_filters( 'openid-connect-generic-login-button-text', $button_text ); + $href = $this->client_wrapper->get_authentication_url( $atts ); ob_start(); ?>