1 /*
2  * Copyright (C) the libgit2 contributors. All rights reserved.
3  *
4  * This file is part of libgit2, distributed under the GNU GPL v2 with
5  * a Linking Exception. For full terms see the included COPYING file.
6  */
7 /**
8  * License: GPL-2.0(Linking Exception)
9  */
10 module libgit2.credential;
11 
12 
13 private static import libgit2.sys.credential;
14 private import libgit2.common: GIT_EXTERN;
15 
16 /*
17  * @file git2/credential.h
18  * @brief Git authentication & credential management
19  * @defgroup git_credential Authentication & credential management
20  * @ingroup Git
21  * @{
22  */
23 extern (C):
24 nothrow @nogc:
25 public:
26 
27 /**
28  * Supported credential types
29  *
30  * This represents the various types of authentication methods supported by
31  * the library.
32  */
33 enum git_credential_t
34 {
35 	/**
36 	 * A vanilla user/password request
37 	 * @see git_credential_userpass_plaintext_new
38 	 */
39 	GIT_CREDENTIAL_USERPASS_PLAINTEXT = 1u << 0,
40 
41 	/**
42 	 * An SSH key-based authentication request
43 	 * @see git_credential_ssh_key_new
44 	 */
45 	GIT_CREDENTIAL_SSH_KEY = 1u << 1,
46 
47 	/**
48 	 * An SSH key-based authentication request, with a custom signature
49 	 * @see git_credential_ssh_custom_new
50 	 */
51 	GIT_CREDENTIAL_SSH_CUSTOM = 1u << 2,
52 
53 	/**
54 	 * An NTLM/Negotiate-based authentication request.
55 	 * @see git_credential_default
56 	 */
57 	GIT_CREDENTIAL_DEFAULT = 1u << 3,
58 
59 	/**
60 	 * An SSH interactive authentication request
61 	 * @see git_credential_ssh_interactive_new
62 	 */
63 	GIT_CREDENTIAL_SSH_INTERACTIVE = 1u << 4,
64 
65 	/**
66 	 * Username-only authentication request
67 	 *
68 	 * Used as a pre-authentication step if the underlying transport
69 	 * (eg. SSH, with no username in its URL) does not know which username
70 	 * to use.
71 	 *
72 	 * @see git_credential_username_new
73 	 */
74 	GIT_CREDENTIAL_USERNAME = 1u << 5,
75 
76 	/**
77 	 * An SSH key-based authentication request
78 	 *
79 	 * Allows credentials to be read from memory instead of files.
80 	 * Note that because of differences in crypto backend support, it might
81 	 * not be functional.
82 	 *
83 	 * @see git_credential_ssh_key_memory_new
84 	 */
85 	GIT_CREDENTIAL_SSH_MEMORY = 1u << 6,
86 }
87 
88 //Declaration name in C language
89 enum
90 {
91 	GIT_CREDENTIAL_USERPASS_PLAINTEXT = .git_credential_t.GIT_CREDENTIAL_USERPASS_PLAINTEXT,
92 	GIT_CREDENTIAL_SSH_KEY = .git_credential_t.GIT_CREDENTIAL_SSH_KEY,
93 	GIT_CREDENTIAL_SSH_CUSTOM = .git_credential_t.GIT_CREDENTIAL_SSH_CUSTOM,
94 	GIT_CREDENTIAL_DEFAULT = .git_credential_t.GIT_CREDENTIAL_DEFAULT,
95 	GIT_CREDENTIAL_SSH_INTERACTIVE = .git_credential_t.GIT_CREDENTIAL_SSH_INTERACTIVE,
96 	GIT_CREDENTIAL_USERNAME = .git_credential_t.GIT_CREDENTIAL_USERNAME,
97 	GIT_CREDENTIAL_SSH_MEMORY = .git_credential_t.GIT_CREDENTIAL_SSH_MEMORY,
98 }
99 
100 /**
101  * The base structure for all credential types
102  */
103 alias git_credential = libgit2.sys.credential.git_credential;
104 
105 alias git_credential_userpass_plaintext = libgit2.sys.credential.git_credential_userpass_plaintext;
106 
107 /**
108  * Username-only credential information
109  */
110 alias git_credential_username = libgit2.sys.credential.git_credential_username;
111 
112 /**
113  * A key for NTLM/Kerberos "default" credentials
114  */
115 alias git_credential_default = .git_credential;
116 
117 /**
118  * A ssh key from disk
119  */
120 alias git_credential_ssh_key = libgit2.sys.credential.git_credential_ssh_key;
121 
122 /**
123  * Keyboard-interactive based ssh authentication
124  */
125 alias git_credential_ssh_interactive = libgit2.sys.credential.git_credential_ssh_interactive;
126 
127 /**
128  * A key with a custom signature function
129  */
130 alias git_credential_ssh_custom = libgit2.sys.credential.git_credential_ssh_custom;
131 
132 /**
133  * Credential acquisition callback.
134  *
135  * This callback is usually involved any time another system might need
136  * authentication. As such, you are expected to provide a valid
137  * git_credential object back, depending on allowed_types (a
138  * git_credential_t bitmask).
139  *
140  * Note that most authentication details are your responsibility - this
141  * callback will be called until the authentication succeeds, or you report
142  * an error. As such, it's easy to get in a loop if you fail to stop providing
143  * the same incorrect credentials.
144  *
145  * Returns: 0 for success, < 0 to indicate an error, > 0 to indicate no credential was acquired
146  */
147 /*
148  * Params:
149  *      out_ = The newly created credential object.
150  *      url = The resource for which we are demanding a credential.
151  *      username_from_url = The username that was embedded in a "user\@host" remote url, or null if not included.
152  *      allowed_types = A bitmask stating which credential types are OK to return.
153  *      payload = The payload provided when specifying this callback.
154  */
155 alias git_credential_acquire_cb = int function(.git_credential** out_, const (char)* url, const (char)* username_from_url, uint allowed_types, void* payload);
156 
157 /**
158  * Free a credential.
159  *
160  * This is only necessary if you own the object; that is, if you are a
161  * transport.
162  *
163  * Params:
164  *      cred = the object to free
165  */
166 @GIT_EXTERN
167 void git_credential_free(.git_credential* cred);
168 
169 /**
170  * Check whether a credential object contains username information.
171  *
172  * Params:
173  *      cred = object to check
174  *
175  * Returns: 1 if the credential object has non-null username, 0 otherwise
176  */
177 @GIT_EXTERN
178 int git_credential_has_username(.git_credential* cred);
179 
180 /**
181  * Return the username associated with a credential object.
182  *
183  * Params:
184  *      cred = object to check
185  *
186  * Returns: the credential username, or null if not applicable
187  */
188 @GIT_EXTERN
189 const (char)* git_credential_get_username(.git_credential* cred);
190 
191 /**
192  * Create a new plain-text username and password credential object.
193  * The supplied credential parameter will be internally duplicated.
194  *
195  * Params:
196  *      out_ = The newly created credential object.
197  *      username = The username of the credential.
198  *      password = The password of the credential.
199  *
200  * Returns: 0 for success or an error code for failure
201  */
202 @GIT_EXTERN
203 int git_credential_userpass_plaintext_new(.git_credential** out_, const (char)* username, const (char)* password);
204 
205 /**
206  * Create a "default" credential usable for Negotiate mechanisms like NTLM
207  * or Kerberos authentication.
208  *
209  * Params:
210  *      out_ = The newly created credential object.
211  *
212  * Returns: 0 for success or an error code for failure
213  */
214 @GIT_EXTERN
215 int git_credential_default_new(.git_credential** out_);
216 
217 /**
218  * Create a credential to specify a username.
219  *
220  * This is used with ssh authentication to query for the username if
221  * none is specified in the url.
222  *
223  * Params:
224  *      out_ = The newly created credential object.
225  *      username = The username to authenticate with
226  *
227  * Returns: 0 for success or an error code for failure
228  */
229 @GIT_EXTERN
230 int git_credential_username_new(.git_credential** out_, const (char)* username);
231 
232 /**
233  * Create a new passphrase-protected ssh key credential object.
234  * The supplied credential parameter will be internally duplicated.
235  *
236  * Params:
237  *      out_ = The newly created credential object.
238  *      username = username to use to authenticate
239  *      publickey = The path to the public key of the credential.
240  *      privatekey = The path to the private key of the credential.
241  *      passphrase = The passphrase of the credential.
242  *
243  * Returns: 0 for success or an error code for failure
244  */
245 @GIT_EXTERN
246 int git_credential_ssh_key_new(.git_credential** out_, const (char)* username, const (char)* publickey, const (char)* privatekey, const (char)* passphrase);
247 
248 /**
249  * Create a new ssh key credential object reading the keys from memory.
250  *
251  * Params:
252  *      out_ = The newly created credential object.
253  *      username = username to use to authenticate.
254  *      publickey = The public key of the credential.
255  *      privatekey = The private key of the credential.
256  *      passphrase = The passphrase of the credential.
257  *
258  * Returns: 0 for success or an error code for failure
259  */
260 @GIT_EXTERN
261 int git_credential_ssh_key_memory_new(.git_credential** out_, const (char)* username, const (char)* publickey, const (char)* privatekey, const (char)* passphrase);
262 
263 /*
264  * If the user hasn't included libssh2.h before git2.h, we need to
265  * define a few types for the callback signatures.
266  */
267 version (LIBSSH2_VERSION) {
268 } else {
269 	struct _LIBSSH2_SESSION;
270 	struct _LIBSSH2_USERAUTH_KBDINT_PROMPT;
271 	struct _LIBSSH2_USERAUTH_KBDINT_RESPONSE;
272 	alias LIBSSH2_SESSION = _LIBSSH2_SESSION;
273 	alias LIBSSH2_USERAUTH_KBDINT_PROMPT = _LIBSSH2_USERAUTH_KBDINT_PROMPT;
274 	alias LIBSSH2_USERAUTH_KBDINT_RESPONSE = _LIBSSH2_USERAUTH_KBDINT_RESPONSE;
275 }
276 
277 alias git_credential_ssh_interactive_cb = void function(const (char)* name, int name_len, const (char)* instruction, int instruction_len, int num_prompts, const (.LIBSSH2_USERAUTH_KBDINT_PROMPT)* prompts, .LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses, void** abstract_);
278 
279 /**
280  * Create a new ssh keyboard-interactive based credential object.
281  * The supplied credential parameter will be internally duplicated.
282  *
283  * Params:
284  *      out_ = The newly created credential object.
285  *      username = Username to use to authenticate.
286  *      prompt_callback = The callback method used for prompts.
287  *      payload = Additional data to pass to the callback.
288  *
289  * Returns: 0 for success or an error code for failure.
290  */
291 @GIT_EXTERN
292 int git_credential_ssh_interactive_new(.git_credential** out_, const (char)* username, .git_credential_ssh_interactive_cb prompt_callback, void* payload);
293 
294 /**
295  * Create a new ssh key credential object used for querying an ssh-agent.
296  * The supplied credential parameter will be internally duplicated.
297  *
298  * Params:
299  *      out_ = The newly created credential object.
300  *      username = username to use to authenticate
301  *
302  * Returns: 0 for success or an error code for failure
303  */
304 @GIT_EXTERN
305 int git_credential_ssh_key_from_agent(.git_credential** out_, const (char)* username);
306 
307 alias git_credential_sign_cb = int function(.LIBSSH2_SESSION* session, ubyte** sig, size_t* sig_len, const (ubyte)* data, size_t data_len, void** abstract_);
308 
309 /**
310  * Create an ssh key credential with a custom signing function.
311  *
312  * This lets you use your own function to sign the challenge.
313  *
314  * This function and its credential type is provided for completeness
315  * and wraps `libssh2_userauth_publickey()`, which is undocumented.
316  *
317  * The supplied credential parameter will be internally duplicated.
318  *
319  * Params:
320  *      out_ = The newly created credential object.
321  *      username = username to use to authenticate
322  *      publickey = The bytes of the public key.
323  *      publickey_len = The length of the public key in bytes.
324  *      sign_callback = The callback method to sign the data during the challenge.
325  *      payload = Additional data to pass to the callback.
326  *
327  * Returns: 0 for success or an error code for failure
328  */
329 @GIT_EXTERN
330 int git_credential_ssh_custom_new(.git_credential** out_, const (char)* username, const (char)* publickey, size_t publickey_len, .git_credential_sign_cb sign_callback, void* payload);
331 
332 /* @} */