• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / mender / 1022753986

02 Oct 2023 10:37AM UTC coverage: 78.168% (-2.0%) from 80.127%
1022753986

push

gitlab-ci

oleorhagen
feat: Run the authentication loop once upon bootstrap

Ticket: MEN-6658
Changelog: None

Signed-off-by: Ole Petter <ole.orhagen@northern.tech>

32 of 32 new or added lines in 1 file covered. (100.0%)

6996 of 8950 relevant lines covered (78.17%)

10353.4 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.56
/api/client.cpp
1
// Copyright 2023 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14

15
#include <api/client.hpp>
16

17
#include <common/error.hpp>
18
#include <common/expected.hpp>
19
#include <common/http.hpp>
20
#include <common/log.hpp>
21

22
namespace mender {
23
namespace api {
24

25
namespace error = mender::common::error;
26
namespace expected = mender::common::expected;
27
namespace http = mender::http;
28
namespace log = mender::common::log;
29

30
error::Error Client::AsyncCall(
9✔
31
        http::OutgoingRequestPtr req,
32
        http::ResponseHandler header_handler,
33
        http::ResponseHandler body_handler) {
34
        // If the first request fails with 401, we need to get a new token and then
35
        // try again with the new token. We should avoid using the same
36
        // OutgoingRequest object for the two different requests, hence a copy and a
37
        // different handler using the copy instead of the original OutgoingRequest
38
        // given.
39
        auto reauth_req = make_shared<http::OutgoingRequest>(*req);
18✔
40
        auto reauthenticated_handler =
41
                [this, reauth_req, header_handler, body_handler](auth::ExpectedToken ex_tok) {
4✔
42
                        if (!ex_tok) {
2✔
43
                                log::Error("Failed to obtain authentication credentials");
1✔
44
                                event_loop_.Post([header_handler, ex_tok]() {
1✔
45
                                        error::Error err = ex_tok.error();
1✔
46
                                        header_handler(expected::unexpected(err));
1✔
47
                                });
3✔
48
                                return;
1✔
49
                        }
50
                        reauth_req->SetHeader("Authorization", "Bearer " + ex_tok.value());
1✔
51
                        auto err = http::Client::AsyncCall(reauth_req, header_handler, body_handler);
2✔
52
                        if (err != error::NoError) {
1✔
53
                                log::Error("Failed to schedule an HTTP request with the new token");
×
54
                                event_loop_.Post([header_handler, err]() {
×
55
                                        error::Error err_copy {err};
×
56
                                        header_handler(expected::unexpected(err_copy));
×
57
                                });
×
58
                                return;
×
59
                        }
60
                };
9✔
61

62
        return authenticator_.WithToken(
9✔
63
                [this, req, header_handler, body_handler, reauthenticated_handler](
8✔
64
                        auth::ExpectedToken ex_tok) {
15✔
65
                        if (!ex_tok) {
8✔
66
                                log::Error("Failed to obtain authentication credentials");
1✔
67
                                event_loop_.Post([header_handler, ex_tok]() {
1✔
68
                                        error::Error err = ex_tok.error();
1✔
69
                                        header_handler(expected::unexpected(err));
1✔
70
                                });
3✔
71
                                return;
1✔
72
                        }
73
                        req->SetHeader("Authorization", "Bearer " + ex_tok.value());
7✔
74
                        auto err = http::Client::AsyncCall(
75
                                req,
7✔
76
                                [this, header_handler, reauthenticated_handler](
21✔
77
                                        http::ExpectedIncomingResponsePtr ex_resp) {
6✔
78
                                        if (!ex_resp) {
7✔
79
                                                header_handler(ex_resp);
×
80
                                                return;
5✔
81
                                        }
82
                                        auto resp = ex_resp.value();
7✔
83
                                        auto status = resp->GetStatusCode();
7✔
84
                                        if (status != http::StatusUnauthorized) {
7✔
85
                                                header_handler(ex_resp);
5✔
86
                                                return;
5✔
87
                                        }
88
                                        logger_.Debug(
2✔
89
                                                "Got " + to_string(http::StatusUnauthorized)
4✔
90
                                                + " from the server, expiring token");
4✔
91
                                        authenticator_.ExpireToken();
2✔
92
                                        authenticator_.WithToken(reauthenticated_handler);
2✔
93
                                },
94
                                [body_handler](http::ExpectedIncomingResponsePtr ex_resp) {
14✔
95
                                        if (!ex_resp) {
7✔
96
                                                body_handler(ex_resp);
×
97
                                                return;
×
98
                                        }
99
                                        auto resp = ex_resp.value();
14✔
100
                                        auto status = resp->GetStatusCode();
7✔
101
                                        if (status != http::StatusUnauthorized) {
7✔
102
                                                body_handler(ex_resp);
5✔
103
                                        }
104
                                        // 401 handled by the header handler
105
                                });
14✔
106
                        if (err != error::NoError) {
7✔
107
                                log::Error("Failed to schedule an HTTP request with an existing new token");
×
108
                                event_loop_.Post([header_handler, err]() {
×
109
                                        error::Error err_copy {err};
×
110
                                        header_handler(expected::unexpected(err_copy));
×
111
                                });
×
112
                                return;
×
113
                        }
114
                });
18✔
115
}
116

117
} // namespace api
118
} // namespace mender
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc