GithubHelp home page GithubHelp logo

liboauthcpp's Introduction

liboauthcpp

liboauthcpp is a pure C++ library for performing OAuth requests. It doesn't contain any networking code -- you provide for performing HTTP requests yourself, however you like -- instead focusing on performing OAuth-specific functionality and providing a nice interface for it. If you already have infrastructure for making HTTP requests and are looking to add OAuth support, liboauthcpp is for you.

liboauthcpp currently implements OAuth 1.0a (see http://tools.ietf.org/html/rfc5849).

Buildbot

Build Status

Requirements

You should only need:

  • CMake
  • A C++ compiler for your platform (e.g. g++, Microsoft Visual C++)

Compiling

The build process is simple:

cd liboauthcpp
cd build
cmake .
make            # or open Visual Studio and build the solution

If your own project uses CMake you can also include build/CMakeLists.txt directly into your project and reference the target "oauthcpp", a static library, in your project.

Percent (URL) Encoding

To get correct results, you need to pass your URL properly encoded to liboauthcpp. If you are not at all familiar, you should probably start by reading the URI Spec, especially Section 2. Alternatively, this article gives a more readable overview.

The basic idea is that there are 3 classes of characters: reserved, unreserved, and other. Reserved characters are special characters that are used in the URI syntax itself, e.g. ':' (after the scheme), '/' (the hierarchical path separator), and '?' (prefixing the query string). Unreserved characters are characters that are always safe to include unencoded, e.g. the alphanumerics. Other characters must always be encoded, mainly covering special characters like ' ', '<' or '>', and '{' or '}'.

The basic rule is that reserved characters must be encoded if they appear in any part of the URI when not being used as a separator. Unreserved characters are always safe. And the other characters they didn't know if they would be safe or not so they must always be encoded.

Unfortunately, the reserved set is a bit more complicated. They are broken down into 'general delimiters' and 'sub delimiters'. The ones already mentioned, like ':', can appear in many forms of URIs (say, http, ftp, about, gopher, mailto, etc. Those are called general delimiters. Others (e.g. '(', ')', '!', '$', '+', ',', '=', and more) are called subdelimiters because their use depends on the URI scheme. Worse, their use depends on the part of the URI. Depending on the particular URI scheme, these may or may not have to be encoded, and it might also depend on where they appear. (As an example, an '&' in an http URI isn't an issue if it appears in the path -- before the query string -- i.e. before a '?' appears. Worse, '=' can appear unencoded in the path, or in a query parameter value, but not in a query parameter key since it would be interpreted as the end of the key.)

Additionally, in many cases it is permitted to encode a character unnecessarily and the result is supposed to be the same. This means that it's possible to percent encode some URLs in multiple ways (e.g. encoding the unreserved set unnecessarily). It is possible, but not guaranteed, that if you pass exactly the same URI to liboauthcpp and the OAuth server, it will handle it regardless of the variant of encoding, so long as it is a valid encoding.

The short version: percent encoding a URL properly is non-trivial and you can even encode the same URL multiple ways, but has to be done correctly so that the OAuth signature can be computed. Sadly, "correctly" in this case really means "in whatever way the server your interacting with wants it encoded".

Internally, liboauthcpp needs to do another step of percent encoding, but the OAuth spec is very precise about how that works (none of these scheme-dependent issues). liboauth applies this percent encoding, but assumes that you have encoded your URLs properly. This assumption makes sense since the actual request is made separately, and the URI has to be specified in it, so you should already have a form which the server will accept.

However, in order to aid you, a very simple percent encoding API is exposed. It should help you encode URLs minimally and in a way that many services accept. In most cases you should use HttpPercentEncodePath(), HttpPercentEncodeQueryKey(), and HttpPercentEncodeQueryValue() to encode those parts of your http URL, then combine them and pass them to liboauthcpp for signing.

Thread Safety

liboauthcpp doesn't provide any thread safety guarantees. That said, there is very little shared state, and some classes (e.g. Consumer) are naturally immutable and therefore thread safe. Similarly, nearly the entire library uses no static/shared state, so as long as you create separate objects for separate threads, you should be safe.

The one exception is nonces: the Client class needs to generate a nonce for authorization. To do so, the random number generator needs to be seeded. We do this with the current time, but fast, repeated use of the Client class from different threads could result in the same nonce. To avoid requiring an entire thread library just for this one case, you can call Client::initialize() explicitly before using the Client from multiple threads. For single-threaded use, you are not required to call it.

Demos

There are two demos included in the demos/ directory, and they are built by default with the instructions above. In both, you enter key/secret information and it generates URLs for you to visit (in a browser) and copy data back into the program.

simple_auth should be executed first. It starts with only a consumer key and secret and performs 3-legged auth: you enter in consumer keys, it generates URLs to authenticate the user and generate access tokens. It requires 3 steps: request_token, authorize, and access_token (which correspond the URLs accessed). At the end of this process, you'll be provided an access key/secret pair which you can use to access actual resources.

simple_request actually does something useful now that your application is authorized. Enter your consumer key/secret and the access key/secret from simple_auth (or which you've generated elsewhere) and it will generate a URL you can use to access your home timeline in JSON format. It adds a parameter to ask for only 5 entries (demonstrating that signing works properly over additional query parameters). This is a one-step process -- it just gives you the URL and you get the results in your browser.

In both, the URLs accessed are specified at the top of the demo files. simple_auth requires URLs for request_token, authorize_url, and access_token. Some providers require additional parameters (notably an oauth_callback for Twitter, even if its out of band, or oob), which you can also specify in that location. simple_request only needs the URL of the resource being accessed (i.e. the URL for the home_timeline JSON data used by default in the demo), with optional parameters stored as a query string.

Both demos only use GET requests with query strings, but all HTTP methods (e.g. POST, PUT, DELETE) and approaches to sending parameters (e.g. HTTP headers, url-encoded body) should be supported in the API.

License

liboauthcpp is MIT licensed. See the LICENSE file for more details.

liboauthcpp is mostly taken from libtwitcurl (https://github.com/swatkat/twitcurl), which is similarly licensed. It mostly serves to isolate the OAuth code from libtwitcurl's Twitter and cURL specific code.

libtwitcurl also borrowed code from other projects: twitcurl uses HMAC_SHA1 from http://www.codeproject.com/KB/recipes/HMACSHA1class.aspx twitcurl uses base64 from http://www.adp-gmbh.ch/cpp/common/base64.html

liboauthcpp's People

Contributors

catboxparadox avatar danielrh avatar ewencp avatar hatkirby avatar joseexposito avatar jterrace avatar shonfrazier avatar tim-holdaway avatar zlyle avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

liboauthcpp's Issues

Duplicate oauth_nonce timestamps on fast requests

The srand call in Client::generateNonceTimeStamp should only be called once. This will prevent duplicate time stamps from being returned.

Something like this should fix it:

static bool initialized = false;
if(!initialized)
{
    srand( time( NULL ) );
    initialized = true;
}

Beautiful library!

I just stumbled across this library, and I very much agree with this idea that the interface does not provide network services, just pure OAuth functionality - you "BYONI" (Bring Your Own Networking Interface). What do you think about leveraging Beast's HTTP message model to generate complete messages which Beast can send?
https://github.com/vinniefalco/Beast

Thanks!

Some suggested fixes for compiling x64 version in Visual Studio 2013

Hi all,
Not sure if this is an active project, but there do seem to have been some changes in the last year since I started using it for accessing QuickBooks Online.

Just downloaded the latest files, and there appears to be a new bug in urlencode.cpp, which can be fixed as follows:
change line 97
from: assert(false && 'Unknown urlencode type');
to: assert(false && "Unknown urlencode type");

Previously, when compiling this library into my applications with Visual Studio 2013, there were a number of warning messages, especially when compiling for x64 version.

Firstly, in both liboauthcpp.cpp, and SHA1.cpp, add the following line at top to avoid "strcpy possible unsafe" warnings:
#define _CRT_SECURE_NO_WARNINGS

The attached text file lists changes which have worked for me to stop warning messages when compiling x64. These mostly seem to relate to converting from size_t (which may be a 64 bit value) to an int (which may be only 32 bit value).

Anyway, submitted for consideration and possible inclusion in future versions.

regards,
Garry.

Mods in liboauthcpp to accomodate x64.txt

how to use simple_auth and simple_request

how to use simple_auth and simple_request
This problem appears in simple_auth:
terminate called after throwing an instance of 'OAuth::MissingKeyError'
what(): Couldn't find oauth_token_secret in response Aborted (core dumped).

doesn't work with imgur

OAuth Verification Failed: Verification of signature failed (signature base string was "GET&https%3A%2F%2Fapi.imgur.com%2Foauth%2Frequest_token&oauth_consumer_key%3Dcb5ae201bac88c3ec7c6cac2f07dd7b10509eb5bb%26oauth_nonce%3D135258652339e%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1352586523%26oauth_version%3D1.0"). with Array ( [0] => 9e55e4b7e3bd34207e290d0c59670101 [1] => [2] => )

it gives me this url
https://api.imgur.com/oauth/request_token?oauth_consumer_key=cb5ae201bac88c3ec7c6cac2f07dd7b10509eb5bb&oauth_nonce=135258652339e&oauth_signature=Q4kxVYhGz%252Fd%252F8WE%252FqpATsDtvcOg%253D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1352586523&oauth_version=1.0

CMake does not build an x64 version of the lib

I'd like to build an x64 compatible version for my project (which is x64), but the solution provided by CMake doesn't provide the option. Is there a way to add this functionality to CMake? I'm not very familiar with the tool, unfortunately :(

MinGW/MSYS compilation fails due to wrong compiler parameters being set

Using MinGW in combination with MSYS you have to use the "MSYS Makefiles" generator like this:
$ cd liboauthcpp/build
$ cmake . -G "MSYS Makefiles"
$ make

In the last step it fails because of the following piece of logic in the CMakeFiles.txt (line 36-40):

# Disable some annoying, unnecessary Windows warnings. Enable unwinding for exception handlers.
IF(WIN32)
  ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS -EHsc)
  SET(CMAKE_CXX_FLAGS "-D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS")
ENDIF()

It adds the -EHsc parameter, amongst other things, to the compiler parameters and during make this fails:

$ make
Scanning dependencies of target oauthcpp
[  7%] Building CXX object CMakeFiles/oauthcpp.dir/C_/dev/cpp/libs/liboauthcpp-testing/src/base64.obj
g++.exe: error: unrecognized command line option '-EHsc'
make[2]: *** [CMakeFiles/oauthcpp.dir/C_/dev/cpp/libs/liboauthcpp-testing/src/base64.obj] Error 1
make[1]: *** [CMakeFiles/oauthcpp.dir/all] Error 2
make: *** [all] Error 2

I presume there's some logic missing in order to detect whether it's a Windows platform and using MSVC for the exception settings. When you comment out these lines and run it again everything builds fine.

My John Deere Api does not accept authorization request

I am using this library to access resources as explained in https://colab.research.google.com/github/JohnDeere/DevelopWithDeereNotebooks/blob/master/Onboarding/myjohndeere-api-onboarding.ipynb . In this tutorial they are using requests module from Python. I am able to access resources using Python code. But with liboauth I am able to get verifier. After that server does not accept my request with verifier. It says invalid signature for HMAC_SHA1 method .

Chars to exclude from percent encoding

Hi,

From a Qt client, I'm encoding a URL to be able to make some file operations:
https://github.com/JoseExposito/ubuntuone-qt-files/blob/master/src/com/egg-software/ubuntuone-qt-files/network/AbstractMessage.cpp#L67

For example, to download a file named "(HI).txt" I send a request like this:

https://files.one.ubuntu.com/content/~/Ubuntu%20One/%28HI%29.txt

But I have noticed that the Ubuntu One server returns a "Host requires
authentication" error. To avoid this error I have excluded the chars "("
and ")" from the encoding, and all work as spected with a URL like this:

https://files.one.ubuntu.com/content/~/Ubuntu%20One/(HI).txt

Probably (I'm not sure) the liboauthcpp library is encoding or not some chars to generate the signature, and that is why the Ubuntu One server is returning the "Host requires authentication"

My question is.. Where can I get the full list of chars to exclude from the
encoding?

Thank you very much for the library and the support ๐Ÿ˜„

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.