123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- //
- // OAuth20Credentials.cpp
- //
- // Library: Net
- // Package: OAuth
- // Module: OAuth20Credentials
- //
- // Copyright (c) 2014, Applied Informatics Software Engineering GmbH.
- // and Contributors.
- //
- // SPDX-License-Identifier: BSL-1.0
- //
- #include "Poco/Net/OAuth20Credentials.h"
- #include "Poco/Net/HTTPRequest.h"
- #include "Poco/Net/NetException.h"
- #include "Poco/String.h"
- namespace Poco {
- namespace Net {
- const std::string OAuth20Credentials::SCHEME = "Bearer";
- OAuth20Credentials::OAuth20Credentials():
- _scheme(SCHEME)
- {
- }
- OAuth20Credentials::OAuth20Credentials(const std::string& bearerToken):
- _bearerToken(bearerToken),
- _scheme(SCHEME)
- {
- }
- OAuth20Credentials::OAuth20Credentials(const std::string& bearerToken, const std::string& scheme):
- _bearerToken(bearerToken),
- _scheme(scheme)
- {
- }
- OAuth20Credentials::OAuth20Credentials(const HTTPRequest& request):
- _scheme(SCHEME)
- {
- extractBearerToken(request);
- }
- OAuth20Credentials::OAuth20Credentials(const HTTPRequest& request, const std::string& scheme):
- _scheme(scheme)
- {
- extractBearerToken(request);
- }
- OAuth20Credentials::~OAuth20Credentials()
- {
- }
- void OAuth20Credentials::setBearerToken(const std::string& bearerToken)
- {
- _bearerToken = bearerToken;
- }
- void OAuth20Credentials::setScheme(const std::string& scheme)
- {
- _scheme = scheme;
- }
-
- void OAuth20Credentials::authenticate(HTTPRequest& request)
- {
- std::string auth(_scheme);
- auth += ' ';
- auth += _bearerToken;
- request.set(HTTPRequest::AUTHORIZATION, auth);
- }
- void OAuth20Credentials::extractBearerToken(const HTTPRequest& request)
- {
- if (request.hasCredentials())
- {
- std::string authScheme;
- std::string authInfo;
- request.getCredentials(authScheme, authInfo);
- if (icompare(authScheme, _scheme) == 0)
- {
- _bearerToken = authInfo;
- }
- else throw NotAuthenticatedException("No bearer token in Authorization header", authScheme);
- }
- else throw NotAuthenticatedException("No Authorization header found");
- }
- } } // namespace Poco::Net
|