summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Straub <bs@github.com>2013-01-08 12:58:20 -0800
committerBen Straub <bs@github.com>2013-01-09 13:31:17 -0800
commitffb02b1630da85e063a816cc6dddcdc004a8ff72 (patch)
treef8660cf4d0c99b7185b1428edc777c6f573b9041
parent252b24049cf7018ba80c736fda636c53c465fbd2 (diff)
downloadlibgit2-ffb02b1630da85e063a816cc6dddcdc004a8ff72.tar.gz
Expose stock user/pass credential utility
-rw-r--r--include/git2/transport.h20
-rw-r--r--src/transports/cred.c20
-rw-r--r--tests-clar/network/cred.c27
-rw-r--r--tests-clar/online/clone.c29
4 files changed, 71 insertions, 25 deletions
diff --git a/include/git2/transport.h b/include/git2/transport.h
index fba5fb920..f03e9fc87 100644
--- a/include/git2/transport.h
+++ b/include/git2/transport.h
@@ -68,6 +68,26 @@ typedef int (*git_cred_acquire_cb)(
unsigned int allowed_types,
void *payload);
+/**
+ * Payload for git_cred_stock_userpass_plaintext.
+ */
+typedef struct git_cred_stock_userpass_plaintext_payload {
+ char *username;
+ char *password;
+} git_cred_stock_userpass_plaintext_payload;
+
+
+/**
+ * Stock callback usable as a git_cred_acquire_cb. This calls
+ * git_cred_userpass_plaintext_new unless the protocol has not specified
+ * GIT_CREDTYPE_USERPASS_PLAINTEXT as an allowed type.
+ */
+GIT_EXTERN(int) git_cred_stock_userpass_plaintext(
+ git_cred **cred,
+ const char *url,
+ unsigned int allowed_types,
+ void *payload);
+
/*
*** End interface for credentials acquisition ***
*** Begin base transport interface ***
diff --git a/src/transports/cred.c b/src/transports/cred.c
index 85472bd6a..5ecb8a4b9 100644
--- a/src/transports/cred.c
+++ b/src/transports/cred.c
@@ -57,3 +57,23 @@ int git_cred_userpass_plaintext_new(
*cred = &c->parent;
return 0;
}
+
+int git_cred_stock_userpass_plaintext(
+ git_cred **cred,
+ const char *url,
+ unsigned int allowed_types,
+ void *payload)
+{
+ git_cred_stock_userpass_plaintext_payload *userpass =
+ (git_cred_stock_userpass_plaintext_payload*)payload;
+
+ GIT_UNUSED(url);
+
+ if (!userpass || !userpass->username || !userpass->password) return -1;
+
+ if ((GIT_CREDTYPE_USERPASS_PLAINTEXT & allowed_types) == 0 ||
+ git_cred_userpass_plaintext_new(cred, userpass->username, userpass->password) < 0)
+ return -1;
+
+ return 0;
+}
diff --git a/tests-clar/network/cred.c b/tests-clar/network/cred.c
new file mode 100644
index 000000000..52920c3f2
--- /dev/null
+++ b/tests-clar/network/cred.c
@@ -0,0 +1,27 @@
+#include "clar_libgit2.h"
+
+#include "git2/transport.h"
+
+void test_network_cred__stock_userpass_validates_args(void)
+{
+ git_cred_stock_userpass_plaintext_payload payload = {0};
+
+ cl_git_fail(git_cred_stock_userpass_plaintext(NULL, NULL, 0, NULL));
+
+ payload.username = "user";
+ cl_git_fail(git_cred_stock_userpass_plaintext(NULL, NULL, 0, &payload));
+
+ payload.username = NULL;
+ payload.username = "pass";
+ cl_git_fail(git_cred_stock_userpass_plaintext(NULL, NULL, 0, &payload));
+}
+
+void test_network_cred__stock_userpass_validates_that_method_is_allowed(void)
+{
+ git_cred *cred;
+ git_cred_stock_userpass_plaintext_payload payload = {"user", "pass"};
+
+ cl_git_fail(git_cred_stock_userpass_plaintext(&cred, NULL, 0, &payload));
+ cl_git_pass(git_cred_stock_userpass_plaintext(&cred, NULL, GIT_CREDTYPE_USERPASS_PLAINTEXT, &payload));
+ git__free(cred);
+}
diff --git a/tests-clar/online/clone.c b/tests-clar/online/clone.c
index 082ed52b3..9c51d692c 100644
--- a/tests-clar/online/clone.c
+++ b/tests-clar/online/clone.c
@@ -121,7 +121,7 @@ static int update_tips(const char *refname, const git_oid *a, const git_oid *b,
return 0;
}
-void test_clone_network__custom_remote_callbacks(void)
+void test_online_clone__custom_remote_callbacks(void)
{
git_remote_callbacks remote_callbacks = GIT_REMOTE_CALLBACKS_INIT;
int callcount = 0;
@@ -134,39 +134,18 @@ void test_clone_network__custom_remote_callbacks(void)
cl_assert(callcount > 0);
}
-struct cred_user_pass {
- const char *user;
- const char *pass;
-};
-
-static int cred_acquire(
- git_cred **cred,
- const char *url,
- unsigned int allowed_types,
- void *payload)
-{
- struct cred_user_pass *user_pass = (struct cred_user_pass*)payload;
-
- GIT_UNUSED(url);
- if ((GIT_CREDTYPE_USERPASS_PLAINTEXT & allowed_types) == 0 ||
- git_cred_userpass_plaintext_new(cred, user_pass->user, user_pass->pass) < 0)
- return -1;
-
- return 0;
-}
-
-void test_clone_network__credentials(void)
+void test_online_clone__credentials(void)
{
/* Remote URL environment variable must be set. User and password are optional. */
const char *remote_url = cl_getenv("GITTEST_REMOTE_URL");
- struct cred_user_pass user_pass = {
+ git_cred_stock_userpass_plaintext_payload user_pass = {
cl_getenv("GITTEST_REMOTE_USER"),
cl_getenv("GITTEST_REMOTE_PASS")
};
if (!remote_url) return;
- g_options.cred_acquire_cb = cred_acquire;
+ g_options.cred_acquire_cb = git_cred_stock_userpass_plaintext;
g_options.cred_acquire_payload = &user_pass;
cl_git_pass(git_clone(&g_repo, remote_url, "./foo", &g_options));