yahoo.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Yahoo service definition for Keyring. Clean implementation of OAuth1
  4. */
  5. class Keyring_Service_Yahoo extends Keyring_Service_OAuth1 {
  6. const NAME = 'yahoo';
  7. const LABEL = 'Yahoo! Updates';
  8. function __construct() {
  9. parent::__construct();
  10. // Enable "basic" UI for entering key/secret
  11. if ( ! KEYRING__HEADLESS_MODE ) {
  12. add_action( 'keyring_yahoo_manage_ui', array( $this, 'basic_ui' ) );
  13. add_filter( 'keyring_yahoo_basic_ui_intro', array( $this, 'basic_ui_intro' ) );
  14. }
  15. $this->set_endpoint( 'request_token', 'https://api.login.yahoo.com/oauth/v2/get_request_token', 'GET' );
  16. $this->set_endpoint( 'authorize', 'https://api.login.yahoo.com/oauth/v2/request_auth', 'GET' );
  17. $this->set_endpoint( 'access_token', 'https://api.login.yahoo.com/oauth/v2/get_token', 'POST' );
  18. $creds = $this->get_credentials();
  19. $this->app_id = $creds['app_id'];
  20. $this->key = $creds['key'];
  21. $this->secret = $creds['secret'];
  22. $this->consumer = new OAuthConsumer( $this->key, $this->secret, $this->callback_url );
  23. $this->signature_method = new OAuthSignatureMethod_HMAC_SHA1;
  24. }
  25. function basic_ui_intro() {
  26. echo '<p>' . sprintf( __( "To connect to Yahoo!, you need to <a href='https://developer.apps.yahoo.com/dashboard/createKey.html'>Create a new project</a>. Make sure you set the <strong>Access Scope</strong> to <strong>This app requires access to private user data</strong>. When you select that, you will be asked for an <strong>Application Domain</strong>, which should probably be set to <code>http://%s</code>. Which APIs you request access for will depend on how Keyring will be used on this site. Common ones will be <strong>Contacts</strong>, <strong>Social Directory</strong>, <strong>Status</strong>, and <strong>Updates</strong>.", 'keyring' ), $_SERVER['HTTP_HOST'] ) . '</p>';
  27. echo '<p>' . __( "Once you've created your project, copy and paste your <strong>Consumer key</strong> and <strong>Consumer secret</strong> (from under the <strong>Authentication Information: OAuth</strong> section of your app's details) into the boxes below. You don't need an App ID for Yahoo!.", 'keyring' ) . '</p>';
  28. }
  29. function parse_response( $response ) {
  30. return json_decode( $response );
  31. }
  32. function build_token_meta( $token ) {
  33. $expires = isset( $token['oauth_expires_in'] ) ? gmdate( 'Y-m-d H:i:s', time() + $token['oauth_expires_in'] ) : 0;
  34. $this->set_token(
  35. new Keyring_Access_Token(
  36. 'yahoo',
  37. new OAuthToken(
  38. $token['oauth_token'],
  39. $token['oauth_token_secret']
  40. )
  41. )
  42. );
  43. // Get user profile information
  44. $response = $this->request( "http://social.yahooapis.com/v1/user/{$token['xoauth_yahoo_guid']}/profile?format=json" );
  45. if ( Keyring_Util::is_error( $response ) ) {
  46. $meta = array();
  47. } else {
  48. $this->person = $response->profile;
  49. $meta = array(
  50. 'user_id' => $token['xoauth_yahoo_guid'],
  51. 'name' => $this->person->nickname,
  52. 'picture' => $this->person->image->imageUrl,
  53. );
  54. }
  55. return apply_filters( 'keyring_access_token_meta', $meta, 'yahoo', $token, $response, $this );
  56. }
  57. function get_display( Keyring_Access_Token$token ) {
  58. return $token->get_meta( 'name' );
  59. }
  60. function test_connection() {
  61. $this->maybe_refresh_token();
  62. $guid = $this->token->get_meta( 'external_id' );
  63. $res = $this->request( 'http://social.yahooapis.com/v1/user/' . $guid . '/profile?format=json' );
  64. if ( !Keyring_Util::is_error( $res ) )
  65. return true;
  66. return $res;
  67. }
  68. function maybe_refresh_token() {
  69. global $wpdb;
  70. if ( empty( $this->token->token ) || empty( $this->token->token->tokenExpires ) )
  71. return;
  72. if ( $this->token->token->tokenExpires && $this->token->token->tokenExpires < time() ) {
  73. $api_url = 'https://api.login.yahoo.com/oauth/v2/get_token';
  74. $api_url .= '?oauth_session_handle=' . $this->token->token->sessionHandle;
  75. $refresh = $this->request( $api_url, array(
  76. 'method' => 'GET',
  77. 'raw_response' => true,
  78. ) );
  79. if ( !Keyring_Util::is_error( $refresh ) ) {
  80. $token = $this->parse_access_token( $refresh );
  81. // Fake request token
  82. global $keyring_request_token;
  83. $keyring_request_token = new Keyring_Request_Token(
  84. $this->get_name(),
  85. array()
  86. );
  87. // Build (real) access token
  88. $access_token = new Keyring_Access_Token(
  89. $this->get_name(),
  90. new OAuthToken(
  91. $token['oauth_token'],
  92. $token['oauth_token_secret']
  93. ),
  94. $this->build_token_meta( $token ),
  95. $this->token->unique_id
  96. );
  97. // Store the updated access token
  98. $access_token = apply_filters( 'keyring_access_token', $access_token, $token );
  99. $id = $this->store->update( $access_token );
  100. // And switch to using it
  101. $this->set_token( $access_token );
  102. }
  103. }
  104. }
  105. }
  106. add_action( 'keyring_load_services', array( 'Keyring_Service_Yahoo', 'init' ) );