- Timestamp:
- 2012-10-28T23:36:55Z (12 years ago)
- Branches:
- master
- Children:
- 3b4a22a
- Parents:
- 696dc9e
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/oauth2.c
r696dc9e rba654ec 4 4 * Simple OAuth client (consumer) implementation. * 5 5 * * 6 * Copyright 2010-201 1Wilmer van der Gaast <wilmer@gaast.net> *6 * Copyright 2010-2012 Wilmer van der Gaast <wilmer@gaast.net> * 7 7 * * 8 8 * This program is free software; you can redistribute it and/or modify * … … 26 26 #include "oauth2.h" 27 27 #include "oauth.h" 28 #include "json.h" 28 29 #include "url.h" 29 30 … … 44 45 }; 45 46 46 static char *oauth2_json_dumb_get( const char *json, const char *key );47 47 static void oauth2_access_token_done( struct http_request *req ); 48 48 … … 115 115 else if( content_type && strstr( content_type, "application/json" ) ) 116 116 { 117 atoken = oauth2_json_dumb_get( req->reply_body, "access_token" ); 118 rtoken = oauth2_json_dumb_get( req->reply_body, "refresh_token" ); 117 json_value *js = json_parse( req->reply_body ); 118 if( js && js->type == json_object ) 119 { 120 int i; 121 122 for( i = 0; i < js->u.object.length; i ++ ) 123 { 124 if( js->u.object.values[i].value->type != json_string ) 125 continue; 126 if( strcmp( js->u.object.values[i].name, "access_token" ) == 0 ) 127 atoken = g_strdup( js->u.object.values[i].value->u.string.ptr ); 128 if( strcmp( js->u.object.values[i].name, "refresh_token" ) == 0 ) 129 rtoken = g_strdup( js->u.object.values[i].value->u.string.ptr ); 130 } 131 } 132 json_value_free( js ); 119 133 } 120 134 else … … 136 150 g_free( cb_data ); 137 151 } 138 139 /* Super dumb. I absolutely refuse to use/add a complete json parser library140 (adding a new dependency to BitlBee for the first time in.. 6 years?) just141 to parse 100 bytes of data. So I have to do my own parsing because OAuth2142 dropped support for XML. (GRRR!) This is very dumb and for example won't143 work for integer values, nor will it strip/handle backslashes. */144 static char *oauth2_json_dumb_get( const char *json, const char *key )145 {146 int is_key = 0; /* 1 == reading key, 0 == reading value */147 int found_key = 0;148 149 while( json && *json )150 {151 /* Grab strings and see if they're what we're looking for. */152 if( *json == '"' || *json == '\'' )153 {154 char q = *json;155 const char *str_start;156 json ++;157 str_start = json;158 159 while( *json )160 {161 /* \' and \" are not string terminators. */162 if( *json == '\\' && json[1] == q )163 json ++;164 /* But without a \ it is. */165 else if( *json == q )166 break;167 json ++;168 }169 if( *json == '\0' )170 return NULL;171 172 if( is_key && strncmp( str_start, key, strlen( key ) ) == 0 )173 {174 found_key = 1;175 }176 else if( !is_key && found_key )177 {178 char *ret = g_memdup( str_start, json - str_start + 1 );179 ret[json-str_start] = '\0';180 return ret;181 }182 183 }184 else if( *json == '{' || *json == ',' )185 {186 found_key = 0;187 is_key = 1;188 }189 else if( *json == ':' )190 is_key = 0;191 192 json ++;193 }194 195 return NULL;196 }
Note: See TracChangeset
for help on using the changeset viewer.