]> git.proxmox.com Git - libgit2.git/blame - src/transports/cred_helpers.c
Merge pull request #3016 from pks-t/ignore-exclude-fix
[libgit2.git] / src / transports / cred_helpers.c
CommitLineData
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"
9#include "git2/cred_helpers.h"
10
11int git_cred_userpass(
12 git_cred **cred,
13 const char *url,
7602cb7c 14 const char *user_from_url,
520dcc1c
BS
15 unsigned int allowed_types,
16 void *payload)
17{
18 git_cred_userpass_payload *userpass = (git_cred_userpass_payload*)payload;
7602cb7c 19 const char *effective_username = NULL;
520dcc1c
BS
20
21 GIT_UNUSED(url);
22
7602cb7c
BS
23 if (!userpass || !userpass->password) return -1;
24
25 /* Username resolution: a username can be passed with the URL, the
630146bd
BS
26 * credentials payload, or both. Here's what we do. Note that if we get
27 * this far, we know that any password the url may contain has already
28 * failed at least once, so we ignore it.
7602cb7c
BS
29 *
30 * | Payload | URL | Used |
31 * +-------------+----------+-----------+
32 * | yes | no | payload |
33 * | yes | yes | payload |
34 * | no | yes | url |
35 * | no | no | FAIL |
36 */
630146bd
BS
37 if (userpass->username)
38 effective_username = userpass->username;
39 else if (user_from_url)
7602cb7c 40 effective_username = user_from_url;
630146bd
BS
41 else
42 return -1;
520dcc1c 43
ccb85c8f
CMN
44 if (GIT_CREDTYPE_USERNAME & allowed_types)
45 return git_cred_username_new(cred, effective_username);
46
520dcc1c 47 if ((GIT_CREDTYPE_USERPASS_PLAINTEXT & allowed_types) == 0 ||
7602cb7c 48 git_cred_userpass_plaintext_new(cred, effective_username, userpass->password) < 0)
520dcc1c
BS
49 return -1;
50
51 return 0;
52}