/src/poco/Net/src/OAuth20Credentials.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // |
2 | | // OAuth20Credentials.cpp |
3 | | // |
4 | | // Library: Net |
5 | | // Package: OAuth |
6 | | // Module: OAuth20Credentials |
7 | | // |
8 | | // Copyright (c) 2014, Applied Informatics Software Engineering GmbH. |
9 | | // and Contributors. |
10 | | // |
11 | | // SPDX-License-Identifier: BSL-1.0 |
12 | | // |
13 | | |
14 | | |
15 | | #include "Poco/Net/OAuth20Credentials.h" |
16 | | #include "Poco/Net/HTTPRequest.h" |
17 | | #include "Poco/Net/NetException.h" |
18 | | #include "Poco/String.h" |
19 | | |
20 | | |
21 | | namespace Poco { |
22 | | namespace Net { |
23 | | |
24 | | |
25 | | const std::string OAuth20Credentials::SCHEME = "Bearer"; |
26 | | |
27 | | |
28 | | OAuth20Credentials::OAuth20Credentials(): |
29 | 0 | _scheme(SCHEME) |
30 | 0 | { |
31 | 0 | } |
32 | | |
33 | | |
34 | | OAuth20Credentials::OAuth20Credentials(const std::string& bearerToken): |
35 | 0 | _bearerToken(bearerToken), |
36 | 0 | _scheme(SCHEME) |
37 | 0 | { |
38 | 0 | } |
39 | | |
40 | | |
41 | | OAuth20Credentials::OAuth20Credentials(const std::string& bearerToken, const std::string& scheme): |
42 | 0 | _bearerToken(bearerToken), |
43 | 0 | _scheme(scheme) |
44 | 0 | { |
45 | 0 | } |
46 | | |
47 | | |
48 | | OAuth20Credentials::OAuth20Credentials(const HTTPRequest& request): |
49 | 8.01k | _scheme(SCHEME) |
50 | 8.01k | { |
51 | 8.01k | extractBearerToken(request); |
52 | 8.01k | } |
53 | | |
54 | | |
55 | | OAuth20Credentials::OAuth20Credentials(const HTTPRequest& request, const std::string& scheme): |
56 | 0 | _scheme(scheme) |
57 | 0 | { |
58 | 0 | extractBearerToken(request); |
59 | 0 | } |
60 | | |
61 | | |
62 | | OAuth20Credentials::~OAuth20Credentials() |
63 | 14 | { |
64 | 14 | } |
65 | | |
66 | | |
67 | | void OAuth20Credentials::setBearerToken(const std::string& bearerToken) |
68 | 0 | { |
69 | 0 | _bearerToken = bearerToken; |
70 | 0 | } |
71 | | |
72 | | |
73 | | void OAuth20Credentials::setScheme(const std::string& scheme) |
74 | 0 | { |
75 | 0 | _scheme = scheme; |
76 | 0 | } |
77 | | |
78 | | |
79 | | void OAuth20Credentials::authenticate(HTTPRequest& request) |
80 | 14 | { |
81 | 14 | std::string auth(_scheme); |
82 | 14 | auth += ' '; |
83 | 14 | auth += _bearerToken; |
84 | 14 | request.set(HTTPRequest::AUTHORIZATION, auth); |
85 | 14 | } |
86 | | |
87 | | |
88 | | void OAuth20Credentials::extractBearerToken(const HTTPRequest& request) |
89 | 8.01k | { |
90 | 8.01k | if (request.hasCredentials()) |
91 | 2.44k | { |
92 | 2.44k | std::string authScheme; |
93 | 2.44k | std::string authInfo; |
94 | 2.44k | request.getCredentials(authScheme, authInfo); |
95 | 2.44k | if (icompare(authScheme, _scheme) == 0) |
96 | 14 | { |
97 | 14 | _bearerToken = authInfo; |
98 | 14 | } |
99 | 2.43k | else throw NotAuthenticatedException("No bearer token in Authorization header", authScheme); |
100 | 2.44k | } |
101 | 5.56k | else throw NotAuthenticatedException("No Authorization header found"); |
102 | 8.01k | } |
103 | | |
104 | | |
105 | | } } // namespace Poco::Net |