Changeset ba654ec


Ignore:
Timestamp:
2012-10-28T23:36:55Z (11 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Branches:
master
Children:
3b4a22a
Parents:
696dc9e
Message:

Use the new JSON code for OAuth2 processing.

Starting to see the use of convenience functions to dig through many layers
of structs..

File:
1 edited

Legend:

Unmodified
Added
Removed
  • lib/oauth2.c

    r696dc9e rba654ec  
    44*  Simple OAuth client (consumer) implementation.                           *
    55*                                                                           *
    6 *  Copyright 2010-2011 Wilmer van der Gaast <wilmer@gaast.net>              *
     6*  Copyright 2010-2012 Wilmer van der Gaast <wilmer@gaast.net>              *
    77*                                                                           *
    88*  This program is free software; you can redistribute it and/or modify     *
     
    2626#include "oauth2.h"
    2727#include "oauth.h"
     28#include "json.h"
    2829#include "url.h"
    2930
     
    4445};
    4546
    46 static char *oauth2_json_dumb_get( const char *json, const char *key );
    4747static void oauth2_access_token_done( struct http_request *req );
    4848
     
    115115        else if( content_type && strstr( content_type, "application/json" ) )
    116116        {
    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 );
    119133        }
    120134        else
     
    136150        g_free( cb_data );
    137151}
    138 
    139 /* Super dumb. I absolutely refuse to use/add a complete json parser library
    140    (adding a new dependency to BitlBee for the first time in.. 6 years?) just
    141    to parse 100 bytes of data. So I have to do my own parsing because OAuth2
    142    dropped support for XML. (GRRR!) This is very dumb and for example won't
    143    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.