get_name() . '_request_ui', array( $this, 'request_ui' ) );
}
function get_display( Keyring_Access_Token $token ) {
$meta = $token->get_meta();
return $meta['username'];
}
function request_ui() {
// Common Header
echo '
';
screen_icon( 'ms-admin' );
echo '
' . __( 'Account Details', 'keyring' ) . '
';
// Handle errors
if ( isset( $_GET['error'] ) ) {
echo '
';
switch ( $_GET['error'] ) {
case '401':
echo '- ' . __( 'Your account details could not be confirmed, please try again.', 'keyring' ) . '
';
break;
case 'empty':
echo '- ' . __( 'Please make sure you enter a username and password.', 'keyring' ) . '
';
break;
}
echo '
';
}
// Even though it doesn't make too much sense, we support request tokens in HTTP Basic
// to ensure consistency with other services
$request_token = new Keyring_Request_Token(
$this->get_name(),
array(),
apply_filters(
'keyring_request_token_meta',
array(
'for' => isset( $_REQUEST['for'] ) ? (string) $_REQUEST['for'] : false
),
$this->get_name(),
array() // no token
)
);
$request_token = apply_filters( 'keyring_request_token', $request_token, $this );
$request_token_id = $this->store_token( $request_token );
Keyring_Util::debug( 'HTTP Basic Stored Request token ' . $request_token_id );
echo apply_filters( 'keyring_' . $this->get_name() . '_request_ui_intro', '' );
// Output basic form for collecting user/pass
echo '
' . sprintf( __( 'Enter your username and password for accessing %s:', 'keyring' ), $this->get_label() ) . '
';
echo '
';
echo '
';
?>get_name() ) ) {
Keyring::error( __( 'Invalid/missing verification nonce.', 'keyring' ) );
exit;
}
// Load up the request token that got us here and globalize it
if ( isset( $_REQUEST['state'] ) ) {
global $keyring_request_token;
$state = preg_replace( '/[^\x20-\x7E]/', '', $_GET['state'] );
$keyring_request_token = $this->store->get_token( array( 'id' => $state, 'type' => 'request' ) );
Keyring_Util::debug( 'HTTP Basic Loaded Request Token ' . $state );
Keyring_Util::debug( $keyring_request_token );
// Remove request token, don't need it any more.
$this->store->delete( array( 'id' => $state, 'type' => 'request' ) );
}
if ( !strlen( $_POST['username'] ) ) {
$url = Keyring_Util::admin_url(
$this->get_name(),
array(
'action' => 'request',
'error' => 'empty',
'kr_nonce' => wp_create_nonce( 'keyring-request' )
)
);
Keyring_Util::debug( $url );
wp_safe_redirect( $url );
exit;
}
// HTTP Basic does not use Keyring_Request_Tokens, since there's only one step
$token = new Keyring_Access_Token(
$this->get_name(),
base64_encode( $_POST['username'] . ':' . $_POST['password'] )
);
$this->set_token( $token );
$res = $this->request( $this->verify_url, array( 'method' => $this->verify_method ) );
// We will get a 401 if they entered an incorrect user/pass combo. ::request
// will then return a Keyring_Error
if ( Keyring_Util::is_error( $res ) ) {
$url = Keyring_Util::admin_url(
$this->get_name(),
array(
'action' => 'request',
'error' => '401',
'kr_nonce' => wp_create_nonce( 'keyring-request' )
)
);
Keyring_Util::debug( $url );
wp_safe_redirect( $url );
exit;
}
$meta = array_merge( array( 'username' => $_POST['username'] ), $this->build_token_meta( $token ) );
$access_token = new Keyring_Access_Token(
$this->get_name(),
$token,
$meta
);
$access_token = apply_filters( 'keyring_access_token', $access_token, array() );
// If we didn't get a 401, then we'll assume it's OK
$id = $this->store_token( $access_token );
$this->verified( $id, $keyring_request_token );
}
function request( $url, array $params = array() ) {
if ( $this->requires_token() && empty( $this->token ) )
return new Keyring_Error( 'keyring-request-error', __( 'No token' ) );
if ( $this->requires_token() )
$params['headers'] = array( 'Authorization' => 'Basic ' . $this->token );
$method = 'GET';
if ( isset( $params['method'] ) ) {
$method = strtoupper( $params['method'] );
unset( $params['method'] );
}
$raw_response = false;
if ( isset( $params['raw_response'] ) ) {
$raw_response = (bool) $params['raw_response'];
unset( $params['raw_response'] );
}
Keyring_Util::debug( "HTTP Basic $method $url" );
Keyring_Util::debug( $params );
switch ( strtoupper( $method ) ) {
case 'GET':
$res = wp_remote_get( $url, $params );
break;
case 'POST':
$res = wp_remote_post( $url, $params );
break;
default:
Keyring::error( __( 'Unsupported method specified for verify_token.', 'keyring' ) );
exit;
}
Keyring_Util::debug( $res );
$this->set_request_response_code( wp_remote_retrieve_response_code( $res ) );
if ( 200 == wp_remote_retrieve_response_code( $res ) || 201 == wp_remote_retrieve_response_code( $res ) ) {
if ( $raw_response )
return wp_remote_retrieve_body( $res );
else
return $this->parse_response( wp_remote_retrieve_body( $res ) );
} else {
return new Keyring_Error( 'keyring-request-error', $res );
}
}
}