Twitter with OAuth Example.\\ see also http://www.soramimi.jp/twicpp/index.html

Dependencies:   mbed HTTPClient NTPClient_NetServices EthernetNetIf

Embed: (wiki syntax)

« Back to documentation index

oauth.h File Reference

oauth.h File Reference

OAuth.net implementation in POSIX-C. More...

Go to the source code of this file.

Enumerations

enum  OAuthMethod { OA_HMAC = 0, OA_RSA, OA_PLAINTEXT }
 

signature method to used for signing the request.

More...

Functions

std::string oauth_encode_base64 (const unsigned char *src, int size)
 Base64 encode and return size data in 'src'.
std::string oauth_decode_base64 (const char *src)
 Decode the base64 encoded string 'src' into the memory pointed to by 'dest'.
std::string oauth_url_escape (const char *string)
 Escape 'string' according to RFC3986 and http://oauth.net/core/1.0/#encoding_parameters.
std::string oauth_url_unescape (const char *string)
 Parse RFC3986 encoded 'string' back to unescaped version.
std::string oauth_sign_hmac_sha1 (const char *m, const char *k)
 returns base64 encoded HMAC-SHA1 signature for given message and key.
std::string oauth_sign_hmac_sha1_raw (const char *m, const size_t ml, const char *k, const size_t kl)
 same as oauth_sign_hmac_sha1 but allows to specify length of message and key (in case they contain null chars).
std::string oauth_sign_plaintext (const char *m, const char *k)
 returns plaintext signature for the given key.
int oauth_verify_rsa_sha1 (const char *m, const char *c, const char *s)
 returns RSA-SHA1 signature for given data.
std::string oauth_catenc (int len,...)
 url-escape strings and concatenate with '&' separator.
void oauth_split_url_parameters (const char *url, char ***argv)
 splits the given url into a parameter array.
void oauth_split_post_paramters (const char *url, char ***argv, short qesc)
 splits the given url into a parameter array.
std::string oauth_serialize_url (std::vector< std::string > const &argv, int start)
 build a url query string from an array.
std::string oauth_serialize_url_sep (std::vector< std::string > const &argv, int start, char const *sep, int mod)
 encode query parameters from an array.
std::string oauth_serialize_url_parameters (std::vector< std::string > const &argv)
 build a query parameter string from an array.
std::string oauth_gen_nonce ()
 generate a random string between 15 and 32 chars length and return a pointer to it.
int oauth_cmpstringp (const void *p1, const void *p2)
 string compare function for oauth parameters.
bool oauth_param_exists (std::vector< std::string > const &argv, char const *key)
 search array for parameter key.
void oauth_free_array (int *argcp, std::vector< std::string > *argvp)
 free array args
std::string oauth_sign_url2 (const char *url, std::string *postargs, OAuthMethod method, const char *http_method, const char *c_key, const char *c_secret, const char *t_key, const char *t_secret)
 calculate OAuth-signature for a given HTTP request URL, parameters and oauth-tokens.
std::string oauth_sign_url (const char *url, std::string *postargs, OAuthMethod method, const char *c_key, const char *c_secret, const char *t_key, const char *t_secret) attribute_deprecated
void oauth_sign_array2_process (std::vector< std::string > *argvp, std::string *postargs, OAuthMethod method, const char *http_method, const char *c_key, const char *c_secret, const char *t_key, const char *t_secret)
 the back-end behind by /ref oauth_sign_array2.
std::string oauth_sign_array2 (std::vector< std::string > *argvp, std::string *postargs, OAuthMethod method, const char *http_method, const char *c_key, const char *c_secret, const char *t_key, const char *t_secret)
 same as /ref oauth_sign_url with the url already split into parameter array
char * oauth_sign_array (std::vector< std::string > *argvp, char **postargs, OAuthMethod method, const char *c_key, const char *c_secret, const char *t_key, const char *t_secret) attribute_deprecated
std::string oauth_http_post (const char *u, const char *p)
 do a HTTP POST request, wait for it to finish and return the content of the reply.

Detailed Description

OAuth.net implementation in POSIX-C.

Author:
Robin Gareus <robin@gareus.org>

Copyright 2007-2010 Robin Gareus <robin@gareus.org>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Definition in file oauth.h.


Enumeration Type Documentation

signature method to used for signing the request.

Enumerator:
OA_HMAC 

use HMAC-SHA1 request signing method

OA_RSA 

use RSA signature

OA_PLAINTEXT 

use plain text signature (for testing only)

Definition at line 66 of file oauth.h.


Function Documentation

std::string oauth_catenc ( int  len,
  ... 
)

url-escape strings and concatenate with '&' separator.

The number of strings to be concatenated must be given as first argument. all arguments thereafter must be of type (char *)

Parameters:
lenthe number of arguments to follow this parameter
Returns:
pointer to memory holding the concatenated strings - needs to be free(d) by the caller. or NULL in case we ran out of memory.

url-escape strings and concatenate with '&' separator.

The number of strings to be concatenated must be given as first argument. all arguments thereafter must be of type (char *)

Parameters:
lenthe number of arguments to follow this parameter
...string to escape and added (may be NULL)
Returns:
pointer to memory holding the concatenated strings - needs to be free(d) by the caller. or NULL in case we ran out of memory.

Definition at line 282 of file oauth.cpp.

int oauth_cmpstringp ( const void *  p1,
const void *  p2 
)

string compare function for oauth parameters.

used with qsort. needed to normalize request parameters. see http://oauth.net/core/1.0/#anchor14

Definition at line 482 of file oauth.cpp.

std::string oauth_decode_base64 ( const char *  src )

Decode the base64 encoded string 'src' into the memory pointed to by 'dest'.

Parameters:
destPointer to memory for holding the decoded string. Must be large enough to receive the decoded string.
srcA base64 encoded string.
Returns:
the length of the decoded string if decode succeeded otherwise 0.

Definition at line 136 of file oauth.cpp.

std::string oauth_encode_base64 ( const unsigned char *  src,
int  size 
)

Base64 encode and return size data in 'src'.

The caller must free the returned string.

Parameters:
sizeThe size of the data in src
srcThe data to be base64 encode
Returns:
encoded string otherwise NULL

Definition at line 89 of file oauth.cpp.

void oauth_free_array ( int *  argcp,
std::vector< std::string > *  argvp 
)

free array args

Parameters:
argcppointer to array length int
argvppointer to array values to be free()d
std::string oauth_gen_nonce (  )

generate a random string between 15 and 32 chars length and return a pointer to it.

The value needs to be freed by the caller

Returns:
zero terminated random string.

Definition at line 460 of file oauth.cpp.

std::string oauth_http_post ( const char *  u,
const char *  p 
)

do a HTTP POST request, wait for it to finish and return the content of the reply.

(requires libcurl or a command-line HTTP client)

If compiled without libcurl this function calls a command-line executable defined in the environment variable OAUTH_HTTP_CMD - it defaults to curl -sA 'liboauth-agent/0.1' -d '%p' '%u' where %p is replaced with the postargs and %u is replaced with the URL.

bash & wget example: export OAUTH_HTTP_CMD="wget -q -U 'liboauth-agent/0.1' --post-data='%p' '%u' "

NOTE: This function uses the curl's default HTTP-POST Content-Type: application/x-www-form-urlencoded which is the only option allowed by oauth core 1.0 spec. Experimental code can use the Environment variable to transmit custom HTTP headers or parameters.

WARNING: this is a tentative function. it's convenient and handy for testing or developing OAuth code. But don't rely on this function to become a stable part of this API. It does not do much error checking for one thing..

Parameters:
uurl to query
ppostargs to send along with the HTTP request.
Returns:
replied content from HTTP server. needs to be freed by caller.

(requires libcurl or a command-line HTTP client)

more documentation in oauth.h

Parameters:
uurl to query
ppostargs to send along with the HTTP request.
Returns:
In case of an error NULL is returned; otherwise a pointer to the replied content from HTTP server. latter needs to be freed by caller.

Definition at line 52 of file oauth_http.cpp.

bool oauth_param_exists ( std::vector< std::string > const &  argv,
char const *  key 
)

search array for parameter key.

Parameters:
argvlength of array to search
argcparameter array to search
keykey of parameter to check.
Returns:
FALSE (0) if array does not contain a parameter with given key, TRUE (1) otherwise.

Definition at line 512 of file oauth.cpp.

std::string oauth_serialize_url ( std::vector< std::string > const &  argv,
int  start 
)

build a url query string from an array.

Parameters:
argcthe total number of elements in the array
startelement in the array at which to start concatenating.
argvparameter-array to concatenate.
Returns:
url string needs to be freed by the caller.

Definition at line 378 of file oauth.cpp.

std::string oauth_serialize_url_parameters ( std::vector< std::string > const &  argv )

build a query parameter string from an array.

This function is a shortcut for oauth_serialize_url (argc, 1, argv). It strips the leading host/path, which is usually the first element when using oauth_split_url_parameters on an URL.

Parameters:
argcthe total number of elements in the array
argvparameter-array to concatenate.
Returns:
url string needs to be freed by the caller.

Definition at line 448 of file oauth.cpp.

std::string oauth_serialize_url_sep ( std::vector< std::string > const &  argv,
int  start,
char const *  sep,
int  mod 
)

encode query parameters from an array.

Parameters:
argcthe total number of elements in the array
startelement in the array at which to start concatenating.
argvparameter-array to concatenate.
sepseparator for parameters (usually "&")
mod- bitwise modifiers: 1: skip all values that start with "oauth_" 2: skip all values that don't start with "oauth_" 4: double quotation marks are added around values (use with sep ", " for HTTP Authorization header).
Returns:
url string needs to be freed by the caller.
Parameters:
argcthe total number of elements in the array
startelement in the array at which to start concatenating.
argvparameter-array to concatenate.
sepseparator for parameters (usually "&")
mod- bitwise modifiers: 1: skip all values that start with "oauth_" 2: skip all values that don't start with "oauth_" 4: add double quotation marks around values (use with sep=", " to generate HTTP Authorization header).
Returns:
url string needs to be freed by the caller.

Definition at line 396 of file oauth.cpp.

char* oauth_sign_array ( std::vector< std::string > *  argvp,
char **  postargs,
OAuthMethod  method,
const char *  c_key,
const char *  c_secret,
const char *  t_key,
const char *  t_secret 
)
std::string oauth_sign_array2 ( std::vector< std::string > *  argvp,
std::string *  postargs,
OAuthMethod  method,
const char *  http_method,
const char *  c_key,
const char *  c_secret,
const char *  t_key,
const char *  t_secret 
)

same as /ref oauth_sign_url with the url already split into parameter array

Parameters:
argcppointer to array length int
argvppointer to array values (argv[0]="http://example.org:80/" argv[1]="first=QueryParamater" .. the array is modified: fi. oauth_ parameters are added) These arrays can be generated with /ref oauth_split_url_parameters or /ref oauth_split_post_paramters.
postargsThis parameter points to an area where the return value is stored. If 'postargs' is NULL, no value is stored.
methodspecify the signature method to use. It is of type OAuthMethod and most likely OA_HMAC.
http_methodThe HTTP request method to use (ie "GET", "PUT",..) If NULL is given as 'http_method' this defaults to "GET" when 'postargs' is also NULL and when postargs is not NULL "POST" is used.
c_keyconsumer key
c_secretconsumer secret
t_keytoken key
t_secrettoken secret
Returns:
the signed url or NULL if an error occurred.

Definition at line 694 of file oauth.cpp.

void oauth_sign_array2_process ( std::vector< std::string > *  argvp,
std::string *  postargs,
OAuthMethod  method,
const char *  http_method,
const char *  c_key,
const char *  c_secret,
const char *  t_key,
const char *  t_secret 
)

the back-end behind by /ref oauth_sign_array2.

however it does not serialize the signed URL again. The user needs to call /ref oauth_serialize_url (oA) and /ref oauth_free_array to do so.

This allows to split parts of the URL to be used for OAuth HTTP Authorization header: see http://oauth.net/core/1.0a/#consumer_req_param the oauthtest2 example code does so.

Parameters:
argcppointer to array length int
argvppointer to array values (argv[0]="http://example.org:80/" argv[1]="first=QueryParamater" .. the array is modified: fi. oauth_ parameters are added) These arrays can be generated with /ref oauth_split_url_parameters or /ref oauth_split_post_paramters.
postargsThis parameter points to an area where the return value is stored. If 'postargs' is NULL, no value is stored.
methodspecify the signature method to use. It is of type OAuthMethod and most likely OA_HMAC.
http_methodThe HTTP request method to use (ie "GET", "PUT",..) If NULL is given as 'http_method' this defaults to "GET" when 'postargs' is also NULL and when postargs is not NULL "POST" is used.
c_keyconsumer key
c_secretconsumer secret
t_keytoken key
t_secrettoken secret
Returns:
void

Definition at line 632 of file oauth.cpp.

std::string oauth_sign_hmac_sha1 ( const char *  m,
const char *  k 
)

returns base64 encoded HMAC-SHA1 signature for given message and key.

both data and key need to be urlencoded.

the returned string needs to be freed by the caller

Parameters:
mmessage to be signed
kkey used for signing
Returns:
signature string.

Definition at line 45 of file hash.cpp.

std::string oauth_sign_hmac_sha1_raw ( const char *  m,
const size_t  ml,
const char *  k,
const size_t  kl 
)

same as oauth_sign_hmac_sha1 but allows to specify length of message and key (in case they contain null chars).

Parameters:
mmessage to be signed
mllength of message
kkey used for signing
kllength of key
Returns:
signature string.

Definition at line 50 of file hash.cpp.

std::string oauth_sign_plaintext ( const char *  m,
const char *  k 
)

returns plaintext signature for the given key.

the returned string needs to be freed by the caller

Parameters:
mmessage to be signed
kkey used for signing
Returns:
signature string

Definition at line 264 of file oauth.cpp.

std::string oauth_sign_url ( const char *  url,
std::string *  postargs,
OAuthMethod  method,
const char *  c_key,
const char *  c_secret,
const char *  t_key,
const char *  t_secret 
)

Definition at line 574 of file oauth.cpp.

std::string oauth_sign_url2 ( const char *  url,
std::string *  postargs,
OAuthMethod  method,
const char *  http_method,
const char *  c_key,
const char *  c_secret,
const char *  t_key,
const char *  t_secret 
)

calculate OAuth-signature for a given HTTP request URL, parameters and oauth-tokens.

if 'postargs' is NULL a "GET" request is signed and the signed URL is returned. Else this fn will modify 'postargs' to point to memory that contains the signed POST-variables and returns the base URL.

both, the return value and (if given) 'postargs' need to be freed by the caller.

Parameters:
urlThe request URL to be signed. append all GET or POST query-parameters separated by either '?' or '&' to this parameter.
postargsThis parameter points to an area where the return value is stored. If 'postargs' is NULL, no value is stored.
methodspecify the signature method to use. It is of type OAuthMethod and most likely OA_HMAC.
http_methodThe HTTP request method to use (ie "GET", "PUT",..) If NULL is given as 'http_method' this defaults to "GET" when 'postargs' is also NULL and when postargs is not NULL "POST" is used.
c_keyconsumer key
c_secretconsumer secret
t_keytoken key
t_secrettoken secret
Returns:
the signed url or NULL if an error occurred.

Definition at line 587 of file oauth.cpp.

void oauth_split_post_paramters ( const char *  url,
char ***  argv,
short  qesc 
)

splits the given url into a parameter array.

(see oauth_serialize_url and oauth_serialize_url_parameters for the reverse)

Parameters:
urlthe url or query-string to parse.
argvpointer to a (char *) array where the results are stored. The array is re-allocated to match the number of parameters and each parameter-string is allocated with strdup. - The memory needs to be freed by the caller.
qescuse query parameter escape (vs post-param-escape) - if set to 1 all '+' are treated as spaces ' '
Returns:
number of parameter(s) in array.
void oauth_split_url_parameters ( const char *  url,
char ***  argv 
)

splits the given url into a parameter array.

(see oauth_serialize_url and oauth_serialize_url_parameters for the reverse) (see oauth_split_post_paramters for a more generic version)

Parameters:
urlthe url or query-string to parse; may be NULL
argvpointer to a (char *) array where the results are stored. The array is re-allocated to match the number of parameters and each parameter-string is allocated with strdup. - The memory needs to be freed by the caller.
Returns:
number of parameter(s) in array.
std::string oauth_url_escape ( const char *  string )

Escape 'string' according to RFC3986 and http://oauth.net/core/1.0/#encoding_parameters.

Parameters:
stringThe data to be encoded
Returns:
encoded string otherwise NULL The caller must free the returned string.

Definition at line 184 of file oauth.cpp.

std::string oauth_url_unescape ( const char *  string )

Parse RFC3986 encoded 'string' back to unescaped version.

Parameters:
stringThe data to be unescaped
olenunless NULL the length of the returned string is stored there.
Returns:
decoded string or NULL The caller must free the returned string.

Definition at line 225 of file oauth.cpp.

int oauth_verify_rsa_sha1 ( const char *  m,
const char *  c,
const char *  s 
)

returns RSA-SHA1 signature for given data.

the returned signature needs to be freed by the caller.

Parameters:
mmessage to be signed
kprivate-key PKCS and Base64-encoded
Returns:
base64 encoded signature string. verify RSA-SHA1 signature.

returns the output of EVP_VerifyFinal() for a given message, cert/pubkey and signature.

Parameters:
mmessage to be verified
cpublic-key or x509 certificate
sbase64 encoded signature
Returns:
1 for a correct signature, 0 for failure and -1 if some other error occurred