Skip to content

Commit

Permalink
v2.4.beta
Browse files Browse the repository at this point in the history
  • Loading branch information
milesimarco committed May 23, 2022
1 parent febf2e8 commit 8792ba4
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 25 deletions.
21 changes: 15 additions & 6 deletions frontend-ui.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php


function spid_get_idp_list( $showinfo = false ) {
function spid_get_idp_list( $showinfo = false, $spid_redirect_to = '' ) {
$return = '';

$plugin_dir = plugin_dir_url( __FILE__ );
Expand Down Expand Up @@ -34,10 +33,20 @@ function spid_get_idp_list( $showinfo = false ) {
$shuffle[] = array( 'Lepida ID', 'https://id.lepida.it/idp/shibboleth', 'lepidaid', 9 );
shuffle( $shuffle );
$provider = array_merge( $provider, $shuffle );

$return .= '<ul id="spid-idp-list-small-root-get" class="spid-idp-button-menu" aria-labelledby="spid-idp">';
foreach ( $provider as $p ) {
$return .= '<li class="spid-idp-button-link" data-idp="infocertid"><a href="'.esc_url( wp_login_url().'?spid_sso=in&spid_idp='.$p[3] ).'" alt="'.$p[0].'"><img class="spid-provider" src="'.$plugin_dir.'img/idp/spid-idp-'.$p[2].'.svg" alt="'.$p[0].'" /></a></li>';
$url = wp_spid_italia_get_login_url();
$url = add_query_arg( 'spid_sso', 'in', $url );
$url = add_query_arg( 'spid_idp', $p[3], $url );
if ( $spid_redirect_to ) {
if ( $spid_redirect_to == 'CURRENT_URL' ) {
$spid_redirect_to = $_SERVER['REQUEST_URI'];
}
$url = add_query_arg( 'spid_redirect_to', $spid_redirect_to, $url );
}

$return .= '<li class="spid-idp-button-link" data-idp="infocertid"><a href="'.esc_url( $url ).'" alt="'.$p[0].'"><img class="spid-provider" src="'.$plugin_dir.'img/idp/spid-idp-'.$p[2].'.svg" alt="'.$p[0].'" /></a></li>';
}
if ( $showinfo ) {
$return .= '<li class="spid-idp-support-link"><a href="https://www.spid.gov.it">Maggiori informazioni</a></li>';
Expand Down Expand Up @@ -93,7 +102,7 @@ function spid_get_login_button_link( $size = 's' ) {
return $return;
}

function spid_get_login_button( $size = 's' ) {
function spid_get_login_button( $size = 's', $redirectTo = '' ) {

$return = '';

Expand All @@ -113,7 +122,7 @@ function spid_get_login_button( $size = 's' ) {
$return .= '<div id="spid-idp-button-small-get" class="spid-idp-button spid-idp-button-tip spid-idp-button-relative">';
$return .= '<ul id="spid-idp-list-small-root-get" class="spid-idp-button-menu" aria-labelledby="spid-idp">';

$return .= spid_get_idp_list( true );
$return .= spid_get_idp_list( true, $redirectTo );

$return .= '</ul>';
$return .= '</div>';
Expand Down
72 changes: 53 additions & 19 deletions wp-spid-italia.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,13 @@ function() { include( plugin_dir_path( __FILE__ ) . 'admin/settings.php'); spid_
} );

add_shortcode( 'spid_login_button', function( $atts ) {
$a = shortcode_atts( array(
'size' => 's',
'redirect_to' => '',
), $atts );

$button = '';
$button .= spid_get_login_button();
$button .= spid_get_login_button( $a['size'], $a['redirect_to'] );
return $button;
} );

Expand Down Expand Up @@ -167,7 +172,10 @@ function spid_get_metadata_token() {
}

add_filter( 'login_message', function( $message ) {

spid_handle();
});

function spid_handle() {
$internal_debug = false;
$spid_debug = ( WP_DEBUG === true ) || $internal_debug;

Expand All @@ -179,9 +187,9 @@ function spid_get_metadata_token() {

try {
$sp = spid_load();
if ( $sp ) {
$sp->isAuthenticated();
}
if ( $sp ) {
$sp->isAuthenticated();
}

} catch ( Exception $e) {

Expand All @@ -190,9 +198,7 @@ function spid_get_metadata_token() {
}

function spid_errors( $errorMsg2 ){
$xmlString = isset($_GET['SAMLResponse']) ?
gzinflate(base64_decode($_GET['SAMLResponse'])) :
base64_decode($_POST['SAMLResponse']);
$xmlString = isset($_GET['SAMLResponse']) ? gzinflate(base64_decode($_GET['SAMLResponse'])) : base64_decode($_POST['SAMLResponse']);
$xmlResp = new \DOMDocument();
$xmlResp->loadXML($xmlString);
if ( $xmlResp->textContent ) {
Expand Down Expand Up @@ -225,6 +231,14 @@ function spid_errors( $errorMsg2 ){
echo '<small>';
echo '<br>Auth state: '.( $sp->isAuthenticated() ? 'authenticated' : 'not authenticated' );
echo '<br>idpEntityId: '. ( isset( $_SESSION['idpEntityId'] ) ? $_SESSION['idpEntityId'] : '(not set)' );
$xmlString = isset($_GET['SAMLResponse']) ? gzinflate(base64_decode($_GET['SAMLResponse'])) : ( isset($_POST['SAMLResponse']) ? base64_decode($_POST['SAMLResponse']) : '');
if ( $xmlString ) {
$xmlResp = new \DOMDocument();
$xmlResp->loadXML($xmlString);
echo '<br>SAMLResponse: '. $xmlString;
}
echo '<br>Session: ';
print_r( $_SESSION );
echo '</small>';
echo '</form></div>';
}
Expand All @@ -248,9 +262,15 @@ function spid_errors( $errorMsg2 ){
session_destroy();
$_SESSION = NULL;
}
if ( isset( $_GET['spid_redirect_to'] ) ) {
$_SESSION['spid_redirect_to'] = $_GET['spid_redirect_to'];
}
$assertId = 0; // index of assertion consumer service as per the SP metadata (sp_assertionconsumerservice in settings array)
$attrId = 0; // index of attribute consuming service as per the SP metadata (sp_attributeconsumingservice in settings array)
$sp->login( 'idp_'.$_GET['spid_idp'], $assertId, $attrId); // Generate the login URL and redirect to the IdP login page
$_SESSION['start_login'] = 1;
//print_r($_SESSION);
//die();
$sp->login( 'idp_'.$_GET['spid_idp'], $assertId, $attrId ); // Generate the login URL and redirect to the IdP login page
} else if ( $sp->isAuthenticated() ) {
$attributes = $sp->getAttributes();
$name = $attributes['email'][0];
Expand Down Expand Up @@ -303,7 +323,7 @@ function spid_errors( $errorMsg2 ){
}

}
});
}

function spid_update_user( $user, $attributes ) {

Expand Down Expand Up @@ -347,14 +367,28 @@ function spid_update_user( $user, $attributes ) {
function wp_spid_italia_get_login_url( $dir = 'default' ) {
$default_url = wp_login_url();

if ( $default_url != apply_filters( 'spid_filter_login_url_dir_default', $default_url ) ) {
return apply_filters( 'spid_filter_login_url_dir_default', $default_url );
} else if ( $dir == 'in' ) {
return apply_filters( 'spid_filter_login_url_dir_in', $default_url );
} else if ( $dir == 'out' ) {
return apply_filters( 'spid_filter_login_url_dir_out', $default_url );
$filter_default = apply_filters( 'spid_filter_login_url_dir_default', $default_url );
$filter_in = apply_filters( 'spid_filter_login_url_dir_in', $default_url );
$filter_out = apply_filters( 'spid_filter_login_url_dir_out', $default_url );

/*
echo 'def'.$default_url.'<hr>';
echo 'f_def'.$filter_default.'<hr>';
echo 'f_in'.$filter_in.'<hr>';
echo 'f_out'.$filter_out.'<hr>';
*/

if ( $dir == 'default' ) {
return $filter_default;
}

if ( $dir == 'in' && $filter_in != $default_url ) {
return $filter_in;
} else if ( $dir == 'out' && $filter_out != $default_url ) {
return $filter_out;
}
return $default_url;

return $filter_default;
}

function is_spid_enabled() {
Expand Down Expand Up @@ -387,9 +421,9 @@ function spid_load() {
'sp_cert_file' => SPID__CERT_DIR.'sp.crt',
'sp_comparison' => 'minimum', // one of: "exact", "minimum", "better" or "maximum"
'sp_assertionconsumerservice' => [
wp_spid_italia_get_login_url( 'in' ) .'?spid_sso=in', // Servizio standard
add_query_arg( 'spid_sso', 'in', wp_spid_italia_get_login_url( 'in' ) ) // Servizio standard
],
'sp_singlelogoutservice' => [ [ wp_spid_italia_get_login_url( 'out' ) .'?spid_sso=out', '' ] ],
'sp_singlelogoutservice' => [ [ add_query_arg( 'spid_sso', 'out', wp_spid_italia_get_login_url( 'out' ) ), '' ] ],
'sp_org_name' => spid_option( 'sp_org_name' ),
'sp_org_display_name' => spid_option( 'sp_org_display_name' ),
'sp_contact_ipa_code' => spid_option( 'sp_contact_ipa_code' ),
Expand Down

0 comments on commit 8792ba4

Please sign in to comment.