introduce credentials API

There are a few places in git that need to get a username and password credential from the user; the most notable one is HTTP authentication for smart-http pushing. Right now the only choices for providing credentials are to put them plaintext into your ~/.netrc, or to have git prompt you (either on the terminal or via an askpass program). The former is not very secure, and the latter is not very convenient. Unfortunately, there is no "always best" solution for password management. The details will depend on the tradeoff you want between security and convenience, as well as how git can integrate with other security systems (e.g., many operating systems provide a keychain or password wallet for single sign-on). This patch provides an abstract notion of credentials as a data item, and provides three basic operations: - fill (i.e., acquire from external storage or from the user) - approve (mark a credential as "working" for further storage) - reject (mark a credential as "not working", so it can be removed from storage) These operations can be backed by external helper processes that interact with system- or user-specific secure storage. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Dec 10, 2011 at 05:31 UTC abca927dbef2c310056b8a1a8be5561212b3243a
8 files changed +773
.gitignore
+1
@@ -167,6 +167,7 @@
167 /gitweb/static/gitweb.js
168 /gitweb/static/gitweb.min.*
169 /test-chmtime
170 +/test-credential
171 /test-ctype
172 /test-date
173 /test-delta
Documentation/technical/api-credentials.txt new
+241
@@ -0,0 +1,241 @@
1 +credentials API
2 +===============
3 +
4 +The credentials API provides an abstracted way of gathering username and
5 +password credentials from the user (even though credentials in the wider
6 +world can take many forms, in this document the word "credential" always
7 +refers to a username and password pair).
8 +
9 +Data Structures
10 +---------------
11 +
12 +`struct credential`::
13 +
14 + This struct represents a single username/password combination
15 + along with any associated context. All string fields should be
16 + heap-allocated (or NULL if they are not known or not applicable).
17 + The meaning of the individual context fields is the same as
18 + their counterparts in the helper protocol; see the section below
19 + for a description of each field.
20 ++
21 +The `helpers` member of the struct is a `string_list` of helpers. Each
22 +string specifies an external helper which will be run, in order, to
23 +either acquire or store credentials. See the section on credential
24 +helpers below.
25 ++
26 +This struct should always be initialized with `CREDENTIAL_INIT` or
27 +`credential_init`.
28 +
29 +
30 +Functions
31 +---------
32 +
33 +`credential_init`::
34 +
35 + Initialize a credential structure, setting all fields to empty.
36 +
37 +`credential_clear`::
38 +
39 + Free any resources associated with the credential structure,
40 + returning it to a pristine initialized state.
41 +
42 +`credential_fill`::
43 +
44 + Instruct the credential subsystem to fill the username and
45 + password fields of the passed credential struct by first
46 + consulting helpers, then asking the user. After this function
47 + returns, the username and password fields of the credential are
48 + guaranteed to be non-NULL. If an error occurs, the function will
49 + die().
50 +
51 +`credential_reject`::
52 +
53 + Inform the credential subsystem that the provided credentials
54 + have been rejected. This will cause the credential subsystem to
55 + notify any helpers of the rejection (which allows them, for
56 + example, to purge the invalid credentials from storage). It
57 + will also free() the username and password fields of the
58 + credential and set them to NULL (readying the credential for
59 + another call to `credential_fill`). Any errors from helpers are
60 + ignored.
61 +
62 +`credential_approve`::
63 +
64 + Inform the credential subsystem that the provided credentials
65 + were successfully used for authentication. This will cause the
66 + credential subsystem to notify any helpers of the approval, so
67 + that they may store the result to be used again. Any errors
68 + from helpers are ignored.
69 +
70 +Example
71 +-------
72 +
73 +The example below shows how the functions of the credential API could be
74 +used to login to a fictitious "foo" service on a remote host:
75 +
76 +-----------------------------------------------------------------------
77 +int foo_login(struct foo_connection *f)
78 +{
79 + int status;
80 + /*
81 + * Create a credential with some context; we don't yet know the
82 + * username or password.
83 + */
84 +
85 + struct credential c = CREDENTIAL_INIT;
86 + c.protocol = xstrdup("foo");
87 + c.host = xstrdup(f->hostname);
88 +
89 + /*
90 + * Fill in the username and password fields by contacting
91 + * helpers and/or asking the user. The function will die if it
92 + * fails.
93 + */
94 + credential_fill(&c);
95 +
96 + /*
97 + * Otherwise, we have a username and password. Try to use it.
98 + */
99 + status = send_foo_login(f, c.username, c.password);
100 + switch (status) {
101 + case FOO_OK:
102 + /* It worked. Store the credential for later use. */
103 + credential_accept(&c);
104 + break;
105 + case FOO_BAD_LOGIN:
106 + /* Erase the credential from storage so we don't try it
107 + * again. */
108 + credential_reject(&c);
109 + break;
110 + default:
111 + /*
112 + * Some other error occured. We don't know if the
113 + * credential is good or bad, so report nothing to the
114 + * credential subsystem.
115 + */
116 + }
117 +
118 + /* Free any associated resources. */
119 + credential_clear(&c);
120 +
121 + return status;
122 +}
123 +-----------------------------------------------------------------------
124 +
125 +
126 +Credential Helpers
127 +------------------
128 +
129 +Credential helpers are programs executed by git to fetch or save
130 +credentials from and to long-term storage (where "long-term" is simply
131 +longer than a single git process; e.g., credentials may be stored
132 +in-memory for a few minutes, or indefinitely on disk).
133 +
134 +Each helper is specified by a single string. The string is transformed
135 +by git into a command to be executed using these rules:
136 +
137 + 1. If the helper string begins with "!", it is considered a shell
138 + snippet, and everything after the "!" becomes the command.
139 +
140 + 2. Otherwise, if the helper string begins with an absolute path, the
141 + verbatim helper string becomes the command.
142 +
143 + 3. Otherwise, the string "git credential-" is prepended to the helper
144 + string, and the result becomes the command.
145 +
146 +The resulting command then has an "operation" argument appended to it
147 +(see below for details), and the result is executed by the shell.
148 +
149 +Here are some example specifications:
150 +
151 +----------------------------------------------------
152 +# run "git credential-foo"
153 +foo
154 +
155 +# same as above, but pass an argument to the helper
156 +foo --bar=baz
157 +
158 +# the arguments are parsed by the shell, so use shell
159 +# quoting if necessary
160 +foo --bar="whitespace arg"
161 +
162 +# you can also use an absolute path, which will not use the git wrapper
163 +/path/to/my/helper --with-arguments
164 +
165 +# or you can specify your own shell snippet
166 +!f() { echo "password=`cat $HOME/.secret`"; }; f
167 +----------------------------------------------------
168 +
169 +Generally speaking, rule (3) above is the simplest for users to specify.
170 +Authors of credential helpers should make an effort to assist their
171 +users by naming their program "git-credential-$NAME", and putting it in
172 +the $PATH or $GIT_EXEC_PATH during installation, which will allow a user
173 +to enable it with `git config credential.helper $NAME`.
174 +
175 +When a helper is executed, it will have one "operation" argument
176 +appended to its command line, which is one of:
177 +
178 +`get`::
179 +
180 + Return a matching credential, if any exists.
181 +
182 +`store`::
183 +
184 + Store the credential, if applicable to the helper.
185 +
186 +`erase`::
187 +
188 + Remove a matching credential, if any, from the helper's storage.
189 +
190 +The details of the credential will be provided on the helper's stdin
191 +stream. The credential is split into a set of named attributes.
192 +Attributes are provided to the helper, one per line. Each attribute is
193 +specified by a key-value pair, separated by an `=` (equals) sign,
194 +followed by a newline. The key may contain any bytes except `=`,
195 +newline, or NUL. The value may contain any bytes except newline or NUL.
196 +In both cases, all bytes are treated as-is (i.e., there is no quoting,
197 +and one cannot transmit a value with newline or NUL in it). The list of
198 +attributes is terminated by a blank line or end-of-file.
199 +
200 +Git will send the following attributes (but may not send all of
201 +them for a given credential; for example, a `host` attribute makes no
202 +sense when dealing with a non-network protocol):
203 +
204 +`protocol`::
205 +
206 + The protocol over which the credential will be used (e.g.,
207 + `https`).
208 +
209 +`host`::
210 +
211 + The remote hostname for a network credential.
212 +
213 +`path`::
214 +
215 + The path with which the credential will be used. E.g., for
216 + accessing a remote https repository, this will be the
217 + repository's path on the server.
218 +
219 +`username`::
220 +
221 + The credential's username, if we already have one (e.g., from a
222 + URL, from the user, or from a previously run helper).
223 +
224 +`password`::
225 +
226 + The credential's password, if we are asking it to be stored.
227 +
228 +For a `get` operation, the helper should produce a list of attributes
229 +on stdout in the same format. A helper is free to produce a subset, or
230 +even no values at all if it has nothing useful to provide. Any provided
231 +attributes will overwrite those already known about by git.
232 +
233 +For a `store` or `erase` operation, the helper's output is ignored.
234 +If it fails to perform the requested operation, it may complain to
235 +stderr to inform the user. If it does not support the requested
236 +operation (e.g., a read-only store), it should silently ignore the
237 +request.
238 +
239 +If a helper receives any other operation, it should silently ignore the
240 +request. This leaves room for future operations to be added (older
241 +helpers will just ignore the new requests).
Makefile
+3
@@ -431,6 +431,7 @@ PROGRAM_OBJS += sh-i18n--envsubst.o
431 PROGRAMS += $(patsubst %.o,git-%$X,$(PROGRAM_OBJS))
432
433 TEST_PROGRAMS_NEED_X += test-chmtime
434 +TEST_PROGRAMS_NEED_X += test-credential
435 TEST_PROGRAMS_NEED_X += test-ctype
436 TEST_PROGRAMS_NEED_X += test-date
437 TEST_PROGRAMS_NEED_X += test-delta
@@ -525,6 +526,7 @@ LIB_H += compat/win32/poll.h
526 LIB_H += compat/win32/dirent.h
527 LIB_H += connected.h
528 LIB_H += convert.h
529 +LIB_H += credential.h
530 LIB_H += csum-file.h
531 LIB_H += decorate.h
532 LIB_H += delta.h
@@ -609,6 +611,7 @@ LIB_OBJS += connect.o
611 LIB_OBJS += connected.o
612 LIB_OBJS += convert.o
613 LIB_OBJS += copy.o
614 +LIB_OBJS += credential.o
615 LIB_OBJS += csum-file.o
616 LIB_OBJS += ctype.o
617 LIB_OBJS += date.o
credential.c new
+234
@@ -0,0 +1,234 @@
1 +#include "cache.h"
2 +#include "credential.h"
3 +#include "string-list.h"
4 +#include "run-command.h"
5 +
6 +void credential_init(struct credential *c)
7 +{
8 + memset(c, 0, sizeof(*c));
9 + c->helpers.strdup_strings = 1;
10 +}
11 +
12 +void credential_clear(struct credential *c)
13 +{
14 + free(c->protocol);
15 + free(c->host);
16 + free(c->path);
17 + free(c->username);
18 + free(c->password);
19 + string_list_clear(&c->helpers, 0);
20 +
21 + credential_init(c);
22 +}
23 +
24 +static void credential_describe(struct credential *c, struct strbuf *out)
25 +{
26 + if (!c->protocol)
27 + return;
28 + strbuf_addf(out, "%s://", c->protocol);
29 + if (c->username && *c->username)
30 + strbuf_addf(out, "%s@", c->username);
31 + if (c->host)
32 + strbuf_addstr(out, c->host);
33 + if (c->path)
34 + strbuf_addf(out, "/%s", c->path);
35 +}
36 +
37 +static char *credential_ask_one(const char *what, struct credential *c)
38 +{
39 + struct strbuf desc = STRBUF_INIT;
40 + struct strbuf prompt = STRBUF_INIT;
41 + char *r;
42 +
43 + credential_describe(c, &desc);
44 + if (desc.len)
45 + strbuf_addf(&prompt, "%s for '%s': ", what, desc.buf);
46 + else
47 + strbuf_addf(&prompt, "%s: ", what);
48 +
49 + /* FIXME: for usernames, we should do something less magical that
50 + * actually echoes the characters. However, we need to read from
51 + * /dev/tty and not stdio, which is not portable (but getpass will do
52 + * it for us). http.c uses the same workaround. */
53 + r = git_getpass(prompt.buf);
54 +
55 + strbuf_release(&desc);
56 + strbuf_release(&prompt);
57 + return xstrdup(r);
58 +}
59 +
60 +static void credential_getpass(struct credential *c)
61 +{
62 + if (!c->username)
63 + c->username = credential_ask_one("Username", c);
64 + if (!c->password)
65 + c->password = credential_ask_one("Password", c);
66 +}
67 +
68 +int credential_read(struct credential *c, FILE *fp)
69 +{
70 + struct strbuf line = STRBUF_INIT;
71 +
72 + while (strbuf_getline(&line, fp, '\n') != EOF) {
73 + char *key = line.buf;
74 + char *value = strchr(key, '=');
75 +
76 + if (!line.len)
77 + break;
78 +
79 + if (!value) {
80 + warning("invalid credential line: %s", key);
81 + strbuf_release(&line);
82 + return -1;
83 + }
84 + *value++ = '\0';
85 +
86 + if (!strcmp(key, "username")) {
87 + free(c->username);
88 + c->username = xstrdup(value);
89 + } else if (!strcmp(key, "password")) {
90 + free(c->password);
91 + c->password = xstrdup(value);
92 + } else if (!strcmp(key, "protocol")) {
93 + free(c->protocol);
94 + c->protocol = xstrdup(value);
95 + } else if (!strcmp(key, "host")) {
96 + free(c->host);
97 + c->host = xstrdup(value);
98 + } else if (!strcmp(key, "path")) {
99 + free(c->path);
100 + c->path = xstrdup(value);
101 + }
102 + /*
103 + * Ignore other lines; we don't know what they mean, but
104 + * this future-proofs us when later versions of git do
105 + * learn new lines, and the helpers are updated to match.
106 + */
107 + }
108 +
109 + strbuf_release(&line);
110 + return 0;
111 +}
112 +
113 +static void credential_write_item(FILE *fp, const char *key, const char *value)
114 +{
115 + if (!value)
116 + return;
117 + fprintf(fp, "%s=%s\n", key, value);
118 +}
119 +
120 +static void credential_write(const struct credential *c, FILE *fp)
121 +{
122 + credential_write_item(fp, "protocol", c->protocol);
123 + credential_write_item(fp, "host", c->host);
124 + credential_write_item(fp, "path", c->path);
125 + credential_write_item(fp, "username", c->username);
126 + credential_write_item(fp, "password", c->password);
127 +}
128 +
129 +static int run_credential_helper(struct credential *c,
130 + const char *cmd,
131 + int want_output)
132 +{
133 + struct child_process helper;
134 + const char *argv[] = { NULL, NULL };
135 + FILE *fp;
136 +
137 + memset(&helper, 0, sizeof(helper));
138 + argv[0] = cmd;
139 + helper.argv = argv;
140 + helper.use_shell = 1;
141 + helper.in = -1;
142 + if (want_output)
143 + helper.out = -1;
144 + else
145 + helper.no_stdout = 1;
146 +
147 + if (start_command(&helper) < 0)
148 + return -1;
149 +
150 + fp = xfdopen(helper.in, "w");
151 + credential_write(c, fp);
152 + fclose(fp);
153 +
154 + if (want_output) {
155 + int r;
156 + fp = xfdopen(helper.out, "r");
157 + r = credential_read(c, fp);
158 + fclose(fp);
159 + if (r < 0) {
160 + finish_command(&helper);
161 + return -1;
162 + }
163 + }
164 +
165 + if (finish_command(&helper))
166 + return -1;
167 + return 0;
168 +}
169 +
170 +static int credential_do(struct credential *c, const char *helper,
171 + const char *operation)
172 +{
173 + struct strbuf cmd = STRBUF_INIT;
174 + int r;
175 +
176 + if (helper[0] == '!')
177 + strbuf_addstr(&cmd, helper + 1);
178 + else if (is_absolute_path(helper))
179 + strbuf_addstr(&cmd, helper);
180 + else
181 + strbuf_addf(&cmd, "git credential-%s", helper);
182 +
183 + strbuf_addf(&cmd, " %s", operation);
184 + r = run_credential_helper(c, cmd.buf, !strcmp(operation, "get"));
185 +
186 + strbuf_release(&cmd);
187 + return r;
188 +}
189 +
190 +void credential_fill(struct credential *c)
191 +{
192 + int i;
193 +
194 + if (c->username && c->password)
195 + return;
196 +
197 + for (i = 0; i < c->helpers.nr; i++) {
198 + credential_do(c, c->helpers.items[i].string, "get");
199 + if (c->username && c->password)
200 + return;
201 + }
202 +
203 + credential_getpass(c);
204 + if (!c->username && !c->password)
205 + die("unable to get password from user");
206 +}
207 +
208 +void credential_approve(struct credential *c)
209 +{
210 + int i;
211 +
212 + if (c->approved)
213 + return;
214 + if (!c->username || !c->password)
215 + return;
216 +
217 + for (i = 0; i < c->helpers.nr; i++)
218 + credential_do(c, c->helpers.items[i].string, "store");
219 + c->approved = 1;
220 +}
221 +
222 +void credential_reject(struct credential *c)
223 +{
224 + int i;
225 +
226 + for (i = 0; i < c->helpers.nr; i++)
227 + credential_do(c, c->helpers.items[i].string, "erase");
228 +
229 + free(c->username);
230 + c->username = NULL;
231 + free(c->password);
232 + c->password = NULL;
233 + c->approved = 0;
234 +}
credential.h new
+28
@@ -0,0 +1,28 @@
1 +#ifndef CREDENTIAL_H
2 +#define CREDENTIAL_H
3 +
4 +#include "string-list.h"
5 +
6 +struct credential {
7 + struct string_list helpers;
8 + unsigned approved:1;
9 +
10 + char *username;
11 + char *password;
12 + char *protocol;
13 + char *host;
14 + char *path;
15 +};
16 +
17 +#define CREDENTIAL_INIT { STRING_LIST_INIT_DUP }
18 +
19 +void credential_init(struct credential *);
20 +void credential_clear(struct credential *);
21 +
22 +void credential_fill(struct credential *);
23 +void credential_approve(struct credential *);
24 +void credential_reject(struct credential *);
25 +
26 +int credential_read(struct credential *, FILE *);
27 +
28 +#endif /* CREDENTIAL_H */
t/lib-credential.sh new
+33
@@ -0,0 +1,33 @@
1 +#!/bin/sh
2 +
3 +# Try a set of credential helpers; the expected stdin,
4 +# stdout and stderr should be provided on stdin,
5 +# separated by "--".
6 +check() {
7 + read_chunk >stdin &&
8 + read_chunk >expect-stdout &&
9 + read_chunk >expect-stderr &&
10 + test-credential "$@" <stdin >stdout 2>stderr &&
11 + test_cmp expect-stdout stdout &&
12 + test_cmp expect-stderr stderr
13 +}
14 +
15 +read_chunk() {
16 + while read line; do
17 + case "$line" in
18 + --) break ;;
19 + *) echo "$line" ;;
20 + esac
21 + done
22 +}
23 +
24 +
25 +cat >askpass <<\EOF
26 +#!/bin/sh
27 +echo >&2 askpass: $*
28 +what=`echo $1 | cut -d" " -f1 | tr A-Z a-z | tr -cd a-z`
29 +echo "askpass-$what"
30 +EOF
31 +chmod +x askpass
32 +GIT_ASKPASS="$PWD/askpass"
33 +export GIT_ASKPASS
t/t0300-credentials.sh new
+195
@@ -0,0 +1,195 @@
1 +#!/bin/sh
2 +
3 +test_description='basic credential helper tests'
4 +. ./test-lib.sh
5 +. "$TEST_DIRECTORY"/lib-credential.sh
6 +
7 +test_expect_success 'setup helper scripts' '
8 + cat >dump <<-\EOF &&
9 + whoami=`echo $0 | sed s/.*git-credential-//`
10 + echo >&2 "$whoami: $*"
11 + while IFS== read key value; do
12 + echo >&2 "$whoami: $key=$value"
13 + eval "$key=$value"
14 + done
15 + EOF
16 +
17 + cat >git-credential-useless <<-\EOF &&
18 + #!/bin/sh
19 + . ./dump
20 + exit 0
21 + EOF
22 + chmod +x git-credential-useless &&
23 +
24 + cat >git-credential-verbatim <<-\EOF &&
25 + #!/bin/sh
26 + user=$1; shift
27 + pass=$1; shift
28 + . ./dump
29 + test -z "$user" || echo username=$user
30 + test -z "$pass" || echo password=$pass
31 + EOF
32 + chmod +x git-credential-verbatim &&
33 +
34 + PATH="$PWD:$PATH"
35 +'
36 +
37 +test_expect_success 'credential_fill invokes helper' '
38 + check fill "verbatim foo bar" <<-\EOF
39 + --
40 + username=foo
41 + password=bar
42 + --
43 + verbatim: get
44 + EOF
45 +'
46 +
47 +test_expect_success 'credential_fill invokes multiple helpers' '
48 + check fill useless "verbatim foo bar" <<-\EOF
49 + --
50 + username=foo
51 + password=bar
52 + --
53 + useless: get
54 + verbatim: get
55 + EOF
56 +'
57 +
58 +test_expect_success 'credential_fill stops when we get a full response' '
59 + check fill "verbatim one two" "verbatim three four" <<-\EOF
60 + --
61 + username=one
62 + password=two
63 + --
64 + verbatim: get
65 + EOF
66 +'
67 +
68 +test_expect_success 'credential_fill continues through partial response' '
69 + check fill "verbatim one \"\"" "verbatim two three" <<-\EOF
70 + --
71 + username=two
72 + password=three
73 + --
74 + verbatim: get
75 + verbatim: get
76 + verbatim: username=one
77 + EOF
78 +'
79 +
80 +test_expect_success 'credential_fill passes along metadata' '
81 + check fill "verbatim one two" <<-\EOF
82 + protocol=ftp
83 + host=example.com
84 + path=foo.git
85 + --
86 + username=one
87 + password=two
88 + --
89 + verbatim: get
90 + verbatim: protocol=ftp
91 + verbatim: host=example.com
92 + verbatim: path=foo.git
93 + EOF
94 +'
95 +
96 +test_expect_success 'credential_approve calls all helpers' '
97 + check approve useless "verbatim one two" <<-\EOF
98 + username=foo
99 + password=bar
100 + --
101 + --
102 + useless: store
103 + useless: username=foo
104 + useless: password=bar
105 + verbatim: store
106 + verbatim: username=foo
107 + verbatim: password=bar
108 + EOF
109 +'
110 +
111 +test_expect_success 'do not bother storing password-less credential' '
112 + check approve useless <<-\EOF
113 + username=foo
114 + --
115 + --
116 + EOF
117 +'
118 +
119 +
120 +test_expect_success 'credential_reject calls all helpers' '
121 + check reject useless "verbatim one two" <<-\EOF
122 + username=foo
123 + password=bar
124 + --
125 + --
126 + useless: erase
127 + useless: username=foo
128 + useless: password=bar
129 + verbatim: erase
130 + verbatim: username=foo
131 + verbatim: password=bar
132 + EOF
133 +'
134 +
135 +test_expect_success 'usernames can be preserved' '
136 + check fill "verbatim \"\" three" <<-\EOF
137 + username=one
138 + --
139 + username=one
140 + password=three
141 + --
142 + verbatim: get
143 + verbatim: username=one
144 + EOF
145 +'
146 +
147 +test_expect_success 'usernames can be overridden' '
148 + check fill "verbatim two three" <<-\EOF
149 + username=one
150 + --
151 + username=two
152 + password=three
153 + --
154 + verbatim: get
155 + verbatim: username=one
156 + EOF
157 +'
158 +
159 +test_expect_success 'do not bother completing already-full credential' '
160 + check fill "verbatim three four" <<-\EOF
161 + username=one
162 + password=two
163 + --
164 + username=one
165 + password=two
166 + --
167 + EOF
168 +'
169 +
170 +# We can't test the basic terminal password prompt here because
171 +# getpass() tries too hard to find the real terminal. But if our
172 +# askpass helper is run, we know the internal getpass is working.
173 +test_expect_success 'empty helper list falls back to internal getpass' '
174 + check fill <<-\EOF
175 + --
176 + username=askpass-username
177 + password=askpass-password
178 + --
179 + askpass: Username:
180 + askpass: Password:
181 + EOF
182 +'
183 +
184 +test_expect_success 'internal getpass does not ask for known username' '
185 + check fill <<-\EOF
186 + username=foo
187 + --
188 + username=foo
189 + password=askpass-password
190 + --
191 + askpass: Password:
192 + EOF
193 +'
194 +
195 +test_done
test-credential.c new
+38
@@ -0,0 +1,38 @@
1 +#include "cache.h"
2 +#include "credential.h"
3 +#include "string-list.h"
4 +
5 +static const char usage_msg[] =
6 +"test-credential <fill|approve|reject> [helper...]";
7 +
8 +int main(int argc, const char **argv)
9 +{
10 + const char *op;
11 + struct credential c = CREDENTIAL_INIT;
12 + int i;
13 +
14 + op = argv[1];
15 + if (!op)
16 + usage(usage_msg);
17 + for (i = 2; i < argc; i++)
18 + string_list_append(&c.helpers, argv[i]);
19 +
20 + if (credential_read(&c, stdin) < 0)
21 + die("unable to read credential from stdin");
22 +
23 + if (!strcmp(op, "fill")) {
24 + credential_fill(&c);
25 + if (c.username)
26 + printf("username=%s\n", c.username);
27 + if (c.password)
28 + printf("password=%s\n", c.password);
29 + }
30 + else if (!strcmp(op, "approve"))
31 + credential_approve(&c);
32 + else if (!strcmp(op, "reject"))
33 + credential_reject(&c);
34 + else
35 + usage(usage_msg);
36 +
37 + return 0;
38 +}