Changeset 5f40da7 for lib/oauth.c
- Timestamp:
- 2011-12-26T10:51:19Z (13 years ago)
- Branches:
- master
- Children:
- 199fea6
- Parents:
- 96f954d (diff), 644b808 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/oauth.c
r96f954d r5f40da7 38 38 const char *params, struct oauth_info *oi ) 39 39 { 40 sha1_state_t sha1;41 40 uint8_t hash[sha1_hash_size]; 42 uint8_t key[HMAC_BLOCK_SIZE+1]; 41 GString *payload = g_string_new( "" ); 42 char *key; 43 43 char *s; 44 int i; 45 46 /* Create K. If our current key is >64 chars we have to hash it, 47 otherwise just pad. */ 48 memset( key, 0, HMAC_BLOCK_SIZE ); 49 i = strlen( oi->sp->consumer_secret ) + 1 + ( oi->token_secret ? strlen( oi->token_secret ) : 0 ); 50 if( i > HMAC_BLOCK_SIZE ) 51 { 52 sha1_init( &sha1 ); 53 sha1_append( &sha1, (uint8_t*) oi->sp->consumer_secret, strlen( oi->sp->consumer_secret ) ); 54 sha1_append( &sha1, (uint8_t*) "&", 1 ); 55 if( oi->token_secret ) 56 sha1_append( &sha1, (uint8_t*) oi->token_secret, strlen( oi->token_secret ) ); 57 sha1_finish( &sha1, key ); 58 } 59 else 60 { 61 g_snprintf( (gchar*) key, HMAC_BLOCK_SIZE + 1, "%s&%s", 62 oi->sp->consumer_secret, oi->token_secret ? oi->token_secret : "" ); 63 } 64 65 /* Inner part: H(K XOR 0x36, text) */ 66 sha1_init( &sha1 ); 67 68 for( i = 0; i < HMAC_BLOCK_SIZE; i ++ ) 69 key[i] ^= 0x36; 70 sha1_append( &sha1, key, HMAC_BLOCK_SIZE ); 71 72 /* OAuth: text = method&url¶ms, all http_encoded. */ 73 sha1_append( &sha1, (const uint8_t*) method, strlen( method ) ); 74 sha1_append( &sha1, (const uint8_t*) "&", 1 ); 44 45 key = g_strdup_printf( "%s&%s", oi->sp->consumer_secret, oi->token_secret ? oi->token_secret : "" ); 46 47 g_string_append_printf( payload, "%s&", method ); 75 48 76 49 s = g_new0( char, strlen( url ) * 3 + 1 ); 77 50 strcpy( s, url ); 78 51 http_encode( s ); 79 sha1_append( &sha1, (const uint8_t*) s, strlen( s ) ); 80 sha1_append( &sha1, (const uint8_t*) "&", 1 ); 52 g_string_append_printf( payload, "%s&", s ); 81 53 g_free( s ); 82 54 … … 84 56 strcpy( s, params ); 85 57 http_encode( s ); 86 sha1_append( &sha1, (const uint8_t*) s, strlen( s ) ); 87 g_free( s ); 88 89 sha1_finish( &sha1, hash ); 90 91 /* Final result: H(K XOR 0x5C, inner stuff) */ 92 sha1_init( &sha1 ); 93 for( i = 0; i < HMAC_BLOCK_SIZE; i ++ ) 94 key[i] ^= 0x36 ^ 0x5c; 95 sha1_append( &sha1, key, HMAC_BLOCK_SIZE ); 96 sha1_append( &sha1, hash, sha1_hash_size ); 97 sha1_finish( &sha1, hash ); 58 g_string_append( payload, s ); 59 g_free( s ); 60 61 sha1_hmac( key, 0, payload->str, 0, hash ); 62 63 g_free( key ); 64 g_string_free( payload, TRUE ); 98 65 99 66 /* base64_encode + HTTP escape it (both consumers … … 122 89 char *item; 123 90 91 if( !key || !value ) 92 return; 93 124 94 item = g_strdup_printf( "%s=%s", key, value ); 125 95 *params = g_slist_insert_sorted( *params, item, (GCompareFunc) strcmp ); … … 130 100 int key_len = strlen( key ); 131 101 GSList *l, *n; 102 103 if( params == NULL ) 104 return; 132 105 133 106 for( l = *params; l; l = n ) … … 155 128 GSList *l; 156 129 130 if( params == NULL ) 131 return NULL; 132 157 133 for( l = *params; l; l = l->next ) 158 134 { … … 165 141 } 166 142 167 staticvoid oauth_params_parse( GSList **params, char *in )143 void oauth_params_parse( GSList **params, char *in ) 168 144 { 169 145 char *amp, *eq, *s; … … 333 309 oauth_params_parse( ¶ms, req->reply_body ); 334 310 st->request_token = g_strdup( oauth_params_get( ¶ms, "oauth_token" ) ); 311 st->token_secret = g_strdup( oauth_params_get( ¶ms, "oauth_token_secret" ) ); 335 312 oauth_params_free( ¶ms ); 336 313 } … … 362 339 oauth_params_parse( &st->params, req->reply_body ); 363 340 st->token = g_strdup( oauth_params_get( &st->params, "oauth_token" ) ); 341 g_free( st->token_secret ); 364 342 st->token_secret = g_strdup( oauth_params_get( &st->params, "oauth_token_secret" ) ); 365 343 }
Note: See TracChangeset
for help on using the changeset viewer.