@alicloud/oauth2

1.0.0 • Public • Published

Aliyun OAuth 2.0 Node.js Client

This SDK is used support login in Web Application, more details see https://help.aliyun.com/document_detail/69962.html .

Installation

$ npm install @alicloud/oauth2

Usage

  1. Build the client with client id and client secret
// Require it
const AliyunOAuth2 = require('@alicloud/oauth2');

const client = new AliyunOAuth2({
  clientId,
  clientSecret
});

The client can be used for All user to login with OAuth 2.0.

  1. Get the authorize URL and let user redirect to the url
const callback = 'https://yourwebapp.com/authcallback/';
const scope = 'openid /acs/ccc';
const state = '1234567890';
const url = auth.getAuthorizeURL(callback, state, scope, 'offline');
// like
// https://signin.aliyun.com/oauth2/v1/auth?client_id=123456&redirect_uri=https%3A%2F%2Fyourwebapp.com%2Fauthcallback%2F&response_type=code&scope=openid%20%2Facs%2Fccc&access_type=offline&state=1234567890

After user login with Aliyun Web UI, it will callback to your web app with code, like:

https://yourwebapp.com/authcallback/?code=xxx

  1. Use code to get access token
async function () {
  const reuslt = await client.getAccessToken('code');
});

If ok, the result like:

{
  "access_token": "eyJraWQiOiJrMTIzNCIsImVuY...",
  "token_type": "Bearer",
  "expires_in": 3600,
  "refresh_token": "Ccx63VVeTn2dxV7ovXXfLtAqLLERAH1Bc",
  "id_token": "eyJhbGciOiJIUzI1N..."
}

If fails, throw an error like:

var err = new Error(`${data.error}: ${data.error_description}`);
err.name = 'OAuthServerError';
err.code = data.error;
err.data = {
  httpcode: data.http_code,
  requestid: data.request_id
};
throw err;
  1. When token expired, we can refresh it with refresh token
async function () {
  const reuslt = await client.refreshAccessToken('refresh token');
});

The result is like getAccessToken return value.

  1. Also, If need, we can revoke the token
async function () {
  const reuslt = await client.revokeAccessToken('access token');
});

License

The MIT license

Readme

Keywords

none

Package Sidebar

Install

npm i @alicloud/oauth2

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

6.24 kB

Total Files

3

Last publish

Collaborators

  • console-fe
  • aliyunsdkteam
  • pagecao
  • fengmk2
  • jacksontian