Skip to content

Commit ccf8448

Browse files
feat: Expose authenticated request context
Stainless-Generated-From: 6e370a57405066ad44066d88f375704812affb83
1 parent b6b6518 commit ccf8448

6 files changed

Lines changed: 165 additions & 1 deletion

File tree

.stats.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
configured_endpoints: 133
1+
configured_endpoints: 134

api.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,16 @@ Methods:
261261

262262
# Auth
263263

264+
## Context
265+
266+
Types:
267+
268+
- <code><a href="./src/resources/auth/context.ts">AuthContext</a></code>
269+
270+
Methods:
271+
272+
- <code title="get /auth/context">client.auth.context.<a href="./src/resources/auth/context.ts">retrieve</a>() -> AuthContext</code>
273+
264274
## Connections
265275

266276
Types:

src/resources/auth/auth.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,20 @@ import {
2121
SubmitFieldsRequest,
2222
SubmitFieldsResponse,
2323
} from './connections';
24+
import * as ContextAPI from './context';
25+
import { AuthContext, Context } from './context';
2426

2527
export class Auth extends APIResource {
28+
context: ContextAPI.Context = new ContextAPI.Context(this._client);
2629
connections: ConnectionsAPI.Connections = new ConnectionsAPI.Connections(this._client);
2730
}
2831

32+
Auth.Context = Context;
2933
Auth.Connections = Connections;
3034

3135
export declare namespace Auth {
36+
export { Context as Context, type AuthContext as AuthContext };
37+
3238
export {
3339
Connections as Connections,
3440
type LoginResponse as LoginResponse,

src/resources/auth/context.ts

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2+
3+
import { APIResource } from '../../core/resource';
4+
import { APIPromise } from '../../core/api-promise';
5+
import { RequestOptions } from '../../internal/request-options';
6+
7+
/**
8+
* Inspect the identity and authorization context for the current request.
9+
*/
10+
export class Context extends APIResource {
11+
/**
12+
* Returns the authenticated principal, organization, credential scope, and
13+
* effective request scope. The response is derived from the verified request
14+
* context and does not expose credential secrets.
15+
*
16+
* @example
17+
* ```ts
18+
* const authContext = await client.auth.context.retrieve();
19+
* ```
20+
*/
21+
retrieve(options?: RequestOptions): APIPromise<AuthContext> {
22+
return this._client.get('/auth/context', options);
23+
}
24+
}
25+
26+
/**
27+
* The identity and authorization context resolved for the current request.
28+
*/
29+
export interface AuthContext {
30+
authentication: AuthContext.Authentication;
31+
32+
/**
33+
* The credential's maximum scope and the effective scope selected for this
34+
* request. Future permission data can be added without changing scope semantics.
35+
*/
36+
authorization: AuthContext.Authorization;
37+
38+
organization: AuthContext.Organization;
39+
40+
principal: AuthContext.Principal;
41+
}
42+
43+
export namespace AuthContext {
44+
export interface Authentication {
45+
/**
46+
* The API key ID when authenticated with an API key; null for session credentials.
47+
*/
48+
credential_id: string | null;
49+
50+
/**
51+
* The credential format used to authenticate the request.
52+
*/
53+
method: 'api_key' | 'jwt';
54+
55+
/**
56+
* The source classification resolved by authentication middleware.
57+
*/
58+
source: 'api_key' | 'oauth' | 'dashboard';
59+
}
60+
61+
/**
62+
* The credential's maximum scope and the effective scope selected for this
63+
* request. Future permission data can be added without changing scope semantics.
64+
*/
65+
export interface Authorization {
66+
/**
67+
* A scope within the authenticated organization. A null project_id represents
68+
* organization-wide scope.
69+
*/
70+
credential_scope: Authorization.CredentialScope;
71+
72+
/**
73+
* A scope within the authenticated organization. A null project_id represents
74+
* organization-wide scope.
75+
*/
76+
effective_scope: Authorization.EffectiveScope;
77+
}
78+
79+
export namespace Authorization {
80+
/**
81+
* A scope within the authenticated organization. A null project_id represents
82+
* organization-wide scope.
83+
*/
84+
export interface CredentialScope {
85+
/**
86+
* The Kernel project ID, or null when the scope is organization-wide.
87+
*/
88+
project_id: string | null;
89+
}
90+
91+
/**
92+
* A scope within the authenticated organization. A null project_id represents
93+
* organization-wide scope.
94+
*/
95+
export interface EffectiveScope {
96+
/**
97+
* The Kernel project ID, or null when the scope is organization-wide.
98+
*/
99+
project_id: string | null;
100+
}
101+
}
102+
103+
export interface Organization {
104+
/**
105+
* The authenticated Kernel organization ID.
106+
*/
107+
id: string;
108+
}
109+
110+
export interface Principal {
111+
/**
112+
* The API key ID for API-key principals or user ID for user principals.
113+
*/
114+
id: string;
115+
116+
/**
117+
* The kind of principal authenticated for the request.
118+
*/
119+
type: 'api_key' | 'user';
120+
}
121+
}
122+
123+
export declare namespace Context {
124+
export { type AuthContext as AuthContext };
125+
}

src/resources/auth/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ export {
2020
type ManagedAuthsOffsetPagination,
2121
type ManagedAuthTimelineEventsOffsetPagination,
2222
} from './connections';
23+
export { Context, type AuthContext } from './context';
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2+
3+
import Kernel from '@onkernel/sdk';
4+
5+
const client = new Kernel({
6+
apiKey: 'My API Key',
7+
baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010',
8+
});
9+
10+
describe('resource context', () => {
11+
// Mock server tests are disabled
12+
test.skip('retrieve', async () => {
13+
const responsePromise = client.auth.context.retrieve();
14+
const rawResponse = await responsePromise.asResponse();
15+
expect(rawResponse).toBeInstanceOf(Response);
16+
const response = await responsePromise;
17+
expect(response).not.toBeInstanceOf(Response);
18+
const dataAndResponse = await responsePromise.withResponse();
19+
expect(dataAndResponse.data).toBe(response);
20+
expect(dataAndResponse.response).toBe(rawResponse);
21+
});
22+
});

0 commit comments

Comments
 (0)