Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions packages/postcat-oauth-2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# JWT authentication plugin

Json Web Token (JWT) is an open JSON-based standard implemented for passing claims between web application environments.

JWT is designed to be compact and secure, especially suitable for single sign-on (SSO) scenarios of distributed sites. JWT is generally used to transfer authenticated user identity information between identity providers and service providers, so as to obtain resources from resource servers, and can also add some additional statement information necessary for other business logic.

## How to use

After installing the plug-in, select authentication on the test page, and after filling in the data, the header `Authorization` will be automatically added to the request information.
![](https://raw.githubusercontent.com/eolinker/postcat-extensions/main/packages/postcat-jwt/assets/images/2023-03-15-10-42-12.png)

## JWT description

```HTTP
Client Request

GET /security/somethings HTTP/1.1
Authorization: Basic bmFtZTpwYXNzd29yZA==
```

including:

**head**

```json
{
"alg": "HS256",
"typ": "JWT"
}
```

**Payload**

```json
{
"name": "Postcat",
"introduce": "An extensible API tool."
}
```

WT provides 7 default fields for developers to choose.

- iss (issuer): issuer
- exp (expiration time): expiration time
- sub (subject): subject
- aud (audience): Audience, equivalent to the recipient
- nbf (Not Before): the start time to take effect
- iat (Issued At): Issue time
- jti (JWT ID): number, unique identifier

**Signature**

For each encryption algorithm, the signature corresponds to a calculation formula. For example, the signature of the SHA256 encryption algorithm is as follows:

```
HMACSHA256(
base64UrlEncode(header) + "." +
base64UrlEncode(payload) + "." +
Secret
)
```
21 changes: 21 additions & 0 deletions packages/postcat-oauth-2/README.zh-Hans.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# OAuth 2.0 鉴权插件

OAuth 2.0 是目前最流行的授权机制,用来授权第三方应用,获取用户数据。

## 使用方式

安装插件后,测试页面选中鉴权,填入数据后会自动在请求信息中添加头部 `Authorization`。
![](https://raw.githubusercontent.com/eolinker/postcat-extensions/main/packages/postcat-jwt/assets/images/2023-03-15-10-42-12.png)

## JWT 说明

OAuth 2.0 规定了四种获得令牌的流程。你可以选择最适合自己的那一种,向第三方应用颁发令牌。下面就是这四种授权方式。

- 授权码(authorization-code)
- 隐藏式(implicit)
- 密码式(password):
- 客户端凭证(client credentials)

![](../assets/images/2023-04-10-14-49-55.png)

> 引用自:https://www.ruanyifeng.com/blog/2019/04/oauth-grant-types.html
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions packages/postcat-oauth-2/i18n/en-US.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
4 changes: 4 additions & 0 deletions packages/postcat-oauth-2/i18n/zh-Hans.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"title": "JWT 鉴权",
"description": "JSON Web Token (JWT) 鉴权方式,安装后会在请求头部 Authorization 加入鉴权值"
}
41 changes: 41 additions & 0 deletions packages/postcat-oauth-2/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const OAuth = require('oauth')
var OAuth2 = OAuth.OAuth2
var twitterConsumerKey = '9c7cfcd303eb07cca8541ba59e916f3eFHxoKHnCoUF'
var twitterConsumerSecret = 'sRQn6ZzLnLA2B4alF9VzFwJb74ZwOYZc3AcvWagcDn'
var oauth2 = new OAuth2(
twitterConsumerKey,
twitterConsumerSecret,
'https://api.twitter.com/',
null,
'oauth2/token',
null
)
oauth2.getOAuthAccessToken(
'',
{ grant_type: 'client_credentials' },
function (e, access_token, refresh_token, results) {
console.log(e)
console.log('bearer: ', access_token)
}
)

// module.exports = {
// authAPI: (config) => {
// try {
// const authorizationValue = `Bearer ${jwt.sign(
// JSON.parse(config.payload),
// config.isBase64Encoded
// ? Buffer.from(config.Secret).toString('base64')
// : config.Secret,
// { algorithm: config.Algorithm }
// )}`

// return `pc.request.headers.add({
// key:'Authorization', value:'${authorizationValue}'
// })`
// } catch (err) {
// console.error(err)
// return null
// }
// }
// }
88 changes: 88 additions & 0 deletions packages/postcat-oauth-2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{
"name": "postcat-oauth-2",
"title": "OAuth2.0 Authorization",
"version": "0.0.1",
"description": "OAuth2.0 Authorization",
"author": {
"name": "Postcat"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Postcatlab/postcat-extensions.git"
},
"logo": "https://data.eolink.com/5fwiFW182ef051fc6150c6a274fde19a83667432b534ef0",
"engines": {
"postcat": "^0.4.0"
},
"categories": [
"API Testing"
],
"features": {
"authAPI": {
"action": "authAPI",
"label": "OAuth 2.0",
"configuration": {
"type": "object",
"properties": {
"token": {
"type": "string",
"ui": {
"widget": "select"
},
"required": true,
"default": "HS256",
"label": "Algorithm",
"enum": [
{
"label": "HS256",
"value": "HS256"
},
{
"label": "HS384",
"value": "HS384"
},
{
"label": "HS512",
"value": "HS512"
}
]
},
"Secret": {
"type": "string",
"required": false,
"default": "",
"label": "Secret"
},
"isBase64Encoded": {
"ui": {
"widget": "checkbox"
},
"type": "boolean",
"label": "",
"title": "Secret Base64 encoded",
"default": false
},
"payload": {
"ui": {
"widget": "textarea",
"rows": 4
},
"placeholder": "Please enter Payload,the format must be json",
"type": "string",
"label": "Payload",
"default": ""
}
}
}
},
"i18n": {
"sourceLocale": "en-US",
"locales": [
"zh-Hans"
]
}
},
"dependencies": {
"oauth": "^0.10.0"
}
}
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.