@advanced-rest-client/authorization

中文文档
0.1.13发布于 2024/05/31Apache-2.0ESM + CJS近 30 天下载 3255

The UI and logic related to HTTP authorization.

原始使用文档
文档目录9 节
  1. Usage
  2. Installation
  3. Components
  4. OAuth authorization
  5. Development
  6. Running the demo locally
  7. Running the tests
  8. Acknowledgements
  9. Required dependencies

Authorization

Published on NPM

tests

The UI and logic related to HTTP authorization.

Usage

Installation

npm install --save @advanced-rest-client/authorization

Components

authorization-selector

An element that utilizes the authorization-method to render a selection box of authorization methods.

<authorization-selector
  horizontal
  multi
>
  <!-- None option -->
  <div type="none" aria-describedby="noneDesc">Authorization configuration is disabled</div>
  <p id="noneDesc" slot="aria">Select authorization method required by the API.</p>

  <!-- HTTP basic authorization -->
  <authorization-method type="basic" aria-describedby="basicDesc"></authorization-method>
  <p id="basicDesc" slot="aria">
    Basic authorization allows to send a username and a password in a request header.
  </p>

  <!-- Bearer token authorization -->
  <authorization-method type="bearer" aria-describedby="tokenDesc"></authorization-method>
  <p id="tokenDesc" slot="aria">
    Bearer authorization allows to send an authentication token in the authorization header using the "bearer" method.
  </p>

  <!-- ... -->
</authorization-selector>

authorization-method

The authorization-method element renders a form with inputs related to an authorization scheme defined by the type property.

<authorization-method
  type="oauth 2"
  redirectUri="https://..."
  authorizationUri="https://..."
  accessTokenUri="https://..."
  clientId="test-client-id"
  grantType="authorization_code"
  pkce
></authorization-method>

oauth2-scope-selector

An input that specializes in selecting an OAuth 2 scope.

 <oauth2-scope-selector
  required
  autoValidate
  allowedScopes='["user", "user:email", "user:follow", "..."]'
></oauth2-scope-selector>

OAuth authorization

import { OAuth2Authorization } from '@advanced-rest-client/oauth-authorization';

const settings = {
  grantType: 'implicit',
  clientId: 'CLIENT ID',
  redirectUri: 'https://example.com/auth-popup.html',
  authorizationUri: 'https://auth.example.com/token',
  scopes: ['email'],
  state: 'Optional string',
};

const factory = new OAuth2Authorization(settings);
const tokenInfo = await factory.authorize(settings)

Popup in authorization flow

This package contains the oauth-popup.html that can be used to exchange token / code data with hosting page. Other page can be used as well. The popup page must use the window.postMessage() to report back to the library the parameters returned by the authorization server. It expect to return the part of the URL that contains the authorization result. For example, for the popup url having values like this: https://redirect.domain.com/popup.html#code=1234&state=5678 the popup window should post message with code=1234&state=5678.

The state parameter and security

This element is intend to be used in debug applications where confidentially is already compromised because users may be asked to provide client secret parameter (depending on the flow). It should not be used in client applications that don't serve debugging purposes. Client secret should never be used on the client side.

To have at least minimum of protection (in already compromised environment) this library generates a state parameter as a series of alphanumeric characters and append them to the request. It is expected to return the same string in the response (as defined in rfc6749). Though this parameter is optional, it will reject the response if the state parameter is not the same as the one generated before the request.

The state parameter is generated automatically by the element if non provided in settings. It is a good idea to use this property to check if the event response (either token or error) are coming from your request for token. The app can support different OAuth clients so you can check later with the token response if this is a response for the same client.

Non-interactive authorization

For implicit and authorization_code token requests you can set the interactive configuration property to false to request the token in the background without rendering any UI related to authorization to the user. It can be used to request an access token after the user authorized the application. Server should return the token which will be passed back to the application.

Pre-defined client credentials

For user convenience in a test environment (like testing or documentation tools) we can define a list of client credentials (client id and client secret) that are rendered in the OAuth 2 editor in a drop down selector. This way a user can choose to use one of these credentials instead providing them manually.

const credentialsSource = [{
  grantType: 'client_credentials', 
  credentials: [
    {
      name: 'My social Network', 
      clientId: '123', 
      clientSecret: 'xyz'
    }, {
      name: 'My social Network 2', 
      clientId: '9876', 
      clientSecret: 'abc'
    }
  ]
}];
const editor = document.querySelector('authorization-method[oauth 2]');
editor.credentialsSource = credentialsSource;

Development

git clone https://github.com/advanced-rest-client/authorization
cd authorization
npm install

Running the demo locally

npm start

Running the tests

npm test

Acknowledgements

  • This element uses jsrsasign library distributed under MIT licence.
  • This element uses crypto-js library distributed under BSD license.

Required dependencies

The CryptoJS and RSAKey libraries are not included into the element sources. If your project do not use this libraries already include it into your project.

npm i cryptojslib jsrsasign
<script src="../../../cryptojslib/components/core.js"></script>
<script src="../../../cryptojslib/rollups/sha1.js"></script>
<script src="../../../cryptojslib/components/enc-base64-min.js"></script>
<script src="../../../cryptojslib/rollups/md5.js"></script>
<script src="../../../cryptojslib/rollups/hmac-sha1.js"></script>
<script src="../../../jsrsasign/lib/jsrsasign-rsa-min.js"></script>

Also OAuth1 element uses URL class with searchParams properties. If targeting old browsers include polyfill for this too.

版本与发布记录

@advanced-rest-client/authorization 共发布 19 个版本,首次发布于 2021/06/05;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.1.13latest2024/05/312 年前
0.1.112023/08/013 年前
0.2.52023/07/113 年前
0.1.102023/06/093 年前
0.1.92023/03/283 年前
0.1.82023/01/313 年前

关于 @advanced-rest-client/authorization 的常见问题

@advanced-rest-client/authorization 是什么?

The UI and logic related to HTTP authorization.

如何安装 @advanced-rest-client/authorization?

在项目目录运行 npm install @advanced-rest-client/authorization,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:jsrsasign(^10.0.5)、cryptojslib(^3.1.2)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@advanced-rest-client/authorization 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/advanced-rest-client__authorization 是否存在。

@advanced-rest-client/authorization 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@advanced-rest-client/authorization 可以商用吗?

@advanced-rest-client/authorization 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@advanced-rest-client/authorization 的下载量有多少?

根据 npm 官方下载统计,@advanced-rest-client/authorization 在 2026/10/05 – 2026/11/03 这 30 天内被下载 3,255 次,日均约 109 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@advanced-rest-client/authorization 还在维护吗?

最新版本 0.1.13 发布于 2024/05/31(2 年前);至今共发布 19 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/06/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。