]>
Commit | Line | Data |
---|---|---|
520dcc1c BS |
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" | |
eae0bfdc | 9 | |
520dcc1c BS |
10 | #include "git2/cred_helpers.h" |
11 | ||
12 | int git_cred_userpass( | |
13 | git_cred **cred, | |
14 | const char *url, | |
7602cb7c | 15 | const char *user_from_url, |
520dcc1c BS |
16 | unsigned int allowed_types, |
17 | void *payload) | |
18 | { | |
19 | git_cred_userpass_payload *userpass = (git_cred_userpass_payload*)payload; | |
7602cb7c | 20 | const char *effective_username = NULL; |
520dcc1c BS |
21 | |
22 | GIT_UNUSED(url); | |
23 | ||
7602cb7c BS |
24 | if (!userpass || !userpass->password) return -1; |
25 | ||
26 | /* Username resolution: a username can be passed with the URL, the | |
630146bd BS |
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. | |
7602cb7c BS |
30 | * |
31 | * | Payload | URL | Used | | |
32 | * +-------------+----------+-----------+ | |
33 | * | yes | no | payload | | |
34 | * | yes | yes | payload | | |
35 | * | no | yes | url | | |
36 | * | no | no | FAIL | | |
37 | */ | |
630146bd BS |
38 | if (userpass->username) |
39 | effective_username = userpass->username; | |
40 | else if (user_from_url) | |
7602cb7c | 41 | effective_username = user_from_url; |
630146bd BS |
42 | else |
43 | return -1; | |
520dcc1c | 44 | |
ccb85c8f CMN |
45 | if (GIT_CREDTYPE_USERNAME & allowed_types) |
46 | return git_cred_username_new(cred, effective_username); | |
47 | ||
520dcc1c | 48 | if ((GIT_CREDTYPE_USERPASS_PLAINTEXT & allowed_types) == 0 || |
7602cb7c | 49 | git_cred_userpass_plaintext_new(cred, effective_username, userpass->password) < 0) |
520dcc1c BS |
50 | return -1; |
51 | ||
52 | return 0; | |
53 | } |