oauth-callback 0.0.1 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +69 -24
- package/dist/errors.d.ts +37 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/index.d.ts +32 -11
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +929 -7
- package/dist/server.d.ts +58 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/templates.d.ts +14 -0
- package/dist/templates.d.ts.map +1 -0
- package/dist/types.d.ts +63 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +10 -6
- package/src/errors.ts +50 -0
- package/src/index.ts +123 -0
- package/src/server.ts +557 -0
- package/src/templates.ts +35 -0
- package/src/types.ts +74 -0
- package/index.ts +0 -30
package/README.md
CHANGED
|
@@ -1,16 +1,25 @@
|
|
|
1
|
-
# OAuth Callback
|
|
1
|
+
# OAuth Callback
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://badge.fury.io/js/oauth-callback)
|
|
4
|
+
[](https://npmjs.com/package/oauth-callback)
|
|
5
|
+
[](https://github.com/kriasoft/oauth-callback/blob/main/LICENSE)
|
|
6
|
+
[](https://www.typescriptlang.org/)
|
|
7
|
+
|
|
8
|
+
A lightweight local HTTP server for handling OAuth 2.0 authorization callbacks in Node.js, Deno, and Bun applications. Perfect for CLI tools, desktop applications, and development environments that need to capture OAuth authorization codes.
|
|
4
9
|
|
|
5
10
|
## Features
|
|
6
11
|
|
|
7
|
-
- 🚀
|
|
8
|
-
- 🔒 Secure
|
|
9
|
-
- ⚡
|
|
10
|
-
- 🎯 TypeScript support out of the box
|
|
11
|
-
- 🛡️
|
|
12
|
-
- 🔄 Automatic server cleanup after callback
|
|
13
|
-
-
|
|
12
|
+
- 🚀 **Multi-runtime support** - Works with Node.js 18+, Deno, and Bun
|
|
13
|
+
- 🔒 **Secure localhost-only server** for OAuth callbacks
|
|
14
|
+
- ⚡ **Minimal dependencies** - Only requires `open` package
|
|
15
|
+
- 🎯 **TypeScript support** out of the box
|
|
16
|
+
- 🛡️ **Comprehensive OAuth error handling** with detailed error classes
|
|
17
|
+
- 🔄 **Automatic server cleanup** after callback
|
|
18
|
+
- 🎪 **Auto-closing success pages** with countdown timer
|
|
19
|
+
- 🎨 **Customizable HTML templates** with placeholder support
|
|
20
|
+
- 🚦 **AbortSignal support** for programmatic cancellation
|
|
21
|
+
- 📝 **Request logging and debugging** callbacks
|
|
22
|
+
- 🌐 **Modern Web Standards APIs** (Request/Response/URL)
|
|
14
23
|
|
|
15
24
|
## Installation
|
|
16
25
|
|
|
@@ -76,7 +85,8 @@ async function authenticate() {
|
|
|
76
85
|
```typescript
|
|
77
86
|
import { getAuthCode } from "oauth-callback";
|
|
78
87
|
|
|
79
|
-
const result = await getAuthCode(
|
|
88
|
+
const result = await getAuthCode({
|
|
89
|
+
authorizationUrl: authUrl,
|
|
80
90
|
port: 8080, // Use custom port (default: 3000)
|
|
81
91
|
timeout: 60000, // Custom timeout in ms (default: 30000)
|
|
82
92
|
});
|
|
@@ -110,21 +120,58 @@ const microsoftAuth = await getAuthCode(
|
|
|
110
120
|
);
|
|
111
121
|
```
|
|
112
122
|
|
|
123
|
+
### Advanced Usage
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
// With custom HTML templates and logging
|
|
127
|
+
const result = await getAuthCode({
|
|
128
|
+
authorizationUrl: authUrl,
|
|
129
|
+
port: 3000,
|
|
130
|
+
hostname: "127.0.0.1", // Bind to specific IP
|
|
131
|
+
successHtml: "<h1>Success! You can close this window.</h1>",
|
|
132
|
+
errorHtml: "<h1>Error: {{error_description}}</h1>",
|
|
133
|
+
onRequest: (req) => {
|
|
134
|
+
console.log(`Received request: ${req.method} ${req.url}`);
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
// With cancellation support
|
|
139
|
+
const controller = new AbortController();
|
|
140
|
+
|
|
141
|
+
// Cancel after 10 seconds
|
|
142
|
+
setTimeout(() => controller.abort(), 10000);
|
|
143
|
+
|
|
144
|
+
try {
|
|
145
|
+
const result = await getAuthCode({
|
|
146
|
+
authorizationUrl: authUrl,
|
|
147
|
+
signal: controller.signal,
|
|
148
|
+
});
|
|
149
|
+
} catch (error) {
|
|
150
|
+
if (error.message === "Operation aborted") {
|
|
151
|
+
console.log("Authorization was cancelled");
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
```
|
|
155
|
+
|
|
113
156
|
## API Reference
|
|
114
157
|
|
|
115
|
-
### `getAuthCode(
|
|
158
|
+
### `getAuthCode(input)`
|
|
116
159
|
|
|
117
160
|
Starts a local HTTP server and opens the authorization URL in the user's browser.
|
|
118
161
|
|
|
119
162
|
#### Parameters
|
|
120
163
|
|
|
121
|
-
- `
|
|
122
|
-
- `
|
|
164
|
+
- `input` (string | GetAuthCodeOptions): Either a string containing the OAuth authorization URL, or an options object with:
|
|
165
|
+
- `authorizationUrl` (string): The OAuth authorization URL
|
|
123
166
|
- `port` (number): Port for the local server (default: 3000)
|
|
167
|
+
- `hostname` (string): Hostname to bind the server to (default: "localhost")
|
|
168
|
+
- `callbackPath` (string): URL path for the OAuth callback (default: "/callback")
|
|
124
169
|
- `timeout` (number): Timeout in milliseconds (default: 30000)
|
|
125
|
-
- `
|
|
126
|
-
- `
|
|
127
|
-
- `
|
|
170
|
+
- `openBrowser` (boolean): Whether to open browser automatically (default: true)
|
|
171
|
+
- `successHtml` (string): Custom HTML to display on successful authorization
|
|
172
|
+
- `errorHtml` (string): Custom HTML to display on authorization error
|
|
173
|
+
- `signal` (AbortSignal): AbortSignal for cancellation support
|
|
174
|
+
- `onRequest` (function): Callback fired when a request is received (for logging/debugging)
|
|
128
175
|
|
|
129
176
|
#### Returns
|
|
130
177
|
|
|
@@ -140,9 +187,8 @@ Promise that resolves to:
|
|
|
140
187
|
|
|
141
188
|
#### Throws
|
|
142
189
|
|
|
143
|
-
- `OAuthError`: When the OAuth provider returns an error
|
|
144
|
-
- `
|
|
145
|
-
- `Error`: For other unexpected errors
|
|
190
|
+
- `OAuthError`: When the OAuth provider returns an error (always thrown for OAuth errors)
|
|
191
|
+
- `Error`: For timeout or other unexpected errors
|
|
146
192
|
|
|
147
193
|
### `OAuthError`
|
|
148
194
|
|
|
@@ -167,10 +213,9 @@ class OAuthError extends Error {
|
|
|
167
213
|
## Security Considerations
|
|
168
214
|
|
|
169
215
|
- The server only accepts connections from localhost
|
|
170
|
-
- Automatically validates the state parameter if provided
|
|
171
216
|
- Server is closed immediately after receiving the callback
|
|
172
|
-
- Supports PKCE (Proof Key for Code Exchange) flow
|
|
173
217
|
- No data is stored persistently
|
|
218
|
+
- State parameter validation should be implemented by the application
|
|
174
219
|
|
|
175
220
|
## Development
|
|
176
221
|
|
|
@@ -190,7 +235,7 @@ bun run example.ts
|
|
|
190
235
|
|
|
191
236
|
## Requirements
|
|
192
237
|
|
|
193
|
-
- Node.js
|
|
238
|
+
- Node.js 18+ (for native Request/Response support), Deno, or Bun 1.0+
|
|
194
239
|
- A registered OAuth application with a provider
|
|
195
240
|
- Redirect URI configured as `http://localhost:[port]/callback`
|
|
196
241
|
|
|
@@ -201,7 +246,7 @@ bun run example.ts
|
|
|
201
246
|
If port 3000 is already in use, specify a different port:
|
|
202
247
|
|
|
203
248
|
```typescript
|
|
204
|
-
const result = await getAuthCode(authUrl,
|
|
249
|
+
const result = await getAuthCode({ authorizationUrl: authUrl, port: 8080 });
|
|
205
250
|
```
|
|
206
251
|
|
|
207
252
|
### Firewall Warnings
|
|
@@ -228,4 +273,4 @@ This project is released under the MIT License. Feel free to use it in your proj
|
|
|
228
273
|
|
|
229
274
|
## Support
|
|
230
275
|
|
|
231
|
-
Found a bug or have a question? Please open an issue on the [GitHub issue tracker](https://github.com/
|
|
276
|
+
Found a bug or have a question? Please open an issue on the [GitHub issue tracker](https://github.com/kriasoft/oauth-callback/issues) and we'll be happy to help. If this project saves you time and you'd like to support its continued development, consider [becoming a sponsor](https://github.com/sponsors/koistya). Every bit of support helps maintain and improve this tool for the community. Thank you!
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom error class for OAuth-specific errors
|
|
3
|
+
*
|
|
4
|
+
* This error is thrown when the OAuth provider returns an error response
|
|
5
|
+
* (e.g., user denies access, invalid client, etc.). It preserves the
|
|
6
|
+
* original OAuth error details for proper error handling.
|
|
7
|
+
*/
|
|
8
|
+
export declare class OAuthError extends Error {
|
|
9
|
+
/** OAuth error code (e.g., 'access_denied', 'invalid_request') */
|
|
10
|
+
error: string;
|
|
11
|
+
/** Human-readable error description provided by OAuth provider */
|
|
12
|
+
error_description?: string;
|
|
13
|
+
/** URI with additional information about the error */
|
|
14
|
+
error_uri?: string;
|
|
15
|
+
/**
|
|
16
|
+
* Create a new OAuthError
|
|
17
|
+
* @param error - OAuth error code
|
|
18
|
+
* @param description - Human-readable error description
|
|
19
|
+
* @param uri - URI with additional error information
|
|
20
|
+
*/
|
|
21
|
+
constructor(error: string, description?: string, uri?: string);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Error thrown when OAuth callback times out
|
|
25
|
+
*
|
|
26
|
+
* This error indicates that no OAuth callback was received within the
|
|
27
|
+
* specified timeout period. This could happen if the user doesn't complete
|
|
28
|
+
* the authorization flow or if there are network connectivity issues.
|
|
29
|
+
*/
|
|
30
|
+
export declare class TimeoutError extends Error {
|
|
31
|
+
/**
|
|
32
|
+
* Create a new TimeoutError
|
|
33
|
+
* @param message - Custom error message (optional)
|
|
34
|
+
*/
|
|
35
|
+
constructor(message?: string);
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAGA;;;;;;GAMG;AACH,qBAAa,UAAW,SAAQ,KAAK;IACnC,kEAAkE;IAClE,KAAK,EAAE,MAAM,CAAC;IACd,kEAAkE;IAClE,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,sDAAsD;IACtD,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;;;;OAKG;gBACS,KAAK,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM;CAO9D;AAED;;;;;;GAMG;AACH,qBAAa,YAAa,SAAQ,KAAK;IACrC;;;OAGG;gBACS,OAAO,GAAE,MAAmC;CAIzD"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,15 +1,36 @@
|
|
|
1
|
+
import { type CallbackResult } from "./server";
|
|
2
|
+
import type { GetAuthCodeOptions } from "./types";
|
|
3
|
+
export type { CallbackResult, CallbackServer, ServerOptions } from "./server";
|
|
4
|
+
export { OAuthError } from "./errors";
|
|
5
|
+
export type { GetAuthCodeOptions } from "./types";
|
|
1
6
|
/**
|
|
2
7
|
* Get the OAuth authorization code from the authorization URL.
|
|
3
8
|
*
|
|
4
|
-
*
|
|
9
|
+
* Creates a temporary local HTTP server to handle the OAuth callback,
|
|
10
|
+
* opens the authorization URL in the user's browser, and waits for the
|
|
11
|
+
* OAuth provider to redirect back with an authorization code.
|
|
12
|
+
*
|
|
13
|
+
* @param input - Either a string containing the OAuth authorization URL,
|
|
14
|
+
* or a GetAuthCodeOptions object with detailed configuration
|
|
15
|
+
* @returns Promise that resolves to CallbackResult containing the authorization code
|
|
16
|
+
* and other parameters, or rejects with OAuthError if authorization fails
|
|
17
|
+
* @throws {OAuthError} When OAuth provider returns an error (e.g., access_denied)
|
|
18
|
+
* @throws {Error} For timeout, network errors, or other unexpected failures
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* // Simple usage with URL string
|
|
23
|
+
* const result = await getAuthCode('https://oauth.example.com/authorize?...');
|
|
24
|
+
* console.log('Code:', result.code);
|
|
25
|
+
*
|
|
26
|
+
* // Advanced usage with options
|
|
27
|
+
* const result = await getAuthCode({
|
|
28
|
+
* authorizationUrl: 'https://oauth.example.com/authorize?...',
|
|
29
|
+
* port: 8080,
|
|
30
|
+
* timeout: 60000,
|
|
31
|
+
* onRequest: (req) => console.log('Request:', req.url)
|
|
32
|
+
* });
|
|
33
|
+
* ```
|
|
5
34
|
*/
|
|
6
|
-
export declare function getAuthCode(input:
|
|
7
|
-
|
|
8
|
-
authorizeUrl: string;
|
|
9
|
-
/** @default 3000 */
|
|
10
|
-
port?: number;
|
|
11
|
-
/** @default true */
|
|
12
|
-
throwOnError?: boolean;
|
|
13
|
-
/** @default true */
|
|
14
|
-
openBrowser?: boolean;
|
|
15
|
-
} | string): Promise<void>;
|
|
35
|
+
export declare function getAuthCode(input: GetAuthCodeOptions | string): Promise<CallbackResult>;
|
|
36
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAaA,OAAO,EAAwB,KAAK,cAAc,EAAE,MAAM,UAAU,CAAC;AACrE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAGlD,YAAY,EAAE,cAAc,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAC9E,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AACtC,YAAY,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAElD;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAsB,WAAW,CAC/B,KAAK,EAAE,kBAAkB,GAAG,MAAM,GACjC,OAAO,CAAC,cAAc,CAAC,CAsEzB"}
|