Changes in lib/oauth2.c [3bda2c2:ba654ec]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/oauth2.c
r3bda2c2 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 … … 103 103 struct oauth2_access_token_data *cb_data = req->data; 104 104 char *atoken = NULL, *rtoken = NULL; 105 c har *content_type;105 const char *content_type; 106 106 107 107 if( getenv( "BITLBEE_DEBUG" ) && req->reply_body ) … … 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 … … 132 146 133 147 cb_data->func( cb_data->data, atoken, rtoken ); 134 g_free( content_type );135 148 g_free( atoken ); 136 149 g_free( rtoken ); 137 150 g_free( cb_data ); 138 151 } 139 140 /* Super dumb. I absolutely refuse to use/add a complete json parser library141 (adding a new dependency to BitlBee for the first time in.. 6 years?) just142 to parse 100 bytes of data. So I have to do my own parsing because OAuth2143 dropped support for XML. (GRRR!) This is very dumb and for example won't144 work for integer values, nor will it strip/handle backslashes. */145 static char *oauth2_json_dumb_get( const char *json, const char *key )146 {147 int is_key = 0; /* 1 == reading key, 0 == reading value */148 int found_key = 0;149 150 while( json && *json )151 {152 /* Grab strings and see if they're what we're looking for. */153 if( *json == '"' || *json == '\'' )154 {155 char q = *json;156 const char *str_start;157 json ++;158 str_start = json;159 160 while( *json )161 {162 /* \' and \" are not string terminators. */163 if( *json == '\\' && json[1] == q )164 json ++;165 /* But without a \ it is. */166 else if( *json == q )167 break;168 json ++;169 }170 if( *json == '\0' )171 return NULL;172 173 if( is_key && strncmp( str_start, key, strlen( key ) ) == 0 )174 {175 found_key = 1;176 }177 else if( !is_key && found_key )178 {179 char *ret = g_memdup( str_start, json - str_start + 1 );180 ret[json-str_start] = '\0';181 return ret;182 }183 184 }185 else if( *json == '{' || *json == ',' )186 {187 found_key = 0;188 is_key = 1;189 }190 else if( *json == ':' )191 is_key = 0;192 193 json ++;194 }195 196 return NULL;197 }
Note: See TracChangeset
for help on using the changeset viewer.