@mastra/auth-cloud 0.0.1 → 1.1.0-alpha.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/CHANGELOG.md +32 -0
- package/LICENSE.md +30 -0
- package/README.md +65 -1
- package/dist/auth-provider.d.ts +198 -0
- package/dist/auth-provider.d.ts.map +1 -0
- package/dist/client.d.ts +110 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/error.d.ts +65 -0
- package/dist/error.d.ts.map +1 -0
- package/dist/index.cjs +855 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +850 -0
- package/dist/index.js.map +1 -0
- package/dist/oauth/index.d.ts +9 -0
- package/dist/oauth/index.d.ts.map +1 -0
- package/dist/oauth/network.d.ts +20 -0
- package/dist/oauth/network.d.ts.map +1 -0
- package/dist/oauth/oauth.d.ts +68 -0
- package/dist/oauth/oauth.d.ts.map +1 -0
- package/dist/oauth/state.d.ts +47 -0
- package/dist/oauth/state.d.ts.map +1 -0
- package/dist/pkce/cookie.d.ts +42 -0
- package/dist/pkce/cookie.d.ts.map +1 -0
- package/dist/pkce/error.d.ts +31 -0
- package/dist/pkce/error.d.ts.map +1 -0
- package/dist/pkce/index.d.ts +10 -0
- package/dist/pkce/index.d.ts.map +1 -0
- package/dist/pkce/pkce.d.ts +26 -0
- package/dist/pkce/pkce.d.ts.map +1 -0
- package/dist/rbac/index.d.ts +2 -0
- package/dist/rbac/index.d.ts.map +1 -0
- package/dist/rbac/rbac-provider.d.ts +124 -0
- package/dist/rbac/rbac-provider.d.ts.map +1 -0
- package/dist/session/cookie.d.ts +32 -0
- package/dist/session/cookie.d.ts.map +1 -0
- package/dist/session/index.d.ts +9 -0
- package/dist/session/index.d.ts.map +1 -0
- package/dist/session/session.d.ts +56 -0
- package/dist/session/session.d.ts.map +1 -0
- package/dist/types.d.ts +64 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +54 -3
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# @mastra/auth-cloud
|
|
2
|
+
|
|
3
|
+
## 1.1.0-alpha.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- Added `@mastra/auth-cloud` — a new auth provider for Mastra Cloud with PKCE OAuth flow, session management, and role-based access control. ([#13163](https://github.com/mastra-ai/mastra/pull/13163))
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { MastraCloudAuthProvider, MastraRBACCloud } from '@mastra/auth-cloud';
|
|
11
|
+
|
|
12
|
+
const mastra = new Mastra({
|
|
13
|
+
server: {
|
|
14
|
+
auth: new MastraCloudAuthProvider({
|
|
15
|
+
appId: process.env.MASTRA_APP_ID!,
|
|
16
|
+
apiKey: process.env.MASTRA_API_KEY!,
|
|
17
|
+
}),
|
|
18
|
+
rbac: new MastraRBACCloud({
|
|
19
|
+
appId: process.env.MASTRA_APP_ID!,
|
|
20
|
+
apiKey: process.env.MASTRA_API_KEY!,
|
|
21
|
+
}),
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Handles the full OAuth lifecycle including login URL generation, PKCE challenge/verification, callback handling, and session cookie management.
|
|
27
|
+
|
|
28
|
+
### Patch Changes
|
|
29
|
+
|
|
30
|
+
- Updated dependencies [[`504fc8b`](https://github.com/mastra-ai/mastra/commit/504fc8b9d0ddab717577ad3bf9c95ea4bd5377bd), [`f9c150b`](https://github.com/mastra-ai/mastra/commit/f9c150b7595ad05ad9cc9a11098e2944361e8c22), [`88de7e8`](https://github.com/mastra-ai/mastra/commit/88de7e8dfe4b7e1951a9e441bb33136e705ce24e), [`edee4b3`](https://github.com/mastra-ai/mastra/commit/edee4b37dff0af515fc7cc0e8d71ee39e6a762f0), [`3790c75`](https://github.com/mastra-ai/mastra/commit/3790c7578cc6a47d854eb12d89e6b1912867fe29), [`e7a235b`](https://github.com/mastra-ai/mastra/commit/e7a235be6472e0c870ed6c791ddb17c492dc188b), [`d51d298`](https://github.com/mastra-ai/mastra/commit/d51d298953967aab1f58ec965b644d109214f085), [`6dbeeb9`](https://github.com/mastra-ai/mastra/commit/6dbeeb94a8b1eebb727300d1a98961f882180794), [`d5f0d8d`](https://github.com/mastra-ai/mastra/commit/d5f0d8d6a03e515ddaa9b5da19b7e44b8357b07b), [`09c3b18`](https://github.com/mastra-ai/mastra/commit/09c3b1802ff14e243a8a8baea327440bc8cc2e32), [`b896379`](https://github.com/mastra-ai/mastra/commit/b8963791c6afa79484645fcec596a201f936b9a2), [`85c84eb`](https://github.com/mastra-ai/mastra/commit/85c84ebb78aebfcba9d209c8e152b16d7a00cb71), [`a89272a`](https://github.com/mastra-ai/mastra/commit/a89272a5d71939b9fcd284e6a6dc1dd091a6bdcf), [`ee9c8df`](https://github.com/mastra-ai/mastra/commit/ee9c8df644f19d055af5f496bf4942705f5a47b7), [`77b4a25`](https://github.com/mastra-ai/mastra/commit/77b4a254e51907f8ff3a3ba95596a18e93ae4b35), [`276246e`](https://github.com/mastra-ai/mastra/commit/276246e0b9066a1ea48bbc70df84dbe528daaf99), [`08ecfdb`](https://github.com/mastra-ai/mastra/commit/08ecfdbdad6fb8285deef86a034bdf4a6047cfca), [`d5f628c`](https://github.com/mastra-ai/mastra/commit/d5f628ca86c6f6f3ff1035d52f635df32dd81cab), [`524c0f3`](https://github.com/mastra-ai/mastra/commit/524c0f3c434c3d9d18f66338dcef383d6161b59c), [`c18a0e9`](https://github.com/mastra-ai/mastra/commit/c18a0e9cef1e4ca004b2963d35e4cfc031971eac), [`4bd21ea`](https://github.com/mastra-ai/mastra/commit/4bd21ea43d44d0a0427414fc047577f9f0aa3bec), [`115a7a4`](https://github.com/mastra-ai/mastra/commit/115a7a47db5e9896fec12ae6507501adb9ec89bf), [`22a48ae`](https://github.com/mastra-ai/mastra/commit/22a48ae2513eb54d8d79dad361fddbca97a155e8), [`3c6ef79`](https://github.com/mastra-ai/mastra/commit/3c6ef798481e00d6d22563be2de98818fd4dd5e0), [`9311c17`](https://github.com/mastra-ai/mastra/commit/9311c17d7a0640d9c4da2e71b814dc67c57c6369), [`7edf78f`](https://github.com/mastra-ai/mastra/commit/7edf78f80422c43e84585f08ba11df0d4d0b73c5), [`1c4221c`](https://github.com/mastra-ai/mastra/commit/1c4221cf6032ec98d0e094d4ee11da3e48490d96), [`d25b9ea`](https://github.com/mastra-ai/mastra/commit/d25b9eabd400167255a97b690ffbc4ee4097ded5), [`fe1ce5c`](https://github.com/mastra-ai/mastra/commit/fe1ce5c9211c03d561606fda95cbfe7df1d9a9b5), [`b03c0e0`](https://github.com/mastra-ai/mastra/commit/b03c0e0389a799523929a458b0509c9e4244d562), [`0a8366b`](https://github.com/mastra-ai/mastra/commit/0a8366b0a692fcdde56c4d526e4cf03c502ae4ac), [`85664e9`](https://github.com/mastra-ai/mastra/commit/85664e9fd857320fbc245e301f764f45f66f32a3), [`bc79650`](https://github.com/mastra-ai/mastra/commit/bc796500c6e0334faa158a96077e3fb332274869), [`9257d01`](https://github.com/mastra-ai/mastra/commit/9257d01d1366d81f84c582fe02b5e200cf9621f4), [`3a3a59e`](https://github.com/mastra-ai/mastra/commit/3a3a59e8ffaa6a985fe3d9a126a3f5ade11a6724), [`3108d4e`](https://github.com/mastra-ai/mastra/commit/3108d4e649c9fddbf03253a6feeb388a5fa9fa5a), [`0c33b2c`](https://github.com/mastra-ai/mastra/commit/0c33b2c9db537f815e1c59e2c898ffce2e395a79), [`191e5bd`](https://github.com/mastra-ai/mastra/commit/191e5bd29b82f5bda35243945790da7bc7b695c2), [`f77cd94`](https://github.com/mastra-ai/mastra/commit/f77cd94c44eabed490384e7d19232a865e13214c), [`e8135c7`](https://github.com/mastra-ai/mastra/commit/e8135c7e300dac5040670eec7eab896ac6092e30), [`daca48f`](https://github.com/mastra-ai/mastra/commit/daca48f0fb17b7ae0b62a2ac40cf0e491b2fd0b7), [`257d14f`](https://github.com/mastra-ai/mastra/commit/257d14faca5931f2e4186fc165b6f0b1f915deee), [`352f25d`](https://github.com/mastra-ai/mastra/commit/352f25da316b24cdd5b410fd8dddf6a8b763da2a), [`93477d0`](https://github.com/mastra-ai/mastra/commit/93477d0769b8a13ea5ed73d508d967fb23eaeed9), [`31c78b3`](https://github.com/mastra-ai/mastra/commit/31c78b3eb28f58a8017f1dcc795c33214d87feac), [`0bc0720`](https://github.com/mastra-ai/mastra/commit/0bc07201095791858087cc56f353fcd65e87ab54), [`36516ac`](https://github.com/mastra-ai/mastra/commit/36516aca1021cbeb42e74751b46a2614101f37c8), [`e947652`](https://github.com/mastra-ai/mastra/commit/e9476527fdecb4449e54570e80dfaf8466901254), [`3c6ef79`](https://github.com/mastra-ai/mastra/commit/3c6ef798481e00d6d22563be2de98818fd4dd5e0), [`9257d01`](https://github.com/mastra-ai/mastra/commit/9257d01d1366d81f84c582fe02b5e200cf9621f4), [`ec248f6`](https://github.com/mastra-ai/mastra/commit/ec248f6b56e8a037c066c49b2178e2507471d988)]:
|
|
31
|
+
- @mastra/core@1.9.0-alpha.0
|
|
32
|
+
- @mastra/auth@1.0.0
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
Portions of this software are licensed as follows:
|
|
2
|
+
|
|
3
|
+
- All content that resides under any directory named "ee/" within this
|
|
4
|
+
repository, including but not limited to:
|
|
5
|
+
- `packages/core/src/auth/ee/`
|
|
6
|
+
- `packages/server/src/server/auth/ee/`
|
|
7
|
+
is licensed under the license defined in `ee/LICENSE`.
|
|
8
|
+
|
|
9
|
+
- All third-party components incorporated into the Mastra Software are
|
|
10
|
+
licensed under the original license provided by the owner of the
|
|
11
|
+
applicable component.
|
|
12
|
+
|
|
13
|
+
- Content outside of the above-mentioned directories or restrictions is
|
|
14
|
+
available under the "Apache License 2.0" as defined below.
|
|
15
|
+
|
|
16
|
+
# Apache License 2.0
|
|
17
|
+
|
|
18
|
+
Copyright (c) 2025 Kepler Software, Inc.
|
|
19
|
+
|
|
20
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
21
|
+
you may not use this file except in compliance with the License.
|
|
22
|
+
You may obtain a copy of the License at
|
|
23
|
+
|
|
24
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
25
|
+
|
|
26
|
+
Unless required by applicable law or agreed to in writing, software
|
|
27
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
28
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
29
|
+
See the License for the specific language governing permissions and
|
|
30
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,67 @@
|
|
|
1
1
|
# @mastra/auth-cloud
|
|
2
2
|
|
|
3
|
-
Mastra Cloud
|
|
3
|
+
Mastra Cloud authentication provider with PKCE OAuth flow.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @mastra/auth-cloud
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { Mastra } from '@mastra/core/mastra';
|
|
15
|
+
import { MastraCloudAuth } from '@mastra/auth-cloud';
|
|
16
|
+
|
|
17
|
+
const auth = new MastraCloudAuth({
|
|
18
|
+
projectId: process.env.MASTRA_PROJECT_ID!,
|
|
19
|
+
// Optional: defaults to https://cloud.mastra.ai
|
|
20
|
+
baseUrl: process.env.MASTRA_CLOUD_URL,
|
|
21
|
+
// Optional: defaults to /auth/callback
|
|
22
|
+
redirectPath: '/auth/callback',
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const mastra = new Mastra({
|
|
26
|
+
server: {
|
|
27
|
+
auth,
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Configuration
|
|
33
|
+
|
|
34
|
+
| Option | Required | Default | Description |
|
|
35
|
+
| -------------- | -------- | ------------------------- | ------------------------------- |
|
|
36
|
+
| `projectId` | Yes | - | Project ID from cloud.mastra.ai |
|
|
37
|
+
| `baseUrl` | No | `https://cloud.mastra.ai` | Mastra Cloud base URL |
|
|
38
|
+
| `redirectPath` | No | `/auth/callback` | OAuth callback path |
|
|
39
|
+
| `cookieName` | No | `mastra_session` | Session cookie name |
|
|
40
|
+
|
|
41
|
+
## Authentication Flow
|
|
42
|
+
|
|
43
|
+
This package implements PKCE OAuth flow with Mastra Cloud:
|
|
44
|
+
|
|
45
|
+
1. User clicks login, redirected to Mastra Cloud with code challenge
|
|
46
|
+
2. User authenticates via Mastra Cloud (GitHub OAuth)
|
|
47
|
+
3. Mastra Cloud redirects back with authorization code
|
|
48
|
+
4. Package exchanges code + verifier for session token
|
|
49
|
+
5. Session token stored in HttpOnly cookie
|
|
50
|
+
|
|
51
|
+
## API
|
|
52
|
+
|
|
53
|
+
### `MastraCloudAuth`
|
|
54
|
+
|
|
55
|
+
The main authentication provider class implementing `MastraAuthProvider`.
|
|
56
|
+
|
|
57
|
+
### Methods
|
|
58
|
+
|
|
59
|
+
- `getLoginUrl(state?)` - Get OAuth login URL with PKCE
|
|
60
|
+
- `handleCallback(code, verifier)` - Exchange code for session
|
|
61
|
+
- `verifyToken(token)` - Verify session and get user with role
|
|
62
|
+
- `refreshSession(token)` - Refresh expiring session
|
|
63
|
+
- `logout(token)` - Invalidate session
|
|
64
|
+
|
|
65
|
+
## License
|
|
66
|
+
|
|
67
|
+
Apache-2.0
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MastraCloudAuthProvider - Server integration for Mastra Cloud authentication.
|
|
3
|
+
*
|
|
4
|
+
* Extends MastraAuthProvider and implements ISSOProvider, ISessionProvider,
|
|
5
|
+
* and IUserProvider interfaces to integrate with Mastra server middleware.
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
import type { IUserProvider, ISSOProvider, ISessionProvider, Session, SSOCallbackResult, SSOLoginConfig } from '@mastra/core/auth';
|
|
10
|
+
import type { EEUser } from '@mastra/core/auth/ee';
|
|
11
|
+
import type { MastraAuthProviderOptions } from '@mastra/core/server';
|
|
12
|
+
import { MastraAuthProvider } from '@mastra/core/server';
|
|
13
|
+
import type { HonoRequest } from 'hono';
|
|
14
|
+
import type { CloudUser } from './types.js';
|
|
15
|
+
/**
|
|
16
|
+
* Configuration options for MastraCloudAuthProvider.
|
|
17
|
+
*/
|
|
18
|
+
export interface MastraCloudAuthProviderOptions extends MastraAuthProviderOptions<CloudUser> {
|
|
19
|
+
/** Mastra Cloud project ID */
|
|
20
|
+
projectId: string;
|
|
21
|
+
/** Base URL of Mastra Cloud API (e.g., https://cloud.mastra.ai) */
|
|
22
|
+
cloudBaseUrl: string;
|
|
23
|
+
/** OAuth callback URL for your application */
|
|
24
|
+
callbackUrl: string;
|
|
25
|
+
/** Whether running in production (adds Secure flag to cookies) */
|
|
26
|
+
isProduction?: boolean;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Mastra Cloud authentication provider for server integration.
|
|
30
|
+
*
|
|
31
|
+
* Wraps the MastraCloudAuth client and implements the required interfaces
|
|
32
|
+
* for Mastra server middleware. Provides SSO login, session management,
|
|
33
|
+
* and user awareness.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* import { MastraCloudAuthProvider } from '@mastra/auth-cloud';
|
|
38
|
+
*
|
|
39
|
+
* const auth = new MastraCloudAuthProvider({
|
|
40
|
+
* cloudBaseUrl: 'https://cloud.mastra.ai',
|
|
41
|
+
* callbackUrl: 'https://myapp.com/auth/callback',
|
|
42
|
+
* });
|
|
43
|
+
*
|
|
44
|
+
* const mastra = new Mastra({
|
|
45
|
+
* auth,
|
|
46
|
+
* // ...
|
|
47
|
+
* });
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export declare class MastraCloudAuthProvider extends MastraAuthProvider<CloudUser> implements IUserProvider<EEUser>, ISSOProvider<EEUser>, ISessionProvider<Session> {
|
|
51
|
+
private client;
|
|
52
|
+
/** Marker for EE license exemption - MastraCloudAuth is exempt */
|
|
53
|
+
readonly isMastraCloudAuth = true;
|
|
54
|
+
/**
|
|
55
|
+
* Cookie header for handleCallback PKCE validation.
|
|
56
|
+
* Set via setCallbackCookieHeader() before handleCallback() is called.
|
|
57
|
+
* @internal
|
|
58
|
+
*/
|
|
59
|
+
private _lastCallbackCookieHeader;
|
|
60
|
+
constructor(options: MastraCloudAuthProviderOptions);
|
|
61
|
+
/**
|
|
62
|
+
* Set cookie header for handleCallback PKCE validation.
|
|
63
|
+
* Must be called before handleCallback() to pass cookie header.
|
|
64
|
+
*
|
|
65
|
+
* @param cookieHeader - Cookie header from original request
|
|
66
|
+
*/
|
|
67
|
+
setCallbackCookieHeader(cookieHeader: string | null): void;
|
|
68
|
+
/**
|
|
69
|
+
* Authenticate a bearer token or session cookie.
|
|
70
|
+
*
|
|
71
|
+
* Checks session cookie first, falls back to bearer token for API clients.
|
|
72
|
+
*
|
|
73
|
+
* @param token - Bearer token (from Authorization header)
|
|
74
|
+
* @param request - Hono or raw Request
|
|
75
|
+
* @returns Authenticated user with role, or null if invalid
|
|
76
|
+
*/
|
|
77
|
+
authenticateToken(token: string, request: HonoRequest | Request): Promise<CloudUser | null>;
|
|
78
|
+
/**
|
|
79
|
+
* Authorize a user for access.
|
|
80
|
+
*
|
|
81
|
+
* Simple validation - detailed permission checking happens in server
|
|
82
|
+
* middleware via checkRoutePermission(), not authorizeUser().
|
|
83
|
+
*
|
|
84
|
+
* @param user - Authenticated user
|
|
85
|
+
* @returns True if user has valid id
|
|
86
|
+
*/
|
|
87
|
+
authorizeUser(user: CloudUser): boolean;
|
|
88
|
+
/**
|
|
89
|
+
* Cached login result for getLoginCookies() to retrieve cookies.
|
|
90
|
+
* @internal
|
|
91
|
+
*/
|
|
92
|
+
private _lastLoginResult;
|
|
93
|
+
/**
|
|
94
|
+
* Get URL to redirect user to for SSO login.
|
|
95
|
+
*
|
|
96
|
+
* @param redirectUri - Callback URL after authentication
|
|
97
|
+
* @param state - State parameter (format: uuid|encodedPostLoginRedirect)
|
|
98
|
+
* @returns Full authorization URL
|
|
99
|
+
*/
|
|
100
|
+
getLoginUrl(redirectUri: string, state: string): string;
|
|
101
|
+
/**
|
|
102
|
+
* Get cookies to set during login redirect (PKCE verifier).
|
|
103
|
+
* Must be called after getLoginUrl() in same request.
|
|
104
|
+
*
|
|
105
|
+
* @returns Array of Set-Cookie header values
|
|
106
|
+
*/
|
|
107
|
+
getLoginCookies(): string[] | undefined;
|
|
108
|
+
/**
|
|
109
|
+
* Handle OAuth callback, exchange code for tokens and user.
|
|
110
|
+
*
|
|
111
|
+
* @param code - Authorization code from callback
|
|
112
|
+
* @param state - State parameter for CSRF validation
|
|
113
|
+
* @returns User, tokens, and session cookies
|
|
114
|
+
*/
|
|
115
|
+
handleCallback(code: string, state: string): Promise<SSOCallbackResult<EEUser>>;
|
|
116
|
+
/**
|
|
117
|
+
* Get configuration for rendering login button in UI.
|
|
118
|
+
*
|
|
119
|
+
* @returns Login button configuration
|
|
120
|
+
*/
|
|
121
|
+
getLoginButtonConfig(): SSOLoginConfig;
|
|
122
|
+
/**
|
|
123
|
+
* Get logout URL for client-side redirect.
|
|
124
|
+
* Requires the request to extract the session token for id_token_hint.
|
|
125
|
+
*
|
|
126
|
+
* @param redirectUri - URL to redirect to after logout
|
|
127
|
+
* @param request - Request to extract session token from
|
|
128
|
+
* @returns Logout URL with redirect and token parameters, or null if no session
|
|
129
|
+
*/
|
|
130
|
+
getLogoutUrl(redirectUri: string, request?: Request): string | null;
|
|
131
|
+
/**
|
|
132
|
+
* Create a new session for a user.
|
|
133
|
+
*
|
|
134
|
+
* For Cloud auth, sessions are created via handleCallback.
|
|
135
|
+
* This method builds a Session object for interface compatibility.
|
|
136
|
+
*
|
|
137
|
+
* @param userId - User to create session for
|
|
138
|
+
* @param metadata - Optional metadata (accessToken can be passed here)
|
|
139
|
+
* @returns Session object
|
|
140
|
+
*/
|
|
141
|
+
createSession(userId: string, metadata?: Record<string, unknown>): Promise<Session>;
|
|
142
|
+
/**
|
|
143
|
+
* Validate a session and return it if valid.
|
|
144
|
+
*
|
|
145
|
+
* @param sessionId - Session token to validate
|
|
146
|
+
* @returns Session object or null if invalid/expired
|
|
147
|
+
*/
|
|
148
|
+
validateSession(sessionId: string): Promise<Session | null>;
|
|
149
|
+
/**
|
|
150
|
+
* Destroy a session (logout).
|
|
151
|
+
*
|
|
152
|
+
* @param sessionId - Session token to destroy
|
|
153
|
+
*/
|
|
154
|
+
destroySession(sessionId: string): Promise<void>;
|
|
155
|
+
/**
|
|
156
|
+
* Refresh a session, extending its expiry.
|
|
157
|
+
* Cloud handles refresh internally, so just validate.
|
|
158
|
+
*
|
|
159
|
+
* @param sessionId - Session token to refresh
|
|
160
|
+
* @returns Session object or null if invalid
|
|
161
|
+
*/
|
|
162
|
+
refreshSession(sessionId: string): Promise<Session | null>;
|
|
163
|
+
/**
|
|
164
|
+
* Extract session ID from an incoming request.
|
|
165
|
+
*
|
|
166
|
+
* @param request - Incoming HTTP request
|
|
167
|
+
* @returns Session token or null if not present
|
|
168
|
+
*/
|
|
169
|
+
getSessionIdFromRequest(request: Request): string | null;
|
|
170
|
+
/**
|
|
171
|
+
* Create response headers to set session cookie.
|
|
172
|
+
*
|
|
173
|
+
* @param session - Session to encode (id is the access token)
|
|
174
|
+
* @returns Headers object with Set-Cookie
|
|
175
|
+
*/
|
|
176
|
+
getSessionHeaders(session: Session): Record<string, string>;
|
|
177
|
+
/**
|
|
178
|
+
* Create response headers to clear session (for logout).
|
|
179
|
+
*
|
|
180
|
+
* @returns Headers object to clear session cookie
|
|
181
|
+
*/
|
|
182
|
+
getClearSessionHeaders(): Record<string, string>;
|
|
183
|
+
/**
|
|
184
|
+
* Get current user from request (session cookie).
|
|
185
|
+
*
|
|
186
|
+
* @param request - Incoming HTTP request
|
|
187
|
+
* @returns User with role or null if not authenticated
|
|
188
|
+
*/
|
|
189
|
+
getCurrentUser(request: Request): Promise<CloudUser | null>;
|
|
190
|
+
/**
|
|
191
|
+
* Get user by ID.
|
|
192
|
+
* Cloud API doesn't have a /users/:id endpoint.
|
|
193
|
+
*
|
|
194
|
+
* @returns Always null (not supported)
|
|
195
|
+
*/
|
|
196
|
+
getUser(_userId: string): Promise<CloudUser | null>;
|
|
197
|
+
}
|
|
198
|
+
//# sourceMappingURL=auth-provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth-provider.d.ts","sourceRoot":"","sources":["../src/auth-provider.ts"],"names":[],"mappings":"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"}
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MastraCloudAuth client class.
|
|
3
|
+
* Facade composing OAuth and session modules into unified API.
|
|
4
|
+
*/
|
|
5
|
+
import type { LoginUrlResult, CallbackResult, VerifyResponse, CloudSession } from './types.js';
|
|
6
|
+
/**
|
|
7
|
+
* Configuration for MastraCloudAuth client.
|
|
8
|
+
*/
|
|
9
|
+
export interface MastraCloudAuthConfig {
|
|
10
|
+
/** Mastra Cloud project ID */
|
|
11
|
+
projectId: string;
|
|
12
|
+
/** Base URL of the Cloud API (e.g., https://cloud.mastra.ai) */
|
|
13
|
+
cloudBaseUrl: string;
|
|
14
|
+
/** OAuth callback URL for your application */
|
|
15
|
+
callbackUrl: string;
|
|
16
|
+
/** Whether running in production (adds Secure flag to cookies) */
|
|
17
|
+
isProduction?: boolean;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Mastra Cloud authentication client.
|
|
21
|
+
*
|
|
22
|
+
* Provides unified API for OAuth flow and session management.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* const auth = new MastraCloudAuth({
|
|
27
|
+
* cloudBaseUrl: 'https://cloud.mastra.ai',
|
|
28
|
+
* callbackUrl: 'https://myapp.com/auth/callback',
|
|
29
|
+
* });
|
|
30
|
+
*
|
|
31
|
+
* // Start login flow
|
|
32
|
+
* const { url, cookies } = auth.getLoginUrl({
|
|
33
|
+
* requestOrigin: 'https://myapp.com',
|
|
34
|
+
* });
|
|
35
|
+
*
|
|
36
|
+
* // After callback
|
|
37
|
+
* const result = await auth.handleCallback({
|
|
38
|
+
* code: 'auth_code',
|
|
39
|
+
* state: 'state_param',
|
|
40
|
+
* cookieHeader: request.headers.get('cookie'),
|
|
41
|
+
* });
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export declare class MastraCloudAuth {
|
|
45
|
+
private readonly config;
|
|
46
|
+
constructor(config: MastraCloudAuthConfig);
|
|
47
|
+
/**
|
|
48
|
+
* Generate login URL for OAuth authorization.
|
|
49
|
+
*
|
|
50
|
+
* @param options - Login options
|
|
51
|
+
* @returns URL to redirect to and cookies to set
|
|
52
|
+
*/
|
|
53
|
+
getLoginUrl(options: {
|
|
54
|
+
returnTo?: string;
|
|
55
|
+
requestOrigin: string;
|
|
56
|
+
}): LoginUrlResult;
|
|
57
|
+
/**
|
|
58
|
+
* Handle OAuth callback after authorization.
|
|
59
|
+
*
|
|
60
|
+
* @param options - Callback parameters
|
|
61
|
+
* @returns User info, tokens, and redirect URL
|
|
62
|
+
*/
|
|
63
|
+
handleCallback(options: {
|
|
64
|
+
code: string;
|
|
65
|
+
state: string;
|
|
66
|
+
cookieHeader: string | null;
|
|
67
|
+
}): Promise<CallbackResult>;
|
|
68
|
+
/**
|
|
69
|
+
* Verify an access token.
|
|
70
|
+
*
|
|
71
|
+
* @param token - Access token to verify
|
|
72
|
+
* @returns User and role information
|
|
73
|
+
*/
|
|
74
|
+
verifyToken(token: string): Promise<VerifyResponse>;
|
|
75
|
+
/**
|
|
76
|
+
* Validate an existing session.
|
|
77
|
+
*
|
|
78
|
+
* @param sessionToken - Session token to validate
|
|
79
|
+
* @returns Session data if valid, null otherwise
|
|
80
|
+
*/
|
|
81
|
+
validateSession(sessionToken: string): Promise<CloudSession | null>;
|
|
82
|
+
/**
|
|
83
|
+
* Destroy a session (server-side logout).
|
|
84
|
+
*
|
|
85
|
+
* @param sessionToken - Session token to destroy
|
|
86
|
+
*/
|
|
87
|
+
destroySession(sessionToken: string): Promise<void>;
|
|
88
|
+
/**
|
|
89
|
+
* Get the logout URL for client-side redirect.
|
|
90
|
+
*
|
|
91
|
+
* @param postLogoutRedirectUri - URL to redirect to after logout
|
|
92
|
+
* @param idTokenHint - The access token
|
|
93
|
+
* @returns Full logout URL with redirect and token parameters
|
|
94
|
+
*/
|
|
95
|
+
getLogoutUrl(postLogoutRedirectUri: string, idTokenHint: string): string;
|
|
96
|
+
/**
|
|
97
|
+
* Create Set-Cookie header value for session token.
|
|
98
|
+
*
|
|
99
|
+
* @param token - Session token to store
|
|
100
|
+
* @returns Set-Cookie header value
|
|
101
|
+
*/
|
|
102
|
+
setSessionCookie(token: string): string;
|
|
103
|
+
/**
|
|
104
|
+
* Create Set-Cookie header value to clear session cookie.
|
|
105
|
+
*
|
|
106
|
+
* @returns Set-Cookie header value
|
|
107
|
+
*/
|
|
108
|
+
clearSessionCookie(): string;
|
|
109
|
+
}
|
|
110
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAWH,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAE5F;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC,8BAA8B;IAC9B,SAAS,EAAE,MAAM,CAAC;IAClB,gEAAgE;IAChE,YAAY,EAAE,MAAM,CAAC;IACrB,8CAA8C;IAC9C,WAAW,EAAE,MAAM,CAAC;IACpB,kEAAkE;IAClE,YAAY,CAAC,EAAE,OAAO,CAAC;CACxB;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,qBAAa,eAAe;IAC1B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAwB;gBAEnC,MAAM,EAAE,qBAAqB;IAIzC;;;;;OAKG;IACH,WAAW,CAAC,OAAO,EAAE;QAAE,QAAQ,CAAC,EAAE,MAAM,CAAC;QAAC,aAAa,EAAE,MAAM,CAAA;KAAE,GAAG,cAAc;IAWlF;;;;;OAKG;IACH,cAAc,CAAC,OAAO,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,MAAM,GAAG,IAAI,CAAA;KAAE,GAAG,OAAO,CAAC,cAAc,CAAC;IAS9G;;;;;OAKG;IACH,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC;IAInD;;;;;OAKG;IACH,eAAe,CAAC,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,GAAG,IAAI,CAAC;IAInE;;;;OAIG;IACH,cAAc,CAAC,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAInD;;;;;;OAMG;IACH,YAAY,CAAC,qBAAqB,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,MAAM;IAIxE;;;;;OAKG;IACH,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAIvC;;;;OAIG;IACH,kBAAkB,IAAI,MAAM;CAG7B"}
|
package/dist/error.d.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auth error types and error class.
|
|
3
|
+
*
|
|
4
|
+
* Provides typed error handling for OAuth flow and session management.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Error codes for authentication-related failures.
|
|
8
|
+
*/
|
|
9
|
+
export type AuthErrorCode = 'invalid_state' | 'state_mismatch' | 'missing_code' | 'token_exchange_failed' | 'verification_failed' | 'session_invalid' | 'session_expired' | 'network_error' | 'cloud_api_error';
|
|
10
|
+
/**
|
|
11
|
+
* Options for AuthError constructor.
|
|
12
|
+
*/
|
|
13
|
+
export interface AuthErrorOptions {
|
|
14
|
+
cause?: Error;
|
|
15
|
+
cloudCode?: string;
|
|
16
|
+
cloudMessage?: string;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Error class for authentication-related failures.
|
|
20
|
+
* Uses a code discriminator for programmatic error handling.
|
|
21
|
+
*/
|
|
22
|
+
export declare class AuthError extends Error {
|
|
23
|
+
readonly code: AuthErrorCode;
|
|
24
|
+
readonly cause?: Error;
|
|
25
|
+
readonly cloudCode?: string;
|
|
26
|
+
readonly cloudMessage?: string;
|
|
27
|
+
constructor(code: AuthErrorCode, message: string, options?: AuthErrorOptions);
|
|
28
|
+
/**
|
|
29
|
+
* Factory: OAuth state parameter is invalid or malformed.
|
|
30
|
+
*/
|
|
31
|
+
static invalidState(): AuthError;
|
|
32
|
+
/**
|
|
33
|
+
* Factory: OAuth state parameter does not match expected value.
|
|
34
|
+
*/
|
|
35
|
+
static stateMismatch(): AuthError;
|
|
36
|
+
/**
|
|
37
|
+
* Factory: Authorization code is missing from callback.
|
|
38
|
+
*/
|
|
39
|
+
static missingCode(): AuthError;
|
|
40
|
+
/**
|
|
41
|
+
* Factory: Token exchange with Cloud API failed.
|
|
42
|
+
*/
|
|
43
|
+
static tokenExchangeFailed(options?: AuthErrorOptions): AuthError;
|
|
44
|
+
/**
|
|
45
|
+
* Factory: Token verification failed.
|
|
46
|
+
*/
|
|
47
|
+
static verificationFailed(): AuthError;
|
|
48
|
+
/**
|
|
49
|
+
* Factory: Session is invalid.
|
|
50
|
+
*/
|
|
51
|
+
static sessionInvalid(): AuthError;
|
|
52
|
+
/**
|
|
53
|
+
* Factory: Session has expired.
|
|
54
|
+
*/
|
|
55
|
+
static sessionExpired(): AuthError;
|
|
56
|
+
/**
|
|
57
|
+
* Factory: Network error during API call.
|
|
58
|
+
*/
|
|
59
|
+
static networkError(cause?: Error): AuthError;
|
|
60
|
+
/**
|
|
61
|
+
* Factory: Cloud API returned an error.
|
|
62
|
+
*/
|
|
63
|
+
static cloudApiError(options?: AuthErrorOptions): AuthError;
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=error.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;GAEG;AACH,MAAM,MAAM,aAAa,GACrB,eAAe,GACf,gBAAgB,GAChB,cAAc,GACd,uBAAuB,GACvB,qBAAqB,GACrB,iBAAiB,GACjB,iBAAiB,GACjB,eAAe,GACf,iBAAiB,CAAC;AAEtB;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED;;;GAGG;AACH,qBAAa,SAAU,SAAQ,KAAK;IAClC,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAC;IAC7B,SAAkB,KAAK,CAAC,EAAE,KAAK,CAAC;IAChC,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC;gBAEnB,IAAI,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,gBAAgB;IAW5E;;OAEG;IACH,MAAM,CAAC,YAAY,IAAI,SAAS;IAIhC;;OAEG;IACH,MAAM,CAAC,aAAa,IAAI,SAAS;IAIjC;;OAEG;IACH,MAAM,CAAC,WAAW,IAAI,SAAS;IAI/B;;OAEG;IACH,MAAM,CAAC,mBAAmB,CAAC,OAAO,CAAC,EAAE,gBAAgB,GAAG,SAAS;IAIjE;;OAEG;IACH,MAAM,CAAC,kBAAkB,IAAI,SAAS;IAItC;;OAEG;IACH,MAAM,CAAC,cAAc,IAAI,SAAS;IAIlC;;OAEG;IACH,MAAM,CAAC,cAAc,IAAI,SAAS;IAIlC;;OAEG;IACH,MAAM,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE,KAAK,GAAG,SAAS;IAI7C;;OAEG;IACH,MAAM,CAAC,aAAa,CAAC,OAAO,CAAC,EAAE,gBAAgB,GAAG,SAAS;CAI5D"}
|