Skip to content

kgryte/github-create-token

Repository files navigation

Create Token

NPM version Build Status Coverage Status Dependencies

Create a Github OAuth access token.

Installation

$ npm install github-create-token

Usage

var createToken = require( 'github-create-token' );

createToken( options, clbk )

Creates a Github OAuth access token.

var opts = {
	'username': 'beep',
	'password': 'boop',
	'scopes': [ 'public_repo', 'read:org' ],
	'note': 'beep'
};

createToken( opts, clbk );

function clbk( error, results, info ) {
	// Check for rate limit information...
	if ( info ) {
		console.error( 'Limit: %d', info.limit );
		console.error( 'Remaining: %d', info.remaining );
		console.error( 'Reset: %s', (new Date( info.reset*1000 )).toISOString() );
	}
	if ( error ) {
		throw new Error( error.message );
	}
	console.log( JSON.stringify( results ) );
	/* returns
		{
			"id": 1,
			"url":"https://api.github.com/authorizations/1",
			"app": {
				"name":"beep",
				"url":"https://developer.github.com/v3/oauth_authorizations/",
				"client_id":"00000000000000000000"
			},
			"token": "abcdefgh12345678",
			"hashed_token": "25f94a2a5c7fbaf499c665bc73d67c1c87e496da8985131633ee0a95819db2e8",
			"token_last_eight": "12345678",
			"note": "beep",
			"note_url": null,
			"created_at": "2016-03-05T06:34:01Z",
			"updated_at": "2016-03-05T06:34:01Z",
			"scopes": [
				"public_repo",
				"read:org"
			],
			"fingerprint": null
		}
	*/
}

The function accepts the following options:

  • username: Github username (required).
  • password: Github password (required).
  • scopes: list of scopes (required).
  • note: a note as to the purpose of the token (required).
  • otp: Github one-time password (2-factor authentication).
  • note_url: an app URL if authorizing an application.
  • client_id: a 20 character OAuth app client key for which to create the token.
  • client_secret: a 40 character OAuth app client secret for which to create the token.
  • fingerprint: a unique string to distinguish a token from others created for the same client id and user.
  • useragent: user agent string.

The function only supports basic authentication using a username and password. To authenticate with Github, set the username and password options.

var opts = {
	'username': 'beep',
	'password': 'boop',
	'scopes': [ 'public_repo' ],
	'note': 'for my beepboop app'
};

createToken( opts, clbk );

To specify a user agent, set the useragent option.

var opts = {
	'username': 'beep',
	'password': 'boop',
	'scopes': [ 'public_repo' ],
	'note': 'for my beepboop app',
	'useragent': 'hello-github!'
};

createToken( opts, clbk );

If a user has two-factor authentication enabled, set the otp (one-time password) option.

var opts = {
	'username': 'beep',
	'password': 'boop',
	'otp': '1234',
	'scopes': [ 'public_repo' ],
	'note': 'for my beepboop app'
};

createToken( opts, clbk );

Notes

  • Rate limit information includes the following:
    • limit: maximum number of requests a consumer is permitted to make per hour.
    • remaining: number of remaining requests.
    • reset: time at which the current rate limit window resets in UTC seconds.

Examples

var createToken = require( 'github-create-token' );

var opts = {
	'username': '<username>',
	'password': '<password>',
	'otp': '<otp>',
	'scopes': [ 'public_repo' ],
	'note': 'for my beepboop app',
	'useragent': 'beep-boop-bop'
};

createToken( opts, clbk );

function clbk( error, results, info ) {
	if ( info ) {
		console.error( info );
	}
	if ( error ) {
		throw new Error( error.message );
	}
	console.log( results );
}

To run the example code from the top-level application directory,

$ node ./examples/index.js

CLI

Installation

To use the module as a general utility, install the module globally

$ npm install -g github-create-token

Usage

Usage: ghcreatetoken [options]

Options:

  -h,  --help                      Print this message.
  -V,  --version                   Print the package version.
       --lscopes                   List all possible scopes.
       --username username         Github username.
       --password password         Github password.
       --otp password              Github one-time password.
  -ua, --useragent ua              User agent.
       --scopes scope1,scope2,...  List of scopes.
       --note note                 Token note.
       --note_url url              Application URL.
       --client_id id              Client id.
       --client_secret secret      Client secret.
       --fingerprint fingerprint   Unique token identifier.

Notes

  • If a username is not provided, the implementation will attempt to infer a username by executing

    git config user.name
    

    in the current working directory.

  • If a password is not provided, the user will be prompted to provide a password.

  • In addition to the username and password options, a username and password may also be specified by GITHUB_USERNAME and GITHUB_PASSWORD environment variables. The command-line options always take precedence.

  • Token information is written to stdout.

  • Rate limit information is written to stderr.

Examples

Setting a username and password using the command-line options:

$ DEBUG=* ghcreatetoken --username <username> --password <password> --scopes=read_org,repo_status --note 'for my beepboop app'
# => '{...}'

Setting a username and password using environment variables:

$ DEBUG=* GITHUB_USERNAME=<username> GITHUB_PASSWORD=<password> ghcreatetoken --scopes=read_org,repo_status --note 'for my beepboop app'
# => '{...}'

For local installations, modify the command to point to the local installation directory; e.g.,

$ DEBUG=* ./node_modules/.bin/ghcreatetoken --scopes=read_org,repo_status --note 'for my beepboop app'
# => '{...}'

Or, if you have cloned this repository and run npm install, modify the command to point to the executable; e.g.,

$ DEBUG=* node ./bin/cli --scopes=read_org,repo_status --note 'for my beepboop app'
# => '{...}'

Tests

Unit

This repository uses tape for unit tests. To run the tests, execute the following command in the top-level application directory:

$ make test

All new feature development should have corresponding unit tests to validate correct functionality.

Test Coverage

This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:

$ make test-cov

Istanbul creates a ./reports/coverage directory. To access an HTML version of the report,

$ make view-cov

Browser Support

This repository uses Testling for browser testing. To run the tests in a (headless) local web browser, execute the following command in the top-level application directory:

$ make test-browsers

To view the tests in a local web browser,

$ make view-browser-tests

License

MIT license.

Copyright

Copyright © 2016. Athan Reines.