Blame src/transports/cred_helpers.c

Packit ae9e2a
/*
Packit ae9e2a
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit ae9e2a
 *
Packit ae9e2a
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit ae9e2a
 * a Linking Exception. For full terms see the included COPYING file.
Packit ae9e2a
 */
Packit ae9e2a
Packit ae9e2a
#include "common.h"
Packit ae9e2a
#include "git2/cred_helpers.h"
Packit ae9e2a
Packit ae9e2a
int git_cred_userpass(
Packit ae9e2a
		git_cred **cred,
Packit ae9e2a
		const char *url,
Packit ae9e2a
		const char *user_from_url,
Packit ae9e2a
		unsigned int allowed_types,
Packit ae9e2a
		void *payload)
Packit ae9e2a
{
Packit ae9e2a
	git_cred_userpass_payload *userpass = (git_cred_userpass_payload*)payload;
Packit ae9e2a
	const char *effective_username = NULL;
Packit ae9e2a
Packit ae9e2a
	GIT_UNUSED(url);
Packit ae9e2a
Packit ae9e2a
	if (!userpass || !userpass->password) return -1;
Packit ae9e2a
Packit ae9e2a
	/* Username resolution: a username can be passed with the URL, the
Packit ae9e2a
	 * credentials payload, or both. Here's what we do.  Note that if we get
Packit ae9e2a
	 * this far, we know that any password the url may contain has already
Packit ae9e2a
	 * failed at least once, so we ignore it.
Packit ae9e2a
	 *
Packit ae9e2a
	 * |  Payload    |   URL    |   Used    |
Packit ae9e2a
	 * +-------------+----------+-----------+
Packit ae9e2a
	 * |    yes      |   no     |  payload  |
Packit ae9e2a
	 * |    yes      |   yes    |  payload  |
Packit ae9e2a
	 * |    no       |   yes    |  url      |
Packit ae9e2a
	 * |    no       |   no     |  FAIL     |
Packit ae9e2a
	 */
Packit ae9e2a
	if (userpass->username)
Packit ae9e2a
		effective_username = userpass->username;
Packit ae9e2a
	else if (user_from_url)
Packit ae9e2a
		effective_username = user_from_url;
Packit ae9e2a
	else
Packit ae9e2a
		return -1;
Packit ae9e2a
Packit ae9e2a
	if (GIT_CREDTYPE_USERNAME & allowed_types)
Packit ae9e2a
		return git_cred_username_new(cred, effective_username);
Packit ae9e2a
Packit ae9e2a
	if ((GIT_CREDTYPE_USERPASS_PLAINTEXT & allowed_types) == 0 ||
Packit ae9e2a
			git_cred_userpass_plaintext_new(cred, effective_username, userpass->password) < 0)
Packit ae9e2a
		return -1;
Packit ae9e2a
Packit ae9e2a
	return 0;
Packit ae9e2a
}