-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcredentials.cpp
More file actions
42 lines (35 loc) · 1.29 KB
/
credentials.cpp
File metadata and controls
42 lines (35 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <git2/credential.h>
#include <iostream>
#include "credentials.hpp"
#include "input_output.hpp"
// git_credential_acquire_cb
int user_credentials(
git_credential** out,
const char* url,
const char* username_from_url,
unsigned int allowed_types,
void* payload)
{
// Check for cached credentials here, if desired.
// It might be necessary to make this function stateful to avoid repeating unnecessary checks.
*out = nullptr;
if (allowed_types & GIT_CREDENTIAL_USERPASS_PLAINTEXT) {
std::string username = username_from_url ? username_from_url : "";
if (username.empty()) {
username = prompt_input("Username: ");
}
if (username.empty()) {
giterr_set_str(GIT_ERROR_HTTP, "No username specified");
return GIT_EAUTH;
}
std::string password = prompt_input("Password: ", false);
if (password.empty()) {
giterr_set_str(GIT_ERROR_HTTP, "No password specified");
return GIT_EAUTH;
}
// If successful, this will create and return a git_credential* in the out argument.
return git_credential_userpass_plaintext_new(out, username.c_str(), password.c_str());
}
giterr_set_str(GIT_ERROR_HTTP, "Unexpected credentials request");
return GIT_ERROR;
}