credential.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #ifndef CREDENTIAL_H
  2. #define CREDENTIAL_H
  3. #include "string-list.h"
  4. /**
  5. * The credentials API provides an abstracted way of gathering username and
  6. * password credentials from the user.
  7. *
  8. * Typical setup
  9. * -------------
  10. *
  11. * ------------
  12. * +-----------------------+
  13. * | Git code (C) |--- to server requiring --->
  14. * | | authentication
  15. * |.......................|
  16. * | C credential API |--- prompt ---> User
  17. * +-----------------------+
  18. * ^ |
  19. * | pipe |
  20. * | v
  21. * +-----------------------+
  22. * | Git credential helper |
  23. * +-----------------------+
  24. * ------------
  25. *
  26. * The Git code (typically a remote-helper) will call the C API to obtain
  27. * credential data like a login/password pair (credential_fill). The
  28. * API will itself call a remote helper (e.g. "git credential-cache" or
  29. * "git credential-store") that may retrieve credential data from a
  30. * store. If the credential helper cannot find the information, the C API
  31. * will prompt the user. Then, the caller of the API takes care of
  32. * contacting the server, and does the actual authentication.
  33. *
  34. * C API
  35. * -----
  36. *
  37. * The credential C API is meant to be called by Git code which needs to
  38. * acquire or store a credential. It is centered around an object
  39. * representing a single credential and provides three basic operations:
  40. * fill (acquire credentials by calling helpers and/or prompting the user),
  41. * approve (mark a credential as successfully used so that it can be stored
  42. * for later use), and reject (mark a credential as unsuccessful so that it
  43. * can be erased from any persistent storage).
  44. *
  45. * Example
  46. * ~~~~~~~
  47. *
  48. * The example below shows how the functions of the credential API could be
  49. * used to login to a fictitious "foo" service on a remote host:
  50. *
  51. * -----------------------------------------------------------------------
  52. * int foo_login(struct foo_connection *f)
  53. * {
  54. * int status;
  55. * // Create a credential with some context; we don't yet know the
  56. * // username or password.
  57. *
  58. * struct credential c = CREDENTIAL_INIT;
  59. * c.protocol = xstrdup("foo");
  60. * c.host = xstrdup(f->hostname);
  61. *
  62. * // Fill in the username and password fields by contacting
  63. * // helpers and/or asking the user. The function will die if it
  64. * // fails.
  65. * credential_fill(&c);
  66. *
  67. * // Otherwise, we have a username and password. Try to use it.
  68. *
  69. * status = send_foo_login(f, c.username, c.password);
  70. * switch (status) {
  71. * case FOO_OK:
  72. * // It worked. Store the credential for later use.
  73. * credential_accept(&c);
  74. * break;
  75. * case FOO_BAD_LOGIN:
  76. * // Erase the credential from storage so we don't try it again.
  77. * credential_reject(&c);
  78. * break;
  79. * default:
  80. * // Some other error occurred. We don't know if the
  81. * // credential is good or bad, so report nothing to the
  82. * // credential subsystem.
  83. * }
  84. *
  85. * // Free any associated resources.
  86. * credential_clear(&c);
  87. *
  88. * return status;
  89. * }
  90. * -----------------------------------------------------------------------
  91. */
  92. /**
  93. * This struct represents a single username/password combination
  94. * along with any associated context. All string fields should be
  95. * heap-allocated (or NULL if they are not known or not applicable).
  96. * The meaning of the individual context fields is the same as
  97. * their counterparts in the helper protocol.
  98. *
  99. * This struct should always be initialized with `CREDENTIAL_INIT` or
  100. * `credential_init`.
  101. */
  102. struct credential {
  103. /**
  104. * A `string_list` of helpers. Each string specifies an external
  105. * helper which will be run, in order, to either acquire or store
  106. * credentials. This list is filled-in by the API functions
  107. * according to the corresponding configuration variables before
  108. * consulting helpers, so there usually is no need for a caller to
  109. * modify the helpers field at all.
  110. */
  111. struct string_list helpers;
  112. unsigned approved:1,
  113. configured:1,
  114. quit:1,
  115. use_http_path:1,
  116. username_from_proto:1;
  117. char *username;
  118. char *password;
  119. char *protocol;
  120. char *host;
  121. char *path;
  122. };
  123. #define CREDENTIAL_INIT { STRING_LIST_INIT_DUP }
  124. /* Initialize a credential structure, setting all fields to empty. */
  125. void credential_init(struct credential *);
  126. /**
  127. * Free any resources associated with the credential structure, returning
  128. * it to a pristine initialized state.
  129. */
  130. void credential_clear(struct credential *);
  131. /**
  132. * Instruct the credential subsystem to fill the username and
  133. * password fields of the passed credential struct by first
  134. * consulting helpers, then asking the user. After this function
  135. * returns, the username and password fields of the credential are
  136. * guaranteed to be non-NULL. If an error occurs, the function will
  137. * die().
  138. */
  139. void credential_fill(struct credential *);
  140. /**
  141. * Inform the credential subsystem that the provided credentials
  142. * were successfully used for authentication. This will cause the
  143. * credential subsystem to notify any helpers of the approval, so
  144. * that they may store the result to be used again. Any errors
  145. * from helpers are ignored.
  146. */
  147. void credential_approve(struct credential *);
  148. /**
  149. * Inform the credential subsystem that the provided credentials
  150. * have been rejected. This will cause the credential subsystem to
  151. * notify any helpers of the rejection (which allows them, for
  152. * example, to purge the invalid credentials from storage). It
  153. * will also free() the username and password fields of the
  154. * credential and set them to NULL (readying the credential for
  155. * another call to `credential_fill`). Any errors from helpers are
  156. * ignored.
  157. */
  158. void credential_reject(struct credential *);
  159. int credential_read(struct credential *, FILE *);
  160. void credential_write(const struct credential *, FILE *);
  161. /*
  162. * Parse a url into a credential struct, replacing any existing contents.
  163. *
  164. * If the url can't be parsed (e.g., a missing "proto://" component), the
  165. * resulting credential will be empty and the function will return an
  166. * error (even in the "gently" form).
  167. *
  168. * If we encounter a component which cannot be represented as a credential
  169. * value (e.g., because it contains a newline), the "gently" form will return
  170. * an error but leave the broken state in the credential object for further
  171. * examination. The non-gentle form will issue a warning to stderr and return
  172. * an empty credential.
  173. */
  174. void credential_from_url(struct credential *, const char *url);
  175. int credential_from_url_gently(struct credential *, const char *url, int quiet);
  176. int credential_match(const struct credential *want,
  177. const struct credential *have);
  178. #endif /* CREDENTIAL_H */