4 #include "string-list.h"
8 * The credentials API provides an abstracted way of gathering username and
9 * password credentials from the user.
15 * +-----------------------+
16 * | Git code (C) |--- to server requiring --->
18 * |.......................|
19 * | C credential API |--- prompt ---> User
20 * +-----------------------+
24 * +-----------------------+
25 * | Git credential helper |
26 * +-----------------------+
29 * The Git code (typically a remote-helper) will call the C API to obtain
30 * credential data like a login/password pair (credential_fill). The
31 * API will itself call a remote helper (e.g. "git credential-cache" or
32 * "git credential-store") that may retrieve credential data from a
33 * store. If the credential helper cannot find the information, the C API
34 * will prompt the user. Then, the caller of the API takes care of
35 * contacting the server, and does the actual authentication.
40 * The credential C API is meant to be called by Git code which needs to
41 * acquire or store a credential. It is centered around an object
42 * representing a single credential and provides three basic operations:
43 * fill (acquire credentials by calling helpers and/or prompting the user),
44 * approve (mark a credential as successfully used so that it can be stored
45 * for later use), and reject (mark a credential as unsuccessful so that it
46 * can be erased from any persistent storage).
51 * The example below shows how the functions of the credential API could be
52 * used to login to a fictitious "foo" service on a remote host:
54 * -----------------------------------------------------------------------
55 * int foo_login(struct foo_connection *f)
58 * // Create a credential with some context; we don't yet know the
59 * // username or password.
61 * struct credential c = CREDENTIAL_INIT;
62 * c.protocol = xstrdup("foo");
63 * c.host = xstrdup(f->hostname);
65 * // Fill in the username and password fields by contacting
66 * // helpers and/or asking the user. The function will die if it
68 * credential_fill(&c);
70 * // Otherwise, we have a username and password. Try to use it.
72 * status = send_foo_login(f, c.username, c.password);
75 * // It worked. Store the credential for later use.
76 * credential_accept(&c);
79 * // Erase the credential from storage so we don't try it again.
80 * credential_reject(&c);
83 * // Some other error occurred. We don't know if the
84 * // credential is good or bad, so report nothing to the
85 * // credential subsystem.
88 * // Free any associated resources.
89 * credential_clear(&c);
93 * -----------------------------------------------------------------------
98 * This struct represents a single username/password combination
99 * along with any associated context. All string fields should be
100 * heap-allocated (or NULL if they are not known or not applicable).
101 * The meaning of the individual context fields is the same as
102 * their counterparts in the helper protocol.
104 * This struct should always be initialized with `CREDENTIAL_INIT` or
110 * A `string_list` of helpers. Each string specifies an external
111 * helper which will be run, in order, to either acquire or store
112 * credentials. This list is filled-in by the API functions
113 * according to the corresponding configuration variables before
114 * consulting helpers, so there usually is no need for a caller to
115 * modify the helpers field at all.
117 struct string_list helpers
;
120 * A `strvec` of WWW-Authenticate header values. Each string
121 * is the value of a WWW-Authenticate header in an HTTP response,
122 * in the order they were received in the response.
124 struct strvec wwwauth_headers
;
127 * Internal use only. Keeps track of if we previously matched against a
128 * WWW-Authenticate header line in order to re-fold future continuation
129 * lines into one value.
131 unsigned header_is_last_match
:1;
137 username_from_proto
:1;
146 #define CREDENTIAL_INIT { \
147 .helpers = STRING_LIST_INIT_DUP, \
148 .wwwauth_headers = STRVEC_INIT, \
151 /* Initialize a credential structure, setting all fields to empty. */
152 void credential_init(struct credential
*);
155 * Free any resources associated with the credential structure, returning
156 * it to a pristine initialized state.
158 void credential_clear(struct credential
*);
161 * Instruct the credential subsystem to fill the username and
162 * password fields of the passed credential struct by first
163 * consulting helpers, then asking the user. After this function
164 * returns, the username and password fields of the credential are
165 * guaranteed to be non-NULL. If an error occurs, the function will
168 void credential_fill(struct credential
*);
171 * Inform the credential subsystem that the provided credentials
172 * were successfully used for authentication. This will cause the
173 * credential subsystem to notify any helpers of the approval, so
174 * that they may store the result to be used again. Any errors
175 * from helpers are ignored.
177 void credential_approve(struct credential
*);
180 * Inform the credential subsystem that the provided credentials
181 * have been rejected. This will cause the credential subsystem to
182 * notify any helpers of the rejection (which allows them, for
183 * example, to purge the invalid credentials from storage). It
184 * will also free() the username and password fields of the
185 * credential and set them to NULL (readying the credential for
186 * another call to `credential_fill`). Any errors from helpers are
189 void credential_reject(struct credential
*);
191 int credential_read(struct credential
*, FILE *);
192 void credential_write(const struct credential
*, FILE *);
195 * Parse a url into a credential struct, replacing any existing contents.
197 * If the url can't be parsed (e.g., a missing "proto://" component), the
198 * resulting credential will be empty and the function will return an
199 * error (even in the "gently" form).
201 * If we encounter a component which cannot be represented as a credential
202 * value (e.g., because it contains a newline), the "gently" form will return
203 * an error but leave the broken state in the credential object for further
204 * examination. The non-gentle form will issue a warning to stderr and return
205 * an empty credential.
207 void credential_from_url(struct credential
*, const char *url
);
208 int credential_from_url_gently(struct credential
*, const char *url
, int quiet
);
210 int credential_match(const struct credential
*want
,
211 const struct credential
*have
);
213 #endif /* CREDENTIAL_H */