Skip to content

Commit ec00094

Browse files
authored
add identity and auth interfaces (smithy-lang#105)
* add identity and auth interfaces * pr feedback * pr feedback. move all identity interfaces except AWS specific one to smithy-python * auth scheme resolver to protocol * pr feedback. moved auth stuff out of aws-smithy-python into smithy-python. Removed all usage of TypeVar and Generic and removed some files from gitignore. * remove aws identity and rebase exceptions.py * pr feedback. force named arguments in all methods and more doc string cleanup
1 parent 3dae44b commit ec00094

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
*.iml
99
*.iws
1010

11+
# VS Code
12+
.vscode/
13+
1114
# Mac
1215
.DS_Store
1316

python-packages/smithy-python/smithy_python/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ class SmithyRetryException(SmithyException):
2222

2323
class ExpectationNotMetException(SmithyException):
2424
"""Exception type for exceptions thrown by unmet assertions."""
25+
26+
27+
class SmithyIdentityException(SmithyException):
28+
"""Base exception type for all exceptions raised in identity resolution."""
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
14+
from dataclasses import dataclass
15+
from typing import Any, Protocol
16+
17+
from .http import Request
18+
from .identity import Identity, IdentityResolver
19+
20+
21+
class HttpSigner(Protocol):
22+
"""An interface for generating a signed HTTP request."""
23+
24+
def sign(
25+
self,
26+
*,
27+
http_request: Request,
28+
identity: Identity,
29+
signing_properties: dict[str, Any],
30+
) -> Request:
31+
"""Sign the provided HTTP request, and generate a new HTTP request with the
32+
signature added.
33+
34+
:param http_request: The HTTP request to sign.
35+
36+
:param identity: The signing identity.
37+
38+
:param signing_properties: Additional properties loaded to modify the
39+
signing process.
40+
"""
41+
...
42+
43+
44+
@dataclass(kw_only=True)
45+
class HttpAuthScheme(Protocol):
46+
"""Represents a way a service will authenticate the user's identity."""
47+
48+
# A unique identifier for the authentication scheme.
49+
scheme_id: str
50+
51+
# An API that can be queried to resolve an identity.
52+
identity_resolver: IdentityResolver
53+
54+
# An API that can be used to sign HTTP requests.
55+
signer: HttpSigner
56+
57+
58+
@dataclass(kw_only=True)
59+
class HttpAuthOption:
60+
"""Auth scheme used for signing and identity resolution."""
61+
62+
# The ID of the scheme to use. This string matches the one returned by
63+
# HttpAuthScheme.scheme_id
64+
scheme_id: str
65+
66+
# Parameters to pass to IdentityResolver.get_identity.
67+
identity_properties: dict[str, Any]
68+
69+
# Parameters to pass to HttpSigner.sign.
70+
signer_properties: dict[str, Any]
71+
72+
73+
@dataclass(kw_only=True)
74+
class AuthSchemeParameters:
75+
"""The input to the auth scheme resolver.
76+
77+
A code-generated interface for passing in the data required for determining the
78+
authentication scheme. By default, this only includes the operation name.
79+
"""
80+
81+
# The service operation being invoked by the client.
82+
operation: str
83+
84+
85+
class AuthSchemeResolver(Protocol):
86+
"""Determines which authentication scheme to use for a given service."""
87+
88+
def resolve_auth_scheme(
89+
self, *, auth_parameters: AuthSchemeParameters
90+
) -> list[HttpAuthOption]:
91+
"""Resolve an ordered list of applicable auth schemes.
92+
93+
:param auth_parameters: The parameters required for determining which
94+
authentication schemes to potentially use.
95+
"""
96+
...
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
14+
from datetime import datetime
15+
from typing import Any, Protocol
16+
17+
18+
class Identity(Protocol):
19+
"""An entity available to the client representing who the user is."""
20+
21+
expiration: datetime | None = None
22+
23+
24+
class IdentityResolver(Protocol):
25+
"""Used to load a user's `Identity` from a given source.
26+
27+
Each `Identity` may have one or more resolver implementations.
28+
"""
29+
30+
async def get_identity(self, *, identity_properties: dict[str, Any]) -> Identity:
31+
"""Load the user's identity from this resolver.
32+
33+
:param identity_properties: Properties used to help determine the
34+
identity to return.
35+
"""
36+
...
37+
38+
39+
class IdentityResolverConfiguration(Protocol):
40+
"""The identity resolvers configured in the client."""
41+
42+
def get_identity_resolver(
43+
self, *, identity_type: type[Identity]
44+
) -> IdentityResolver:
45+
"""Retrieve an identity resolver for the provided identity type.
46+
47+
:param identity_type: The type of identity to resolve.
48+
"""
49+
...

0 commit comments

Comments
 (0)