]> git.proxmox.com Git - libgit2.git/blob - src/libgit2/transports/credential_helpers.c
New upstream version 1.5.0+ds
[libgit2.git] / src / libgit2 / transports / credential_helpers.c
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
8 #include "common.h"
9
10 #include "git2/credential_helpers.h"
11
12 int git_credential_userpass(
13 git_credential **cred,
14 const char *url,
15 const char *user_from_url,
16 unsigned int allowed_types,
17 void *payload)
18 {
19 git_credential_userpass_payload *userpass = (git_credential_userpass_payload*)payload;
20 const char *effective_username = NULL;
21
22 GIT_UNUSED(url);
23
24 if (!userpass || !userpass->password) return -1;
25
26 /* Username resolution: a username can be passed with the URL, the
27 * credentials payload, or both. Here's what we do. Note that if we get
28 * this far, we know that any password the url may contain has already
29 * failed at least once, so we ignore it.
30 *
31 * | Payload | URL | Used |
32 * +-------------+----------+-----------+
33 * | yes | no | payload |
34 * | yes | yes | payload |
35 * | no | yes | url |
36 * | no | no | FAIL |
37 */
38 if (userpass->username)
39 effective_username = userpass->username;
40 else if (user_from_url)
41 effective_username = user_from_url;
42 else
43 return -1;
44
45 if (GIT_CREDENTIAL_USERNAME & allowed_types)
46 return git_credential_username_new(cred, effective_username);
47
48 if ((GIT_CREDENTIAL_USERPASS_PLAINTEXT & allowed_types) == 0 ||
49 git_credential_userpass_plaintext_new(cred, effective_username, userpass->password) < 0)
50 return -1;
51
52 return 0;
53 }
54
55 /* Deprecated credential functions */
56
57 #ifndef GIT_DEPRECATE_HARD
58 int git_cred_userpass(
59 git_credential **out,
60 const char *url,
61 const char *user_from_url,
62 unsigned int allowed_types,
63 void *payload)
64 {
65 return git_credential_userpass(out, url, user_from_url,
66 allowed_types, payload);
67 }
68 #endif