Skip to content

Commit 9da2570

Browse files
feat: Expose authenticated request context
Stainless-Generated-From: b06245d06729c7754c443005d0479ad52efa2b7f
1 parent 18fdd7b commit 9da2570

8 files changed

Lines changed: 391 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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,18 @@ Methods:
294294

295295
# Auth
296296

297+
## Context
298+
299+
Types:
300+
301+
```python
302+
from kernel.types.auth import AuthContext
303+
```
304+
305+
Methods:
306+
307+
- <code title="get /auth/context">client.auth.context.<a href="./src/kernel/resources/auth/context.py">retrieve</a>() -> <a href="./src/kernel/types/auth/auth_context.py">AuthContext</a></code>
308+
297309
## Connections
298310

299311
Types:

src/kernel/resources/auth/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
AuthResourceWithStreamingResponse,
99
AsyncAuthResourceWithStreamingResponse,
1010
)
11+
from .context import (
12+
ContextResource,
13+
AsyncContextResource,
14+
ContextResourceWithRawResponse,
15+
AsyncContextResourceWithRawResponse,
16+
ContextResourceWithStreamingResponse,
17+
AsyncContextResourceWithStreamingResponse,
18+
)
1119
from .connections import (
1220
ConnectionsResource,
1321
AsyncConnectionsResource,
@@ -18,6 +26,12 @@
1826
)
1927

2028
__all__ = [
29+
"ContextResource",
30+
"AsyncContextResource",
31+
"ContextResourceWithRawResponse",
32+
"AsyncContextResourceWithRawResponse",
33+
"ContextResourceWithStreamingResponse",
34+
"AsyncContextResourceWithStreamingResponse",
2135
"ConnectionsResource",
2236
"AsyncConnectionsResource",
2337
"ConnectionsResourceWithRawResponse",

src/kernel/resources/auth/auth.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
from __future__ import annotations
44

5+
from .context import (
6+
ContextResource,
7+
AsyncContextResource,
8+
ContextResourceWithRawResponse,
9+
AsyncContextResourceWithRawResponse,
10+
ContextResourceWithStreamingResponse,
11+
AsyncContextResourceWithStreamingResponse,
12+
)
513
from ..._compat import cached_property
614
from ..._resource import SyncAPIResource, AsyncAPIResource
715
from .connections import (
@@ -17,6 +25,11 @@
1725

1826

1927
class AuthResource(SyncAPIResource):
28+
@cached_property
29+
def context(self) -> ContextResource:
30+
"""Inspect the identity and authorization context for the current request."""
31+
return ContextResource(self._client)
32+
2033
@cached_property
2134
def connections(self) -> ConnectionsResource:
2235
"""Create and manage auth connections for automated credential capture and login."""
@@ -43,6 +56,11 @@ def with_streaming_response(self) -> AuthResourceWithStreamingResponse:
4356

4457

4558
class AsyncAuthResource(AsyncAPIResource):
59+
@cached_property
60+
def context(self) -> AsyncContextResource:
61+
"""Inspect the identity and authorization context for the current request."""
62+
return AsyncContextResource(self._client)
63+
4664
@cached_property
4765
def connections(self) -> AsyncConnectionsResource:
4866
"""Create and manage auth connections for automated credential capture and login."""
@@ -72,6 +90,11 @@ class AuthResourceWithRawResponse:
7290
def __init__(self, auth: AuthResource) -> None:
7391
self._auth = auth
7492

93+
@cached_property
94+
def context(self) -> ContextResourceWithRawResponse:
95+
"""Inspect the identity and authorization context for the current request."""
96+
return ContextResourceWithRawResponse(self._auth.context)
97+
7598
@cached_property
7699
def connections(self) -> ConnectionsResourceWithRawResponse:
77100
"""Create and manage auth connections for automated credential capture and login."""
@@ -82,6 +105,11 @@ class AsyncAuthResourceWithRawResponse:
82105
def __init__(self, auth: AsyncAuthResource) -> None:
83106
self._auth = auth
84107

108+
@cached_property
109+
def context(self) -> AsyncContextResourceWithRawResponse:
110+
"""Inspect the identity and authorization context for the current request."""
111+
return AsyncContextResourceWithRawResponse(self._auth.context)
112+
85113
@cached_property
86114
def connections(self) -> AsyncConnectionsResourceWithRawResponse:
87115
"""Create and manage auth connections for automated credential capture and login."""
@@ -92,6 +120,11 @@ class AuthResourceWithStreamingResponse:
92120
def __init__(self, auth: AuthResource) -> None:
93121
self._auth = auth
94122

123+
@cached_property
124+
def context(self) -> ContextResourceWithStreamingResponse:
125+
"""Inspect the identity and authorization context for the current request."""
126+
return ContextResourceWithStreamingResponse(self._auth.context)
127+
95128
@cached_property
96129
def connections(self) -> ConnectionsResourceWithStreamingResponse:
97130
"""Create and manage auth connections for automated credential capture and login."""
@@ -102,6 +135,11 @@ class AsyncAuthResourceWithStreamingResponse:
102135
def __init__(self, auth: AsyncAuthResource) -> None:
103136
self._auth = auth
104137

138+
@cached_property
139+
def context(self) -> AsyncContextResourceWithStreamingResponse:
140+
"""Inspect the identity and authorization context for the current request."""
141+
return AsyncContextResourceWithStreamingResponse(self._auth.context)
142+
105143
@cached_property
106144
def connections(self) -> AsyncConnectionsResourceWithStreamingResponse:
107145
"""Create and manage auth connections for automated credential capture and login."""
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2+
3+
from __future__ import annotations
4+
5+
import httpx
6+
7+
from ..._types import Body, Query, Headers, NotGiven, not_given
8+
from ..._compat import cached_property
9+
from ..._resource import SyncAPIResource, AsyncAPIResource
10+
from ..._response import (
11+
to_raw_response_wrapper,
12+
to_streamed_response_wrapper,
13+
async_to_raw_response_wrapper,
14+
async_to_streamed_response_wrapper,
15+
)
16+
from ..._base_client import make_request_options
17+
from ...types.auth.auth_context import AuthContext
18+
19+
__all__ = ["ContextResource", "AsyncContextResource"]
20+
21+
22+
class ContextResource(SyncAPIResource):
23+
"""Inspect the identity and authorization context for the current request."""
24+
25+
@cached_property
26+
def with_raw_response(self) -> ContextResourceWithRawResponse:
27+
"""
28+
This property can be used as a prefix for any HTTP method call to return
29+
the raw response object instead of the parsed content.
30+
31+
For more information, see https://www.github.com/kernel/kernel-python-sdk#accessing-raw-response-data-eg-headers
32+
"""
33+
return ContextResourceWithRawResponse(self)
34+
35+
@cached_property
36+
def with_streaming_response(self) -> ContextResourceWithStreamingResponse:
37+
"""
38+
An alternative to `.with_raw_response` that doesn't eagerly read the response body.
39+
40+
For more information, see https://www.github.com/kernel/kernel-python-sdk#with_streaming_response
41+
"""
42+
return ContextResourceWithStreamingResponse(self)
43+
44+
def retrieve(
45+
self,
46+
*,
47+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
48+
# The extra values given here take precedence over values defined on the client or passed to this method.
49+
extra_headers: Headers | None = None,
50+
extra_query: Query | None = None,
51+
extra_body: Body | None = None,
52+
timeout: float | httpx.Timeout | None | NotGiven = not_given,
53+
) -> AuthContext:
54+
"""
55+
Returns the authenticated principal, organization, credential scope, and
56+
effective request scope. The response is derived from the verified request
57+
context and does not expose credential secrets.
58+
"""
59+
return self._get(
60+
"/auth/context",
61+
options=make_request_options(
62+
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
63+
),
64+
cast_to=AuthContext,
65+
)
66+
67+
68+
class AsyncContextResource(AsyncAPIResource):
69+
"""Inspect the identity and authorization context for the current request."""
70+
71+
@cached_property
72+
def with_raw_response(self) -> AsyncContextResourceWithRawResponse:
73+
"""
74+
This property can be used as a prefix for any HTTP method call to return
75+
the raw response object instead of the parsed content.
76+
77+
For more information, see https://www.github.com/kernel/kernel-python-sdk#accessing-raw-response-data-eg-headers
78+
"""
79+
return AsyncContextResourceWithRawResponse(self)
80+
81+
@cached_property
82+
def with_streaming_response(self) -> AsyncContextResourceWithStreamingResponse:
83+
"""
84+
An alternative to `.with_raw_response` that doesn't eagerly read the response body.
85+
86+
For more information, see https://www.github.com/kernel/kernel-python-sdk#with_streaming_response
87+
"""
88+
return AsyncContextResourceWithStreamingResponse(self)
89+
90+
async def retrieve(
91+
self,
92+
*,
93+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
94+
# The extra values given here take precedence over values defined on the client or passed to this method.
95+
extra_headers: Headers | None = None,
96+
extra_query: Query | None = None,
97+
extra_body: Body | None = None,
98+
timeout: float | httpx.Timeout | None | NotGiven = not_given,
99+
) -> AuthContext:
100+
"""
101+
Returns the authenticated principal, organization, credential scope, and
102+
effective request scope. The response is derived from the verified request
103+
context and does not expose credential secrets.
104+
"""
105+
return await self._get(
106+
"/auth/context",
107+
options=make_request_options(
108+
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
109+
),
110+
cast_to=AuthContext,
111+
)
112+
113+
114+
class ContextResourceWithRawResponse:
115+
def __init__(self, context: ContextResource) -> None:
116+
self._context = context
117+
118+
self.retrieve = to_raw_response_wrapper(
119+
context.retrieve,
120+
)
121+
122+
123+
class AsyncContextResourceWithRawResponse:
124+
def __init__(self, context: AsyncContextResource) -> None:
125+
self._context = context
126+
127+
self.retrieve = async_to_raw_response_wrapper(
128+
context.retrieve,
129+
)
130+
131+
132+
class ContextResourceWithStreamingResponse:
133+
def __init__(self, context: ContextResource) -> None:
134+
self._context = context
135+
136+
self.retrieve = to_streamed_response_wrapper(
137+
context.retrieve,
138+
)
139+
140+
141+
class AsyncContextResourceWithStreamingResponse:
142+
def __init__(self, context: AsyncContextResource) -> None:
143+
self._context = context
144+
145+
self.retrieve = async_to_streamed_response_wrapper(
146+
context.retrieve,
147+
)

src/kernel/types/auth/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
from .auth_context import AuthContext as AuthContext
56
from .managed_auth import ManagedAuth as ManagedAuth
67
from .login_response import LoginResponse as LoginResponse
78
from .connection_list_params import ConnectionListParams as ConnectionListParams
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2+
3+
from typing import Optional
4+
from typing_extensions import Literal
5+
6+
from ..._models import BaseModel
7+
8+
__all__ = [
9+
"AuthContext",
10+
"Authentication",
11+
"Authorization",
12+
"AuthorizationCredentialScope",
13+
"AuthorizationEffectiveScope",
14+
"Organization",
15+
"Principal",
16+
]
17+
18+
19+
class Authentication(BaseModel):
20+
credential_id: Optional[str] = None
21+
"""
22+
The API key ID when authenticated with an API key; null for session credentials.
23+
"""
24+
25+
method: Literal["api_key", "jwt"]
26+
"""The credential format used to authenticate the request."""
27+
28+
source: Literal["api_key", "oauth", "dashboard"]
29+
"""The source classification resolved by authentication middleware."""
30+
31+
32+
class AuthorizationCredentialScope(BaseModel):
33+
"""A scope within the authenticated organization.
34+
35+
A null project_id represents organization-wide scope.
36+
"""
37+
38+
project_id: Optional[str] = None
39+
"""The Kernel project ID, or null when the scope is organization-wide."""
40+
41+
42+
class AuthorizationEffectiveScope(BaseModel):
43+
"""A scope within the authenticated organization.
44+
45+
A null project_id represents organization-wide scope.
46+
"""
47+
48+
project_id: Optional[str] = None
49+
"""The Kernel project ID, or null when the scope is organization-wide."""
50+
51+
52+
class Authorization(BaseModel):
53+
"""The credential's maximum scope and the effective scope selected for this request.
54+
55+
Future permission data can be added without changing scope semantics.
56+
"""
57+
58+
credential_scope: AuthorizationCredentialScope
59+
"""A scope within the authenticated organization.
60+
61+
A null project_id represents organization-wide scope.
62+
"""
63+
64+
effective_scope: AuthorizationEffectiveScope
65+
"""A scope within the authenticated organization.
66+
67+
A null project_id represents organization-wide scope.
68+
"""
69+
70+
71+
class Organization(BaseModel):
72+
id: str
73+
"""The authenticated Kernel organization ID."""
74+
75+
76+
class Principal(BaseModel):
77+
id: str
78+
"""The API key ID for API-key principals or user ID for user principals."""
79+
80+
type: Literal["api_key", "user"]
81+
"""The kind of principal authenticated for the request."""
82+
83+
84+
class AuthContext(BaseModel):
85+
"""The identity and authorization context resolved for the current request."""
86+
87+
authentication: Authentication
88+
89+
authorization: Authorization
90+
"""The credential's maximum scope and the effective scope selected for this
91+
request.
92+
93+
Future permission data can be added without changing scope semantics.
94+
"""
95+
96+
organization: Organization
97+
98+
principal: Principal

0 commit comments

Comments
 (0)