Changeset 6834 for sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-qbo/class-wordcamp-qbo-oauth-client.php
- Timestamp:
- 03/07/2018 01:45:42 AM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-qbo/class-wordcamp-qbo-oauth-client.php
r2325 r6834 3 3 * WordCamp QBO Oauth Client 4 4 * 5 * Note: This is NOT a general-purpose OAuth client, it is only suitable5 * SECURITY WARNING: This is NOT a general-purpose OAuth client, it is only suitable 6 6 * for the WordCamp QBO plugin. 7 7 */ 8 8 class WordCamp_QBO_OAuth_Client { 9 10 11 12 9 private $consumer_key; 10 private $consumer_secret; 11 private $oauth_token; 12 private $oauth_token_secret; 13 13 14 15 * @param string $consumer_keyThe OAuth consumer key16 17 18 19 $this->consumer_key= $consumer_key;20 21 14 /** 15 * @param string $consumer_key The OAuth consumer key 16 * @param string $consumer_secret The secret 17 */ 18 public function __construct( $consumer_key, $consumer_secret ) { 19 $this->consumer_key = $consumer_key; 20 $this->consumer_secret = $consumer_secret; 21 } 22 22 23 24 25 26 * @param string $oauth_tokenAn OAuth token.27 28 29 30 $this->oauth_token= $oauth_token;31 32 23 /** 24 * Set current OAuth token 25 * 26 * @param string $oauth_token An OAuth token. 27 * @param string $oauth_token_secret The OAuth token secret. 28 */ 29 public function set_token( $oauth_token, $oauth_token_secret ) { 30 $this->oauth_token = $oauth_token; 31 $this->oauth_token_secret = $oauth_token_secret; 32 } 33 33 34 35 36 37 38 39 * @return arrayAn array with the tokens.40 41 42 43 44 34 /** 35 * Get a request token. 36 * 37 * @param string $callback_url The URL to which a successful authentication will return. 38 * 39 * @return array|WP_Error An array with the tokens. 40 */ 41 public function get_request_token( $request_url, $callback_url ) { 42 $args = array_merge( $this->_get_default_args(), array( 43 'oauth_callback' => $callback_url, 44 ) ); 45 45 46 47 $args= array_map( 'rawurlencode', $args );46 $args['oauth_signature'] = $this->_get_signature( 'POST', $request_url, $args ); 47 $args = array_map( 'rawurlencode', $args ); 48 48 49 $response = wp_remote_post( add_query_arg( $args, $request_url ) ); 50 if ( is_wp_error( $response ) ) 51 return $response; 49 $response = wp_remote_post( add_query_arg( $args, $request_url ) ); 52 50 53 if ( wp_remote_retrieve_response_code( $response ) != 200 ) 54 return new WP_Error( 'error', 'Could not get OAuth request token.' ); 51 if ( is_wp_error( $response ) ) { 52 return $response; 53 } 55 54 56 $result = wp_parse_args( wp_remote_retrieve_body( $response ), array( 57 'oauth_token' => '', 58 'oauth_token_secret' => '', 59 'oauth_callback_confirmed' => '', 60 ) ); 55 if ( wp_remote_retrieve_response_code( $response ) != 200 ) { 56 return new WP_Error( 'error', 'Could not get OAuth request token.' ); 57 } 61 58 62 return $result; 63 } 59 $result = wp_parse_args( wp_remote_retrieve_body( $response ), array( 60 'oauth_token' => '', 61 'oauth_token_secret' => '', 62 'oauth_callback_confirmed' => '', 63 ) ); 64 64 65 /** 66 * Get an OAuth access token. 67 * 68 * @param string $verifier A verifier token from the authentication flow. 69 * 70 * @return array The access token. 71 */ 72 public function get_access_token( $request_url, $verifier ) { 73 $args = array_merge( $this->_get_default_args(), array( 74 'oauth_verifier' => $verifier, 75 'oauth_token' => $this->oauth_token, 76 ) ); 65 return $result; 66 } 77 67 78 $args['oauth_signature'] = $this->_get_signature( 'POST', $request_url, $args ); 79 $args = array_map( 'rawurlencode', $args ); 68 /** 69 * Get an OAuth access token. 70 * 71 * @param string $verifier A verifier token from the authentication flow. 72 * 73 * @return array|WP_Error The access token. 74 */ 75 public function get_access_token( $request_url, $verifier ) { 76 $args = array_merge( $this->_get_default_args(), array( 77 'oauth_verifier' => $verifier, 78 'oauth_token' => $this->oauth_token, 79 ) ); 80 80 81 $response = wp_remote_post( add_query_arg( $args, $request_url ) ); 81 $args['oauth_signature'] = $this->_get_signature( 'POST', $request_url, $args ); 82 $args = array_map( 'rawurlencode', $args ); 82 83 83 if ( is_wp_error( $response ) ) 84 return $response; 84 $response = wp_remote_post( add_query_arg( $args, $request_url ) ); 85 85 86 if ( wp_remote_retrieve_response_code( $response ) != 200 ) 87 return new WP_Error( 'error', 'Could not get OAuth access token.' ); 86 if ( is_wp_error( $response ) ) { 87 return $response; 88 } 88 89 89 $result = wp_parse_args( wp_remote_retrieve_body( $response ), array( 90 'oauth_token' => '', 91 'oauth_token_secret' => '', 92 ) ); 90 if ( wp_remote_retrieve_response_code( $response ) != 200 ) { 91 return new WP_Error( 'error', 'Could not get OAuth access token.' ); 92 } 93 93 94 return $result; 95 } 94 $result = wp_parse_args( wp_remote_retrieve_body( $response ), array( 95 'oauth_token' => '', 96 'oauth_token_secret' => '', 97 ) ); 96 98 97 /** 98 * Get a string suitable for the Authorization header. 99 * 100 * @see http://oauth.net/core/1.0a/#auth_header 101 * 102 * @param string $method The request method. 103 * @param string $request_url The request URL (without query) 104 * @param array|string $request_args Any additional query/body args. 105 * 106 * @return string An OAuth string ready for the Authorization header. 107 */ 108 public function get_oauth_header( $method, $request_url, $request_args = array() ) { 109 $oauth_args = array_merge( $this->_get_default_args(), array( 110 'oauth_token' => $this->oauth_token, 111 ) ); 99 return $result; 100 } 112 101 113 $all_args = $oauth_args; 114 if ( is_array( $request_args ) && ! empty( $request_args ) ) 115 $all_args = array_merge( $oauth_args, $request_args ); 102 /** 103 * Get a string suitable for the Authorization header. 104 * 105 * @see http://oauth.net/core/1.0a/#auth_header 106 * 107 * @param string $method The request method. 108 * @param string $request_url The request URL (without query) 109 * @param array|string $request_args Any additional query/body args. 110 * 111 * @return string An OAuth string ready for the Authorization header. 112 */ 113 public function get_oauth_header( $method, $request_url, $request_args = array() ) { 114 $oauth_args = array_merge( $this->_get_default_args(), array( 115 'oauth_token' => $this->oauth_token, 116 ) ); 116 117 117 $oauth_args['oauth_signature'] = $this->_get_signature( $method, $request_url, $all_args );118 $all_args = $oauth_args; 118 119 119 $header_parts = array(); 120 foreach ( $oauth_args as $key => $value ) 121 $header_parts[] = sprintf( '%s="%s"', rawurlencode( $key ), rawurlencode( $value ) ); 120 if ( is_array( $request_args ) && ! empty( $request_args ) ) { 121 $all_args = array_merge( $oauth_args, $request_args ); 122 } 122 123 123 $header = 'OAuth ' . implode( ',', $header_parts ); 124 return $header; 125 } 124 $oauth_args['oauth_signature'] = $this->_get_signature( $method, $request_url, $all_args ); 126 125 127 /** 128 * Get a default set of OAuth arguments. 129 * 130 * @return array Default OAuth arguments. 131 */ 132 private function _get_default_args() { 133 return array( 134 'oauth_nonce' => md5( wp_generate_password( 12 ) ), 135 'oauth_consumer_key' => $this->consumer_key, 136 'oauth_signature_method' => 'HMAC-SHA1', 137 'oauth_timestamp' => time(), 138 'oauth_version' => '1.0', 139 ); 140 } 126 $header_parts = array(); 141 127 142 /** 143 * Get an OAuth signature. 144 * 145 * @see http://oauth.net/core/1.0a/#signing_process 146 * 147 * @param string $method The request method, GET, POST, etc. 148 * @param string $url The request URL (without any query) 149 * @param array $args An optional array of query or body args. 150 * 151 * @return string A base64-encoded hmac-sha1 signature. 152 */ 153 private function _get_signature( $method, $url, $args ) { 154 ksort( $args ); 128 foreach ( $oauth_args as $key => $value ) { 129 $header_parts[] = sprintf( '%s="%s"', rawurlencode( $key ), rawurlencode( $value ) ); 130 } 155 131 156 // Don't sign a signature. 157 unset( $args['oauth_signature'] ); 132 $header = 'OAuth ' . implode( ',', $header_parts ); 133 return $header; 134 } 158 135 159 $parameter_string = ''; 160 foreach ( $args as $key => $value ) 161 $parameter_string .= sprintf( '&%s=%s', rawurlencode( $key ), rawurlencode( $value ) ); 136 /** 137 * Get a default set of OAuth arguments. 138 * 139 * @return array Default OAuth arguments. 140 */ 141 private function _get_default_args() { 142 return array( 143 'oauth_nonce' => md5( wp_generate_password( 12 ) ), 144 'oauth_consumer_key' => $this->consumer_key, 145 'oauth_signature_method' => 'HMAC-SHA1', 146 'oauth_timestamp' => time(), 147 'oauth_version' => '1.0', 148 ); 149 } 162 150 163 $parameter_string = trim( $parameter_string, '&' ); 164 $signature_base = strtoupper( $method ) . '&' . rawurlencode( $url ) . '&' . rawurlencode( $parameter_string ); 165 $signing_key = rawurlencode( $this->consumer_secret ) . '&' . rawurlencode( $this->oauth_token_secret ); 151 /** 152 * Get an OAuth signature. 153 * 154 * @see http://oauth.net/core/1.0a/#signing_process 155 * 156 * @param string $method The request method, GET, POST, etc. 157 * @param string $url The request URL (without any query) 158 * @param array $args An optional array of query or body args. 159 * 160 * @return string A base64-encoded hmac-sha1 signature. 161 */ 162 private function _get_signature( $method, $url, $args ) { 163 ksort( $args ); 166 164 167 return base64_encode( hash_hmac( 'sha1', $signature_base, $signing_key, true ) ); 168 } 165 // Don't sign a signature. 166 unset( $args['oauth_signature'] ); 167 168 $parameter_string = ''; 169 170 foreach ( $args as $key => $value ) { 171 $parameter_string .= sprintf( '&%s=%s', rawurlencode( $key ), rawurlencode( $value ) ); 172 } 173 174 $parameter_string = trim( $parameter_string, '&' ); 175 $signature_base = strtoupper( $method ) . '&' . rawurlencode( $url ) . '&' . rawurlencode( $parameter_string ); 176 $signing_key = rawurlencode( $this->consumer_secret ) . '&' . rawurlencode( $this->oauth_token_secret ); 177 178 return base64_encode( hash_hmac( 'sha1', $signature_base, $signing_key, true ) ); 179 } 169 180 }
Note: See TracChangeset
for help on using the changeset viewer.