auth.c 1.41 KB
Newer Older
1 2 3 4 5 6 7
/*
 * Copyright (C) the libgit2 contributors. All rights reserved.
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

8 9
#include "auth.h"

10
#include "git2/sys/credential.h"
11 12

static int basic_next_token(
13
	git_str *out,
14
	git_http_auth_context *ctx,
15
	git_credential *c)
16
{
17
	git_credential_userpass_plaintext *cred;
18
	git_str raw = GIT_STR_INIT;
19
	int error = GIT_EAUTH;
20 21 22

	GIT_UNUSED(ctx);

23
	if (c->credtype != GIT_CREDENTIAL_USERPASS_PLAINTEXT) {
24
		git_error_set(GIT_ERROR_INVALID, "invalid credential type for basic auth");
25 26 27
		goto on_error;
	}

28
	cred = (git_credential_userpass_plaintext *)c;
29

30
	git_str_printf(&raw, "%s:%s", cred->username, cred->password);
31

32 33 34
	if (git_str_oom(&raw) ||
		git_str_puts(out, "Basic ") < 0 ||
		git_str_encode_base64(out, git_str_cstr(&raw), raw.size) < 0)
35 36 37 38 39 40 41 42
		goto on_error;

	error = 0;

on_error:
	if (raw.size)
		git__memzero(raw.ptr, raw.size);

43
	git_str_dispose(&raw);
44 45 46 47
	return error;
}

static git_http_auth_context basic_context = {
48
	GIT_HTTP_AUTH_BASIC,
49
	GIT_CREDENTIAL_USERPASS_PLAINTEXT,
50
	0,
51 52
	NULL,
	basic_next_token,
53
	NULL,
54 55 56 57
	NULL
};

int git_http_auth_basic(
58
	git_http_auth_context **out, const git_net_url *url)
59
{
60
	GIT_UNUSED(url);
61 62 63 64 65 66

	*out = &basic_context;
	return 0;
}

int git_http_auth_dummy(
67
	git_http_auth_context **out, const git_net_url *url)
68
{
69
	GIT_UNUSED(url);
70 71

	*out = NULL;
72
	return GIT_PASSTHROUGH;
73 74
}