UNPKG

8.29 kBMarkdownView Raw
1# ghauth
2
3**Create and load persistent GitHub authentication tokens for command-line apps**
4
5[![NPM](https://nodei.co/npm/ghauth.svg)](https://nodei.co/npm/ghauth/)
6
7**Important**
8
9Github deprecated their basic username/password auth api and have [scheduled to sunset it November 13, 2020](https://developer.github.com/changes/2020-02-14-deprecating-oauth-auth-endpoint/). `ghauth` v5.0.0+ supports the new [device auth flow](https://docs.github.com/en/developers/apps/authorizing-oauth-apps#device-flow) but requires some implementation changes and application registration with Github. Review the new API docs and see [Setup](#setup) for a simple upgrade guide between v4 and v5.
10
11## Example usage
12
13```js
14const ghauth = require('ghauth')
15const authOptions = {
16 // provide a clientId from a Github oAuth application registration
17 clientId: '123456',
18
19 // awesome.json within the user's config directory will store the token
20 configName: 'awesome',
21
22 // (optional) whatever GitHub auth scopes you require
23 scopes: [ 'user' ],
24
25 // (optional)
26 userAgent: 'My Awesome App'
27}
28
29const authData = await ghauth(authOptions)
30console.log(authData)
31
32// can also be run with a callback as:
33//
34// ghauth(authOptions, function (err, authData) {
35// console.log(authData)
36// })
37
38```
39
40Will run something like this:
41
42```console
43$ node awesome.js
44 Authorize with GitHub by opening this URL in a browser:
45
46 https://github.com/login/device
47
48 and enter the following User Code:
49 (or press ⏎ to enter a personal access token)
50
51✔ Device flow complete. Manage at https://github.com/settings/connections/applications/123456
52✔ Authorized for rvagg
53Wrote access token to "~/.config/awesome/config.json"
54{
55 token: '24d5dee258c64aef38a66c0c5eca459c379901c2',
56 user: 'rvagg'
57}
58```
59
60Because the token is persisted, the next time you run it there will be no prompts:
61
62```console
63$ node awesome.js
64
65{ user: 'rvagg',
66 token: '24d5dee258c64aef38a66c0c5eca459c379901c2' }
67```
68
69When `authUrl` is configured for a Github enterprise endpoint, it will look more like this:
70
71```console
72$ node awesome.js
73
74GitHub username: rvagg
75GitHub password: ✔✔✔✔✔✔✔✔✔✔✔✔
76GitHub OTP (optional): 669684
77
78{ user: 'rvagg',
79 token: '24d5dee258c64aef38a66c0c5eca459c379901c2' }
80```
81
82## API
83
84<b><code>ghauth(options, callback)</code></b>
85
86The <b><code>options</code></b> argument can have the following properties:
87
88* `clientId` (String, required unless `noDeviceFlow` is `true`): the clientId of your oAuth application on Github. See [setup](#setup) below for more info on creating a Github oAuth application.
89* `configName` (String, required unless `noSave` is `true`): the name of the config you are creating, this is required for saving a `<configName>.json` file into the users config directory with the token created. Note that the **config directory is determined by [application-config](https://github.com/LinusU/node-application-config) and is OS-specific.**
90* `noSave` (Boolean, optional): if you don't want to persist the token to disk, set this to `true` but be aware that you will still be creating a saved token on GitHub that will need cleaning up if you are not persisting the token.
91* `authUrl` (String, optional): defaults to `null` since public Github no longer supports basic auth. Setting `authUrl` will allow you to perform basic authentication with a Github Enterprise instance. This setting is ignored if the `host` of the url is `api.github.com` or `github.com`.
92* `promptName` (String, optional): defaults to `'GitHub Enterprise'`, change this if you are prompting for GHE credentials. Not used for public GH authentication.
93* `scopes` (Array, optional): defaults to `[]`, consult the GitHub [scopes](https://developer.github.com/v3/oauth/#scopes) documentation to see what you may need for your application.
94* `note` (String, optional): defaults to `'Node.js command-line app with ghauth'`, override if you want to save a custom note with the GitHub token (user-visible). Only used with GHE basic authentication.
95* `userAgent` (String, optional): defaults to `'Magic Node.js application that does magic things with ghauth'`, only used for requests to GitHub, override if you have a good reason to do so.
96* `passwordReplaceChar` (String, optional): defaults to `'✔'`, the character echoed when the user inputs their password. Can be set to `''` to silence the output.
97* `noDeviceFlow` (Boolean, optional): disable the Device Flow authentication method. This will prompt users for a personal access token immediately if no existing configuration is found. Only applies when `authUrl` is not used.
98
99The <b><code>callback</code></b> will be called with either an `Error` object describing what went wrong, or a `data` object as the second argument if the auth creation (or cache read) was successful. The shape of the second argument is `{ user:String, token:String }`.
100
101## Setup
102
103Github requires a `clientId` from a Github oAuth Application in order to complete oAuth device flow authentication.
104
1051. Register an "oAuth Application" with Github:
106 - [Personal Account oAuth apps page](https://github.com/settings/developers)
107 - `https://github.com/organizations/${org_name}/settings/applications`: Organization oAuth settings page.
1082. Provide an application name, homepage URL and callback URL. You can make these two URLs the same, since your app will not be using a callback URL with the device flow.
1093. Go to your oAuth application's settings page and take note of the "Client ID" (this will get passed as `clientId` to `ghauth`). You can ignore the "Client Secret" value. It is not used.
110
111The `clientId` is registered by the developer of the tool or CLI, and is baked into the code of your program. Users do not need to set this up, onyl the publisher of the app.
112
113- [Device flow docs](https://docs.github.com/en/developers/apps/authorizing-oauth-apps#device-flow)
114
115### v4 to v5 Upgrade guide
116
117- A `options.clientId` is required to use device flow. Set up an oAuth application to get a `clientId`.
118- the `options.authUrl` now only applies to GitHub enterprise authentication which still only supports basic auth. Only pass this if you intend for GitHub Enterpise authentication.
119- `options.note` is only used for GHE basic auth now. Your oAuth application details serve the purpose of token note.
120- `options.noDeviceFlow` is available to skip the device flow if you are unable to create a `clientId` for some reason, and wish to skip to the personal access token input prompt immediately.
121
122## Contributing
123
124ghauth is an **OPEN Open Source Project**. This means that:
125
126> Individuals making significant and valuable contributions are given commit-access to the project to contribute as they see fit. This project is more like an open wiki than a standard guarded open source project.
127
128See the [CONTRIBUTING.md](https://github.com/rvagg/ghauth/blob/master/CONTRIBUTING.md) file for more details.
129
130### A note about tests
131
132... there are no proper tests yet unfortunately. If you would like to contribute some that would be very helpful! We need to mock the GitHub API to properly test the functionality. Otherwise, testing of this library is done by its use downstream.
133
134### Contributors
135
136ghauth is made possible by the excellent work of the following contributors:
137
138<table><tbody>
139<tr><th align="left">Rod Vagg</th><td><a href="https://github.com/rvagg">GitHub/rvagg</a></td><td><a href="http://twitter.com/rvagg">Twitter/@rvagg</a></td></tr>
140<tr><th align="left">Jeppe Nejsum Madsen</th><td><a href="https://github.com/jeppenejsum">GitHub/jeppenejsum</a></td><td><a href="http://twitter.com/nejsum">Twitter/@nejsum</a></td></tr>
141<tr><th align="left">Max Ogden</th><td><a href="https://github.com/maxogden">GitHub/maxogden</a></td><td><a href="http://twitter.com/maxogden">Twitter/@maxogden</a></td></tr>
142<tr><th align="left">Bret Comnes</th><td><a href="https://github.com/bcomnes">GitHub/bcomnes</a></td><td><a href="http://twitter.com/bcomnes">Twitter/@bcomnes</a></td></tr>
143</tbody></table>
144
145License &amp; copyright
146-----------------------
147
148Copyright (c) 2014 ghauth contributors (listed above).
149
150ghauth is licensed under the MIT license. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE.md file for more details.