npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ya-token-cli

v1.0.0

Published

Library to obtain OAuth-token to get access to Yandex services for server application

Downloads

3

Readme

ya-token-cli NPM version Tests Coverage Status

This library contains some utilities to obtain Yandex OAuth token for CLI utilities. The process of getting token consists of two steps:

  1. Obtain confirmation code.
  2. Exchange confirmation code to a token.

Prerequisites

Creating app

Skip this step if you've already had one. Create an app as described in the documentation.

Configuring app

To make possible to authorize your console application, you need to set the redirect URI. To add it, go to the application parameters page and add the following URIs:

  • https://oauth.yandex.ru/verification_code - used when the user enters confirmation code manually
  • http://localhost:8899 - to obtain a confirmation code automatically

Now your app is ready to authorize.

Authorization flow

To authorize your app, add the following code to it:

import { auth } from 'ya-token-cli';

const CLIENT_ID = '<your-app-client-id>'; // can be obtained from application details page
const CLIENT_SECRET = '<your-app-client-secret>'; // see application details page
const REDIRECT_URI = 'http://localhost:8899/'; // Use the free port

const token = await auth(CLIENT_ID, CLIENT_SECRET, { redirectUri: REDIRECT_URI });
console.log(token);

When running, app first will display the URL, which the user should open in the browser:

Visit https://clck.ru/33qdQc

The next string will inform the user, that an additional server started:

Server listening on port 8899

This server will handle the redirect, which happens after the user has been successfully authorized. In the next step, the confirmation code will be exchanged to OAuth-token and returned to the app. The token has the following fields:

{
  "access_token": "9q8uhfp9q83h4faushdfgioaw349-osaiudhfiasuh", // Token itself
  "expires_in": 31529249, // Expiration period in seconds
  "refresh_token": "d09j1f0i8ashjdfahjflakjwefa-wejflakwjeflk", // Refresh token
  "token_type": "bearer" // Token type
}

API

auth(clientID, clientSecret, [clientOptions={}], [niceUrl=true], [obtainCodeAutomatically=true]): Promise<object>

This is an all-in-one method to obtain the OAuth token. It will run the whole workflow and return the OAuth data.

Parameters

  • clientID {string} - Application Client ID. You may find it on the application details page.
  • clientSecret {string} - Necessary to exchange the confirmation token to OAuth-token.
  • [clientOptions={}] {object} - [Optional] Additional properties could be specified. See optional parameters here. ⚠️ If you set obtainCodeAutomatically to true, you must provide clientOptions.redirectURI, which should be equal the local one set in the configuration section
  • [niceUrl=true] {boolean} - [Optional] Make verification URL shorter using clck.ru service.
  • [obtainCodeAutomatically=true] {boolean} - [Optional] Automatically obtain verification code. Otherwise, the user will have to enter this code manually.

Example

import { auth } from 'ya-token-cli';

// Visit https://oauth.yandex.ru to get the Client ID and Secret
const clientID = '74a00000000000000000000000000a06';
const clientSecret = 'ee000000000000000000000000000013';

const { access_token } = await auth(clientID, clientSecret, {
  redirectURI: 'http://localhost:8899' // required to obtain confirmation code automatically
});

console.log(access_token);

getConfirmationCodeUrl(clientID, [clientOptions={}]): string

Created the URL to obtain confirmation code. The user should open this link, authorize himself and grant permissions to the application.

Parameters

  • clientID {string} - Application Client ID. You may find it on the application details page.
  • [clientOptions={}] - [Optional] Additional options could be specified. See full list here.

Example

import { getConfirmationCodeUrl } from 'ya-token-cli/lib/confirmationCode.js';

// Visit https://oauth.yandex.ru to get the Client ID and Secret
const clientID = '74a00000000000000000000000000a06';

const confirmationCodeUrl = getConfirmationCodeUrl(clientID, {
  redirectURI: 'http://localhost:8899'
});

console.log(confirmationCodeUrl); // https://oauth.yandex.ru/authorize?client_id=74a00000000000000000000000000a06&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A8899

readConfirmationCodeAutomatically(redirectURI): Promise<string>

Run local web-server, to handle Yandex OAuth redirect and extract code from it. Return the Promise to be resolved with obtained code or rejected when code wasn't retrieved. The server will be down after the first request, no matter the result.

Parameters

  • redirectURI {string} - Redirect URI used for authorization. Better be the local one. The web-server will run on 0.0.0.0 address using the port from the redirectURI parameter.

Example

import { readConfirmationCodeAutomatically } from 'ya-token-cli/lib/confirmationCode.js';

// Visit https://oauth.yandex.ru/ to configure redirectURI of your app
const confirmationCode = await readConfirmationCodeAutomatically('http://localhost:8899');

console.log(confirmationCode); // 398672

readConfirmationCode([title]): Promise<string>

Will expect a confirmation code from the user to be entered in the terminal.

Parameters

  • [title='Enter confirmation code: '] {string} - The message to be displayed in the terminal.

Example

import { readConfirmationCode } from 'ya-token-cli/lib/confirmationCode.js';

// Visit https://oauth.yandex.ru/ to set redirectURI for your app to 'https://oauth.yandex.ru/verification_code'
const confirmationCode = await readConfirmationCode();

console.log(confirmationCode); // 398672

exchange(clientID, clientSecret, confirmationCode): Promise<>

Will expect a confirmation code from the user to be entered in the terminal.

Parameters

  • [title='Enter confirmation code: '] {string} - The message to be displayed in the terminal.

Example

import { exchange } from 'ya-token-cli/lib/token.js';

// Visit https://oauth.yandex.ru/ to set redirectURI for your app to 'https://oauth.yandex.ru/verification_code'
const confirmationCode = await readConfirmationCode();

console.log(confirmationCode); // 398672