moves.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Moves service definition for Keyring.
  4. * https://dev.moves-app.com/
  5. */
  6. class Keyring_Service_Moves extends Keyring_Service_OAuth2 {
  7. const NAME = 'moves';
  8. const LABEL = 'Moves';
  9. const SCOPE = 'activity location';
  10. function __construct() {
  11. parent::__construct();
  12. $this->place_types = array(
  13. 'unknown' => __( 'Unknown', 'keyring' ),
  14. 'home' => __( 'Home', 'keyring' ),
  15. 'work' => __( 'Work', 'keyring' ),
  16. 'school' => __( 'School', 'keyring' ),
  17. 'user' => __( 'Manually Named', 'keyring' ),
  18. 'foursquare' => __( 'Selected from foursquare', 'keyring' ),
  19. );
  20. $this->activity_types = array(
  21. 'wlk' => __( 'Walking', 'keyring' ),
  22. 'cyc' => __( 'Cycling', 'keyring' ),
  23. 'run' => __( 'Running', 'keyring' ),
  24. 'trp' => __( 'Transport', 'keyring' ),
  25. );
  26. // Enable "basic" UI for entering key/secret
  27. if ( ! KEYRING__HEADLESS_MODE ) {
  28. add_action( 'keyring_moves_manage_ui', array( $this, 'basic_ui' ) );
  29. add_filter( 'keyring_moves_basic_ui_intro', array( $this, 'basic_ui_intro' ) );
  30. }
  31. $this->set_endpoint( 'authorize', 'https://api.moves-app.com/oauth/v1/authorize', 'GET' );
  32. $this->set_endpoint( 'access_token', 'https://api.moves-app.com/oauth/v1/access_token', 'POST' );
  33. $this->set_endpoint( 'verify_token', 'https://api.moves-app.com/oauth/v1/tokeninfo', 'GET' );
  34. $this->set_endpoint( 'profile', 'https://api.moves-app.com/api/1.1/user/profile', 'GET' );
  35. $creds = $this->get_credentials();
  36. $this->app_id = $creds['app_id'];
  37. $this->key = $creds['key'];
  38. $this->secret = $creds['secret'];
  39. $this->consumer = new OAuthConsumer( $this->key, $this->secret, $this->callback_url );
  40. $this->signature_method = new OAuthSignatureMethod_HMAC_SHA1;
  41. // Moves requires an exact match on Redirect URI, which means we can't send any nonces
  42. $this->callback_url = remove_query_arg( array( 'nonce', 'kr_nonce' ), $this->callback_url );
  43. add_action( 'pre_keyring_moves_verify', array( $this, 'redirect_incoming_verify' ) );
  44. $this->authorization_header = 'Bearer';
  45. $this->authorization_parameter = false;
  46. add_filter( 'keyring_moves_request_token_params', array( $this, 'request_token_params' ) );
  47. }
  48. function basic_ui_intro() {
  49. echo '<p>' . sprintf( __( 'Head over and <a href="%s">create a new application</a> on Moves-app which you\'ll use to connect.', 'keyring' ), 'https://dev.moves-app.com/apps/new' ) . '</p>';
  50. echo '<p>' . sprintf( __( "Once it's created, click the <strong>Development</strong> tab. Your <strong>App ID</strong> and <strong>API Key</strong> are both shown on that page as <strong>Client ID</strong>. Enter your <strong>Client secret</strong> in the <strong>API Secret</strong> box. On that tab there is also a <strong>Redirect URI</strong> box, which you should set to <code>%s</code>.", 'keyring' ), Keyring_Util::admin_url( self::NAME, array( 'action' => 'verify' ) ) ) . '</p>';
  51. }
  52. function request_token_params( $params ) {
  53. $params['scope'] = apply_filters( 'keyring_moves_scope', self::SCOPE );
  54. return $params;
  55. }
  56. function redirect_incoming_verify( $request ) {
  57. if ( !isset( $request['kr_nonce'] ) ) {
  58. $kr_nonce = wp_create_nonce( 'keyring-verify' );
  59. $nonce = wp_create_nonce( 'keyring-verify-' . $this->get_name() );
  60. wp_safe_redirect(
  61. Keyring_Util::admin_url(
  62. $this->get_name(),
  63. array(
  64. 'action' => 'verify',
  65. 'kr_nonce' => $kr_nonce,
  66. 'nonce' => $nonce,
  67. 'state' => $request['state'],
  68. 'code' => $request['code'], // Auth code from successful response (maybe)
  69. )
  70. )
  71. );
  72. exit;
  73. }
  74. }
  75. function build_token_meta( $token ) {
  76. $meta = array(
  77. 'user_id' => $token['user_id'],
  78. 'refresh_token' => $token['refresh_token'],
  79. 'expires' => time() + $token['expires_in'],
  80. '_classname' => get_called_class(),
  81. );
  82. $this->set_token(
  83. new Keyring_Access_Token(
  84. $this->get_name(),
  85. $token['access_token'],
  86. array()
  87. )
  88. );
  89. $response = $this->request( $this->profile_url );
  90. if ( !Keyring_Util::is_error( $response ) ) {
  91. $meta['first_date'] = $response->profile->firstDate;
  92. }
  93. return apply_filters( 'keyring_access_token_meta', $meta, self::NAME, $token, array(), $this );
  94. }
  95. function get_display( Keyring_Access_Token $token ) {
  96. return $token->get_meta( 'user_id' );
  97. }
  98. }
  99. add_action( 'keyring_load_services', array( 'Keyring_Service_Moves', 'init' ) );