oauth-callback 0.0.1 → 0.1.0

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 CHANGED
@@ -1,16 +1,20 @@
1
1
  # OAuth Callback Server
2
2
 
3
- A lightweight local HTTP server for handling OAuth 2.0 authorization callbacks in Node.js/Bun applications. Perfect for CLI tools, desktop applications, and development environments that need to capture OAuth authorization codes.
3
+ 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
4
 
5
5
  ## Features
6
6
 
7
- - 🚀 Simple async/await API
8
- - 🔒 Secure local server for OAuth callbacks
9
- - ⚡ Zero dependencies (when using Bun)
10
- - 🎯 TypeScript support out of the box
11
- - 🛡️ Built-in error handling for OAuth errors
12
- - 🔄 Automatic server cleanup after callback
13
- - 📱 Cross-platform support (Windows, macOS, Linux)
7
+ - 🚀 **Multi-runtime support** - Works with Node.js 18+, Deno, and Bun
8
+ - 🔒 **Secure localhost-only server** for OAuth callbacks
9
+ - ⚡ **Minimal dependencies** - Only requires `open` package
10
+ - 🎯 **TypeScript support** out of the box
11
+ - 🛡️ **Comprehensive OAuth error handling** with detailed error classes
12
+ - 🔄 **Automatic server cleanup** after callback
13
+ - 🎪 **Auto-closing success pages** with countdown timer
14
+ - 🎨 **Customizable HTML templates** with placeholder support
15
+ - 🚦 **AbortSignal support** for programmatic cancellation
16
+ - 📝 **Request logging and debugging** callbacks
17
+ - 🌐 **Modern Web Standards APIs** (Request/Response/URL)
14
18
 
15
19
  ## Installation
16
20
 
@@ -76,7 +80,8 @@ async function authenticate() {
76
80
  ```typescript
77
81
  import { getAuthCode } from "oauth-callback";
78
82
 
79
- const result = await getAuthCode(authUrl, {
83
+ const result = await getAuthCode({
84
+ authorizationUrl: authUrl,
80
85
  port: 8080, // Use custom port (default: 3000)
81
86
  timeout: 60000, // Custom timeout in ms (default: 30000)
82
87
  });
@@ -110,21 +115,58 @@ const microsoftAuth = await getAuthCode(
110
115
  );
111
116
  ```
112
117
 
118
+ ### Advanced Usage
119
+
120
+ ```typescript
121
+ // With custom HTML templates and logging
122
+ const result = await getAuthCode({
123
+ authorizationUrl: authUrl,
124
+ port: 3000,
125
+ hostname: "127.0.0.1", // Bind to specific IP
126
+ successHtml: "<h1>Success! You can close this window.</h1>",
127
+ errorHtml: "<h1>Error: {{error_description}}</h1>",
128
+ onRequest: (req) => {
129
+ console.log(`Received request: ${req.method} ${req.url}`);
130
+ },
131
+ });
132
+
133
+ // With cancellation support
134
+ const controller = new AbortController();
135
+
136
+ // Cancel after 10 seconds
137
+ setTimeout(() => controller.abort(), 10000);
138
+
139
+ try {
140
+ const result = await getAuthCode({
141
+ authorizationUrl: authUrl,
142
+ signal: controller.signal,
143
+ });
144
+ } catch (error) {
145
+ if (error.message === "Operation aborted") {
146
+ console.log("Authorization was cancelled");
147
+ }
148
+ }
149
+ ```
150
+
113
151
  ## API Reference
114
152
 
115
- ### `getAuthCode(authUrl, options?)`
153
+ ### `getAuthCode(input)`
116
154
 
117
155
  Starts a local HTTP server and opens the authorization URL in the user's browser.
118
156
 
119
157
  #### Parameters
120
158
 
121
- - `authUrl` (string): The OAuth authorization URL
122
- - `options` (object, optional):
159
+ - `input` (string | GetAuthCodeOptions): Either a string containing the OAuth authorization URL, or an options object with:
160
+ - `authorizationUrl` (string): The OAuth authorization URL
123
161
  - `port` (number): Port for the local server (default: 3000)
162
+ - `hostname` (string): Hostname to bind the server to (default: "localhost")
163
+ - `callbackPath` (string): URL path for the OAuth callback (default: "/callback")
124
164
  - `timeout` (number): Timeout in milliseconds (default: 30000)
125
- - `callbackPath` (string): Callback path (default: "/callback")
126
- - `successMessage` (string): Message shown on successful authorization
127
- - `errorMessage` (string): Message shown on authorization error
165
+ - `openBrowser` (boolean): Whether to open browser automatically (default: true)
166
+ - `successHtml` (string): Custom HTML to display on successful authorization
167
+ - `errorHtml` (string): Custom HTML to display on authorization error
168
+ - `signal` (AbortSignal): AbortSignal for cancellation support
169
+ - `onRequest` (function): Callback fired when a request is received (for logging/debugging)
128
170
 
129
171
  #### Returns
130
172
 
@@ -140,9 +182,8 @@ Promise that resolves to:
140
182
 
141
183
  #### Throws
142
184
 
143
- - `OAuthError`: When the OAuth provider returns an error
144
- - `TimeoutError`: When the authorization times out
145
- - `Error`: For other unexpected errors
185
+ - `OAuthError`: When the OAuth provider returns an error (always thrown for OAuth errors)
186
+ - `Error`: For timeout or other unexpected errors
146
187
 
147
188
  ### `OAuthError`
148
189
 
@@ -167,10 +208,9 @@ class OAuthError extends Error {
167
208
  ## Security Considerations
168
209
 
169
210
  - The server only accepts connections from localhost
170
- - Automatically validates the state parameter if provided
171
211
  - Server is closed immediately after receiving the callback
172
- - Supports PKCE (Proof Key for Code Exchange) flow
173
212
  - No data is stored persistently
213
+ - State parameter validation should be implemented by the application
174
214
 
175
215
  ## Development
176
216
 
@@ -190,7 +230,7 @@ bun run example.ts
190
230
 
191
231
  ## Requirements
192
232
 
193
- - Node.js 14+ or Bun 1.0+
233
+ - Node.js 18+ (for native Request/Response support), Deno, or Bun 1.0+
194
234
  - A registered OAuth application with a provider
195
235
  - Redirect URI configured as `http://localhost:[port]/callback`
196
236
 
@@ -201,7 +241,7 @@ bun run example.ts
201
241
  If port 3000 is already in use, specify a different port:
202
242
 
203
243
  ```typescript
204
- const result = await getAuthCode(authUrl, { port: 8080 });
244
+ const result = await getAuthCode({ authorizationUrl: authUrl, port: 8080 });
205
245
  ```
206
246
 
207
247
  ### Firewall Warnings
@@ -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
- * @param input Authorization URL or authorization options.
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
- /** OAuth authorization URL */
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"}