class Poco::Net::OAuth20Credentials

Overview

This class implements OAuth 2.0 authentication for HTTP requests, via Bearer tokens in the Authorization header, according to RFC 6749 and RFC 6750. More…

#include <OAuth20Credentials.h>

class OAuth20Credentials
{
public:
    // fields

    static const std::string SCHEME;

    // construction

    OAuth20Credentials();
    OAuth20Credentials(const std::string& bearerToken);

    OAuth20Credentials(
        const std::string& bearerToken,
        const std::string& scheme
        );

    OAuth20Credentials(const HTTPRequest& request);

    OAuth20Credentials(
        const HTTPRequest& request,
        const std::string& scheme
        );

    // methods

    void
    setBearerToken(const std::string& bearerToken);

    const std::string&
    getBearerToken() const;

    void
    setScheme(const std::string& scheme);

    const std::string&
    getScheme() const;

    void
    authenticate(HTTPRequest& request);

protected:
    // methods

    void
    extractBearerToken(const HTTPRequest& request);
};

Detailed Documentation

This class implements OAuth 2.0 authentication for HTTP requests, via Bearer tokens in the Authorization header, according to RFC 6749 and RFC 6750.

To add an Authorization header containing a bearer token to a HTTPRequest object, create an OAuth20Credentials object with the bearer token and call authenticate().

The bearer token can also be extracted from a HTTPRequest by creating the OAuth20Credentials object with a HTTPRequest object containing a “Bearer” Authorization header and calling getBearerToken().

The authorization header scheme can be changed from “Bearer” to a custom value. For example, GitHub uses the “token” scheme.

Construction

OAuth20Credentials()

Creates an empty OAuth20Credentials object.

OAuth20Credentials(const std::string& bearerToken)

Creates an OAuth20Credentials object with the given bearer token.

OAuth20Credentials(
    const std::string& bearerToken,
    const std::string& scheme
    )

Creates an OAuth20Credentials object with the given bearer token and authorization scheme, which overrides the default scheme (“Bearer”).

This is useful for services like GitHub, which use “token” as scheme.

OAuth20Credentials(const HTTPRequest& request)

Creates an OAuth20Credentials object from a HTTPRequest object.

Extracts bearer token from the Authorization header, which must use the “Bearer” authorization scheme.

Throws a NotAuthenticatedException if the request does not contain a bearer token in the Authorization header.

OAuth20Credentials(
    const HTTPRequest& request,
    const std::string& scheme
    )

Creates an OAuth20Credentials object from a HTTPRequest object.

Extracts bearer token from the Authorization header, which must use the given authorization scheme.

Throws a NotAuthenticatedException if the request does not contain a bearer token in the Authorization header.

Methods

void
setBearerToken(const std::string& bearerToken)

Sets the bearer token.

const std::string&
getBearerToken() const

Returns the bearer token.

void
setScheme(const std::string& scheme)

Sets the Authorization header scheme.

const std::string&
getScheme() const

Returns the Authorization header scheme.

void
authenticate(HTTPRequest& request)

Adds an Authorization header containing the bearer token to the HTTPRequest.

void
extractBearerToken(const HTTPRequest& request)

Extracts the bearer token from the HTTPRequest.