Poco::Net

class OAuth20Credentials

File Information

Library: Net
Package: OAuth
Header: Poco/Net/OAuth20Credentials.h

Description

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.

Member Summary

Member Functions: authenticate, extractBearerToken, getBearerToken, getScheme, setBearerToken, setScheme

Constructors

OAuth20Credentials

OAuth20Credentials();

Creates an empty OAuth20Credentials object.

OAuth20Credentials

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

Creates an OAuth20Credentials object with the given bearer token.

OAuth20Credentials

explicit 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

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

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.

Destructor

~OAuth20Credentials

~OAuth20Credentials();

Destroys the HTTPCredentials.

Member Functions

authenticate

void authenticate(
    HTTPRequest & request
);

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

getBearerToken inline

const std::string & getBearerToken() const;

Returns the bearer token.

getScheme inline

const std::string & getScheme() const;

Returns the Authorization header scheme.

setBearerToken

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

Sets the bearer token.

setScheme

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

Sets the Authorization header scheme.

extractBearerToken protected

void extractBearerToken(
    const HTTPRequest & request
);

Extracts the bearer token from the HTTPRequest.

Variables

SCHEME static

static const std::string SCHEME;