Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
authenticate.cc
1 #include "authenticate.h"
2 
3 #include "logging/logging.h"
4 #include "oauth2.h"
5 
6 using std::string;
7 
8 int authenticate(const string &cacerts, const ServerCredentials &creds, TreehubServer &treehub) {
9  switch (creds.GetMethod()) {
10  case AuthMethod::kBasic: {
11  treehub.SetAuthBasic(creds.GetAuthUser(), creds.GetAuthPassword());
12  break;
13  }
14  case AuthMethod::kOauth2: {
15  OAuth2 oauth2(creds.GetAuthServer(), creds.GetClientId(), creds.GetClientSecret(), creds.GetScope(), cacerts);
16 
17  if (!creds.GetClientId().empty()) {
18  if (oauth2.Authenticate() != AuthenticationResult::kSuccess) {
19  LOG_FATAL << "Authentication with oauth2 failed";
20  return EXIT_FAILURE;
21  }
22  LOG_INFO << "Using oauth2 authentication token";
23  treehub.SetToken(oauth2.token());
24 
25  } else {
26  LOG_INFO << "Skipping Authentication";
27  }
28  break;
29  }
30  case AuthMethod::kTls: {
31  treehub.SetCerts(creds.GetClientP12());
32  break;
33  }
34  case AuthMethod::kNone:
35  break;
36  default: {
37  LOG_FATAL << "Unexpected authentication method value " << static_cast<int>(creds.GetMethod());
38  return EXIT_FAILURE;
39  }
40  }
41  // Setup ca certificates in all cases. Even with no authentication, curl
42  // checks ca certs by default. Furthermore, curl embeds the path to ca certs
43  // that it was built with and this breaks under bitbake when sharing sstate
44  // cache between machines.
45  treehub.ca_certs(cacerts);
46  treehub.root_url(creds.GetOSTreeServer());
47  treehub.repo_url(creds.GetRepoUrl());
48 
49  return EXIT_SUCCESS;
50 }
51 
52 // vim: set tabstop=2 shiftwidth=2 expandtab:
OAuth2
Definition: oauth2.h:9
ServerCredentials
Definition: server_credentials.h:25
TreehubServer
Definition: treehub_server.h:11