Giter VIP home page Giter VIP logo

ghauth's Introduction

ghauth

Create and load persistent GitHub authentication tokens for command-line apps

NPM

Important

Github deprecated their basic username/password auth api and have scheduled to sunset it November 13, 2020. ghauth v5.0.0+ supports the new device auth flow but requires some implementation changes and application registration with Github. Review the new API docs and see Setup for a simple upgrade guide between v4 and v5.

Example usage

const ghauth = require('ghauth')
const authOptions = {
  // provide a clientId from a Github oAuth application registration
  clientId: '123456',

  // awesome.json within the user's config directory will store the token
  configName: 'awesome',

  // (optional) whatever GitHub auth scopes you require
  scopes: [ 'user' ],

  // (optional)
  userAgent: 'My Awesome App'
}

const authData = await ghauth(authOptions)
console.log(authData)

// can also be run with a callback as:
//
// ghauth(authOptions, function (err, authData) {
//  console.log(authData)
// })

Will run something like this:

$ node awesome.js
  Authorize with GitHub by opening this URL in a browser:

    https://github.com/login/device

  and enter the following User Code:
  (or press ⏎ to enter a personal access token)

✔ Device flow complete.  Manage at https://github.com/settings/connections/applications/123456
✔ Authorized for rvagg
Wrote access token to "~/.config/awesome/config.json"
{
  token: '24d5dee258c64aef38a66c0c5eca459c379901c2',
  user: 'rvagg'
}

Because the token is persisted, the next time you run it there will be no prompts:

$ node awesome.js

{ user: 'rvagg',
  token: '24d5dee258c64aef38a66c0c5eca459c379901c2' }

When authUrl is configured for a Github enterprise endpoint, it will look more like this:

$ node awesome.js

GitHub username: rvagg
GitHub password: ✔✔✔✔✔✔✔✔✔✔✔✔
GitHub OTP (optional): 669684

{ user: 'rvagg',
  token: '24d5dee258c64aef38a66c0c5eca459c379901c2' }

API

ghauth(options, callback)

The options argument can have the following properties:

  • clientId (String, required unless noDeviceFlow is true): the clientId of your oAuth application on Github. See setup below for more info on creating a Github oAuth application.
  • 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 and is OS-specific.
  • 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.
  • 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.
  • promptName (String, optional): defaults to 'GitHub Enterprise', change this if you are prompting for GHE credentials. Not used for public GH authentication.
  • scopes (Array, optional): defaults to [], consult the GitHub scopes documentation to see what you may need for your application.
  • 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.
  • 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.
  • passwordReplaceChar (String, optional): defaults to '✔', the character echoed when the user inputs their password. Can be set to '' to silence the output.
  • 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.

The callback 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 }.

Setup

Github requires a clientId from a Github oAuth Application in order to complete oAuth device flow authentication.

  1. Register an "oAuth Application" with Github:
  1. 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.
  2. 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.

The 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.

v4 to v5 Upgrade guide

  • A options.clientId is required to use device flow. Set up an oAuth application to get a clientId.
  • 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.
  • options.note is only used for GHE basic auth now. Your oAuth application details serve the purpose of token note.
  • 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.

Contributing

ghauth is an OPEN Open Source Project. This means that:

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.

See the CONTRIBUTING.md file for more details.

A note about tests

... 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.

Contributors

ghauth is made possible by the excellent work of the following contributors:

Rod VaggGitHub/rvaggTwitter/@rvagg
Jeppe Nejsum MadsenGitHub/jeppenejsumTwitter/@nejsum
Max OgdenGitHub/maxogdenTwitter/@maxogden
Bret ComnesGitHub/bcomnesTwitter/@bcomnes

License & copyright

Copyright (c) 2014 ghauth contributors (listed above).

ghauth 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.

ghauth's People

Contributors

aduh95 avatar bcomnes avatar dependabot[bot] avatar finnp avatar jeppenejsum avatar linusu avatar mzgoddard avatar rvagg avatar semantic-release-bot avatar trott avatar zeke avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ghauth's Issues

Add option to define replaced character when echoing password

It would be nice if the options argument had an optional property which would make it possible to define the character used to replace each password character when echoing.

For example, setting it to *, would result it output like this:

Your GitHub password: ********

Setting to an empty string (or another falsy value) could disable the replacement but still silence the output:

Your GitHub password:

It could default to for backwards-compatiblility.

I'm happy to open a PR if this is something which would be welcome? 😃

Missing 'notifications' scope

I am trying to use ghauth with this config setting, here:

const authOptions = {
  configName: 'ghauth',
  note: 'Ignore GitHub notifications for a specific user',
  userAgent: 'ignoreGithubUsers',
  scopes: ['notifications']
}

However, I am running into this error:

{ [Error: {"message":"Missing the 'notifications' scope.","documentation_url":"https://developer.github.com/v3"}]
  status: 403,
  json: 
   { message: 'Missing the \'notifications\' scope.',
     documentation_url: 'https://developer.github.com/v3' } }

Any ideas why this is happening? I have the scope set right.

Spinner makes device token difficult to copy/paste

Having the device token next to the spinner makes it hard to copy and past as some terminals don't retain highlight selection over lines that are rewritten during the spinner animation.

Thinking we should move the token to a line above the spinner so this issue is avoided.

Triggering 2FA SMS

Hi @rvagg!

I'm using this for jlord/offline-issues and a few people have run into trouble authenticating with 2FA using SMS. It isn't triggering the message. It seems like 2FA when using an app is Ok, though. Any ideas about this?

Deleting the Authorization

Hi,

I have similar problems to #6. Everytime I redo the authorization, I get an error if I already have authorized the application (but haven't saved the credentials ;)).

The problem is that I would have to delete the authorization (https://developer.github.com/v3/oauth_authorizations/#delete-an-authorization), since I actually don't need it after its use.

Therefore I think it would be nice if ghauth would have a delete method, which first deleted the authorization key (with the authorization key itself ..) and then deletes the .config/<>.json.

This might also be useful for applications that want to implement a logout command.

The revoke methods seemingly can not be used, since they need a client_id, which we don't have. This kind of seems like we either have to keep track of the ID of the authorization, which would suck. A possibility to "self-destruct" them would be nice.

I will look into this - but it kind of looks like that there is no nice way other then let the user delete the tokens for themself..

Best,
Finn

PS: This is the workflow that I am actually trying to achieve. Maybe I should just write my custom solution for it. Not sure whether that's within the scope of this module.

Thank you!

This is exactly what I needed for some client work I am doing. It killed reams of stupid code I knew could/should be extracted to a module but was too lazy to do myself.

Configuration namespacing for different authUrls

Tokens should be saved and read in different configuration namespaces for different authUrls so that multiple GitHub sources can be used on the same machine without needing to delete the config file. This could be accomplished by using the auth URL when reading or writing to the config as follows:

{
  "https://ghe.example.com/api/v3": {
    "user": "enterprise_username",
    "token": "enterprise_token"
  }
}

github.com credentials could either continue to be in the root object for backwards compatibility, or namespaced as well.

Depreciated OAuth Authorization API

Hey just looking into the following emails I've been getting after using ghauth powered clis.

You or an application you used recently accessed the deprecated Authorizations endpoint on the GitHub API with the useragent gh-release.

We will remove the Authorizations API endpoint on November 13, 2020. If you accessed the API via password authentication, then we recommend you use the web flow to authenticate. Please check that your app uses the web flow for authentication: https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps/#web-application-flow

You can learn more about these changes by visiting our developer blog:
https://developer.github.com/changes/2020-02-14-deprecating-oauth-auth-endpoint/

Looks like this wonderfully convenient API is deprecated in favor of some kind of oauth web flow.

https://developer.github.com/changes/2020-02-14-deprecating-oauth-auth-endpoint/
https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps/#web-application-flow
https://docs.github.com/en/developers/apps/authorizing-oauth-apps#device-flow

Any plans to move this library over to that flow? Or do you recommend a different library that covers that? Interested in a PR that moves over to that auth flow?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.