|
| 1 | +--- |
| 2 | +title: Authentication |
| 3 | +sidebar_label: Authentication |
| 4 | +--- |
| 5 | + |
| 6 | +Authentication is normally required for [`push`](./push.html)ing to a git repository. |
| 7 | +It may also be required to [`clone`](./clone.html) or [`fetch`](./fetch.html) from a private repository. |
| 8 | +Git does all its authentication using HTTPS Basic Authentication. |
| 9 | +Usually this is straightforward: just specify `username` and `password`. |
| 10 | + |
| 11 | +```js |
| 12 | +await git.push({ |
| 13 | + ...repo, |
| 14 | + username: 'your username', |
| 15 | + password: 'your password' |
| 16 | +}) |
| 17 | +``` |
| 18 | + |
| 19 | +However, there are some things to watch out for. |
| 20 | + |
| 21 | +If you have two-factor authentication (2FA) enabled on your account, you |
| 22 | +probably cannot push or pull using your regular username and password. |
| 23 | +Instead, you may have to create a Personal Access Token (or an App Password in Bitbucket lingo) and use that to authenticate. |
| 24 | +( [Instructions for GitHub](https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/) |
| 25 | +| [Instructions for Bitbucket](https://confluence.atlassian.com/bitbucket/app-passwords-828781300.html) |
| 26 | +| [Instructions for Gitlab](https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html) |
| 27 | +) |
| 28 | + |
| 29 | +```js |
| 30 | +await git.push({ |
| 31 | + ...repo, |
| 32 | + username: 'your username', // Note: username is optional for Github |
| 33 | + token: 'your Personal Access Token' |
| 34 | +}) |
| 35 | +``` |
| 36 | + |
| 37 | +If you are writing a third-party app that interacts with Github/Gitlab/Bitbucket, you may be obtaining |
| 38 | +OAuth2 tokens from the service via a feature like "Login with Github". |
| 39 | +Depending on the OAuth2 token's grants, you can use those tokens for pushing and pulling from git repos as well. |
| 40 | +Unfortunately, all the major git hosting companies have chosen different conventions for converting |
| 41 | +OAuth2 tokens into Basic Authentication headers! Therefore it is necessary to specify which company's |
| 42 | +convention you are interacting with via the `oauth2format` parameter. |
| 43 | +Currently, the following values are understood: |
| 44 | + |
| 45 | +| oauth2format | Basic Auth username | Basic Auth password | |
| 46 | +| ------------ | ------------------- | ------------------- | |
| 47 | +| 'github' | `token` | 'x-oauth-basic' | |
| 48 | +| 'bitbucket' | 'x-token-auth' | `token` | |
| 49 | +| 'gitlab' | 'oauth2' | `token` | |
| 50 | + |
| 51 | +I will gladly accept pull requests to add support for more companies' conventions. |
| 52 | +Here is what using OAuth2 authentication looks like. |
| 53 | +Note when using OAuth2 tokens, you do not include `username` or `password`. |
| 54 | + |
| 55 | +```js |
| 56 | +await git.push({ |
| 57 | + ...repo, |
| 58 | + oauth2format: 'gitlab', |
| 59 | + token: 'your OAuth2 Token' |
| 60 | +}) |
| 61 | +``` |
| 62 | + |
| 63 | +A complete summary of how the four parameters interact to determine the Basic Auth headers is described by the following truth table: |
| 64 | + |
| 65 | +| username | password | token | oauth2format | = Result | |
| 66 | +| -------- | -------- | ----- | ------------ | -------------------------------------------------------------------------------- | |
| 67 | +| | | | | Basic Auth not used | |
| 68 | +| X | | | | Error "Missing token or password" | |
| 69 | +| | X | | | Error "Missing username" | |
| 70 | +| X | X | | | `{authUsername: username, authPassword: password}` | |
| 71 | +| | | X | | `{authUsername: token, authPassword: ''}` (Github's alternative format) | |
| 72 | +| X | | X | | `{authUsername: username, authPassword: token}` | |
| 73 | +| | X | X | | Error "Cannot mix 'password' with 'token'" | |
| 74 | +| X | X | X | | Error "Cannot mix 'password' with 'token'" | |
| 75 | +| | | | X | Error "Missing token" | |
| 76 | +| X | | | X | Error "Cannot mix 'username' with 'oauth2format'. Missing token." | |
| 77 | +| | X | | X | Error "Cannot mix 'password' with 'oauth2format'. Missing token." | |
| 78 | +| X | X | | X | Error "Cannot mix 'username' and 'password' with 'oauth2format'. Missing token." | |
| 79 | +| | | X | X | as described in the oauth2format table above | |
| 80 | +| X | | X | X | Error "Cannot mix 'username' with 'oauth2format' and 'token'" | |
| 81 | +| | X | X | X | Error "Cannot mix 'password' with 'oauth2format' and 'token'" | |
| 82 | +| X | X | X | X | Error "Cannot mix 'username' and 'password' with 'oauth2format' and 'token'" | |
0 commit comments