snapshot of https://github.com/mountain-loop/yaak.git (main, 2026-09-23; blobs > 131072B omitted)
This commit is contained in:
47
plugins/auth-bearer/README.md
Normal file
47
plugins/auth-bearer/README.md
Normal file
@@ -0,0 +1,47 @@
|
||||
# Bearer Token Authentication Plugin
|
||||
|
||||
A Bearer Token authentication plugin for Yaak that
|
||||
implements [RFC 6750](https://datatracker.ietf.org/doc/html/rfc6750), enabling secure API
|
||||
access using tokens, API keys, and other bearer credentials.
|
||||
|
||||

|
||||
|
||||
## Overview
|
||||
|
||||
This plugin provides Bearer Token authentication support for your API requests in Yaak.
|
||||
Bearer Token authentication is widely used in modern APIs, especially those following REST
|
||||
principles and OAuth 2.0 standards. It's the preferred method for APIs that issue access
|
||||
tokens, API keys, or other bearer credentials.
|
||||
|
||||
## How Bearer Token Authentication Works
|
||||
|
||||
Bearer Token authentication sends your token in the `Authorization` header with each
|
||||
request using the Bearer scheme:
|
||||
|
||||
```
|
||||
Authorization: Bearer <your-token>
|
||||
```
|
||||
|
||||
The token is transmitted as-is without any additional encoding, making it simple and
|
||||
efficient for API authentication.
|
||||
|
||||
## Configuration
|
||||
|
||||
The plugin requires only one field:
|
||||
|
||||
- **Token**: Your bearer token, access token, API key, or other credential
|
||||
- **Prefix**: The prefix to use for the Authorization header, which will be of the
|
||||
format "<PREFIX> <TOKEN>"
|
||||
|
||||
## Usage
|
||||
|
||||
1. Configure the request, folder, or workspace to use Bearer Authentication
|
||||
2. Enter the token and optional prefix in the authentication configuration
|
||||
3. The plugin will automatically add the proper `Authorization` header to your requests
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
- **401 Unauthorized**: Verify your token is valid and not expired
|
||||
- **403 Forbidden**: Check if your token has the necessary permissions/scopes
|
||||
- **Invalid Token Format**: Ensure you're using the complete token without truncation
|
||||
- **Token Expiration**: Refresh or regenerate expired tokens
|
||||
17
plugins/auth-bearer/package.json
Normal file
17
plugins/auth-bearer/package.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "@yaak/auth-bearer",
|
||||
"displayName": "Bearer Authentication",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"description": "Authenticate requests using bearer authentication",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/mountain-loop/yaak.git",
|
||||
"directory": "plugins/auth-bearer"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "yaakcli build",
|
||||
"dev": "yaakcli dev",
|
||||
"test": "vp test --run tests"
|
||||
}
|
||||
}
|
||||
39
plugins/auth-bearer/src/index.ts
Normal file
39
plugins/auth-bearer/src/index.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import type { PluginDefinition } from "@yaakapp/api";
|
||||
import type { CallHttpAuthenticationRequest } from "@yaakapp-internal/plugins";
|
||||
|
||||
export const plugin: PluginDefinition = {
|
||||
authentication: {
|
||||
name: "bearer",
|
||||
label: "Bearer Token",
|
||||
shortLabel: "Bearer",
|
||||
args: [
|
||||
{
|
||||
type: "text",
|
||||
name: "token",
|
||||
label: "Token",
|
||||
optional: true,
|
||||
password: true,
|
||||
},
|
||||
{
|
||||
type: "text",
|
||||
name: "prefix",
|
||||
label: "Prefix",
|
||||
optional: true,
|
||||
placeholder: "",
|
||||
defaultValue: "Bearer",
|
||||
description:
|
||||
'The prefix to use for the Authorization header, which will be of the format "<PREFIX> <TOKEN>".',
|
||||
},
|
||||
],
|
||||
async onApply(_ctx, { values }) {
|
||||
return { setHeaders: [generateAuthorizationHeader(values)] };
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
function generateAuthorizationHeader(values: CallHttpAuthenticationRequest["values"]) {
|
||||
const token = String(values.token || "").trim();
|
||||
const prefix = String(values.prefix || "").trim();
|
||||
const value = `${prefix} ${token}`.trim();
|
||||
return { name: "Authorization", value };
|
||||
}
|
||||
67
plugins/auth-bearer/tests/index.test.ts
Normal file
67
plugins/auth-bearer/tests/index.test.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import type { Context } from "@yaakapp/api";
|
||||
import { describe, expect, test } from "vite-plus/test";
|
||||
import { plugin } from "../src";
|
||||
|
||||
const ctx = {} as Context;
|
||||
|
||||
describe("auth-bearer", () => {
|
||||
test("No values", async () => {
|
||||
expect(
|
||||
await plugin.authentication?.onApply(ctx, {
|
||||
values: {},
|
||||
headers: [],
|
||||
url: "https://yaak.app",
|
||||
method: "POST",
|
||||
contextId: "111",
|
||||
}),
|
||||
).toEqual({ setHeaders: [{ name: "Authorization", value: "" }] });
|
||||
});
|
||||
|
||||
test("Only token", async () => {
|
||||
expect(
|
||||
await plugin.authentication?.onApply(ctx, {
|
||||
values: { token: "my-token" },
|
||||
headers: [],
|
||||
url: "https://yaak.app",
|
||||
method: "POST",
|
||||
contextId: "111",
|
||||
}),
|
||||
).toEqual({ setHeaders: [{ name: "Authorization", value: "my-token" }] });
|
||||
});
|
||||
|
||||
test("Only prefix", async () => {
|
||||
expect(
|
||||
await plugin.authentication?.onApply(ctx, {
|
||||
values: { prefix: "Hello" },
|
||||
headers: [],
|
||||
url: "https://yaak.app",
|
||||
method: "POST",
|
||||
contextId: "111",
|
||||
}),
|
||||
).toEqual({ setHeaders: [{ name: "Authorization", value: "Hello" }] });
|
||||
});
|
||||
|
||||
test("Prefix and token", async () => {
|
||||
expect(
|
||||
await plugin.authentication?.onApply(ctx, {
|
||||
values: { prefix: "Hello", token: "my-token" },
|
||||
headers: [],
|
||||
url: "https://yaak.app",
|
||||
method: "POST",
|
||||
contextId: "111",
|
||||
}),
|
||||
).toEqual({ setHeaders: [{ name: "Authorization", value: "Hello my-token" }] });
|
||||
});
|
||||
|
||||
test("Extra spaces", async () => {
|
||||
expect(
|
||||
await plugin.authentication?.onApply(ctx, {
|
||||
values: { prefix: "\t Hello ", token: " \nmy-token " },
|
||||
headers: [],
|
||||
url: "https://yaak.app",
|
||||
method: "POST",
|
||||
contextId: "111",
|
||||
}),
|
||||
).toEqual({ setHeaders: [{ name: "Authorization", value: "Hello my-token" }] });
|
||||
});
|
||||
});
|
||||
3
plugins/auth-bearer/tsconfig.json
Normal file
3
plugins/auth-bearer/tsconfig.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json"
|
||||
}
|
||||
Reference in New Issue
Block a user