|
| 1 | +# ------------------------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# Licensed under the MIT License. See LICENSE.txt in the project root for |
| 4 | +# license information. |
| 5 | +# ------------------------------------------------------------------------- |
| 6 | +from __future__ import annotations |
| 7 | +from collections import namedtuple |
| 8 | +from types import TracebackType |
| 9 | +from typing import Any, NamedTuple, Optional, AsyncContextManager, Type |
| 10 | +from typing_extensions import Protocol, runtime_checkable |
| 11 | + |
| 12 | + |
| 13 | +class AccessToken(NamedTuple): |
| 14 | + """Represents an OAuth access token.""" |
| 15 | + |
| 16 | + token: str |
| 17 | + expires_on: int |
| 18 | + |
| 19 | + |
| 20 | +AccessToken.token.__doc__ = """The token string.""" |
| 21 | +AccessToken.expires_on.__doc__ = """The token's expiration time in Unix time.""" |
| 22 | + |
| 23 | + |
| 24 | +@runtime_checkable |
| 25 | +class TokenCredential(Protocol): |
| 26 | + """Protocol for classes able to provide OAuth tokens.""" |
| 27 | + |
| 28 | + def get_token(self, *scopes: str, claims: Optional[str] = None, **kwargs: Any) -> AccessToken: |
| 29 | + """Request an access token for `scopes`. |
| 30 | +
|
| 31 | + :param str scopes: The type of access needed. |
| 32 | +
|
| 33 | + :keyword str claims: Additional claims required in the token, such as those returned in a resource |
| 34 | + provider's claims challenge following an authorization failure. |
| 35 | +
|
| 36 | +
|
| 37 | + :rtype: AccessToken |
| 38 | + :return: An AccessToken instance containing the token string and its expiration time in Unix time. |
| 39 | + """ |
| 40 | + ... |
| 41 | + |
| 42 | + |
| 43 | +ServiceNamedKey = namedtuple("ServiceNamedKey", ["name", "key"]) |
| 44 | + |
| 45 | +__all__ = [ |
| 46 | + "AccessToken", |
| 47 | + "ServiceKeyCredential", |
| 48 | + "ServiceNamedKeyCredential", |
| 49 | + "TokenCredential", |
| 50 | + "AsyncTokenCredential", |
| 51 | +] |
| 52 | + |
| 53 | + |
| 54 | +class ServiceKeyCredential: |
| 55 | + """Credential type used for authenticating to a service. |
| 56 | + It provides the ability to update the key without creating a new client. |
| 57 | +
|
| 58 | + :param str key: The key used to authenticate to a service |
| 59 | + :raises: TypeError |
| 60 | + """ |
| 61 | + |
| 62 | + def __init__(self, key: str) -> None: |
| 63 | + if not isinstance(key, str): |
| 64 | + raise TypeError("key must be a string.") |
| 65 | + self._key = key |
| 66 | + |
| 67 | + @property |
| 68 | + def key(self) -> str: |
| 69 | + """The value of the configured key. |
| 70 | +
|
| 71 | + :rtype: str |
| 72 | + :return: The value of the configured key. |
| 73 | + """ |
| 74 | + return self._key |
| 75 | + |
| 76 | + def update(self, key: str) -> None: |
| 77 | + """Update the key. |
| 78 | +
|
| 79 | + This can be used when you've regenerated your service key and want |
| 80 | + to update long-lived clients. |
| 81 | +
|
| 82 | + :param str key: The key used to authenticate to a service |
| 83 | + :raises: ValueError or TypeError |
| 84 | + """ |
| 85 | + if not key: |
| 86 | + raise ValueError("The key used for updating can not be None or empty") |
| 87 | + if not isinstance(key, str): |
| 88 | + raise TypeError("The key used for updating must be a string.") |
| 89 | + self._key = key |
| 90 | + |
| 91 | + |
| 92 | +class ServiceNamedKeyCredential: |
| 93 | + """Credential type used for working with any service needing a named key that follows patterns |
| 94 | + established by the other credential types. |
| 95 | +
|
| 96 | + :param str name: The name of the credential used to authenticate to a service. |
| 97 | + :param str key: The key used to authenticate to a service. |
| 98 | + :raises: TypeError |
| 99 | + """ |
| 100 | + |
| 101 | + def __init__(self, name: str, key: str) -> None: |
| 102 | + if not isinstance(name, str) or not isinstance(key, str): |
| 103 | + raise TypeError("Both name and key must be strings.") |
| 104 | + self._credential = ServiceNamedKey(name, key) |
| 105 | + |
| 106 | + @property |
| 107 | + def named_key(self) -> ServiceNamedKey: |
| 108 | + """The value of the configured name. |
| 109 | +
|
| 110 | + :rtype: ServiceNamedKey |
| 111 | + :return: The value of the configured name. |
| 112 | + """ |
| 113 | + return self._credential |
| 114 | + |
| 115 | + def update(self, name: str, key: str) -> None: |
| 116 | + """Update the named key credential. |
| 117 | +
|
| 118 | + Both name and key must be provided in order to update the named key credential. |
| 119 | + Individual attributes cannot be updated. |
| 120 | +
|
| 121 | + :param str name: The name of the credential used to authenticate to a service. |
| 122 | + :param str key: The key used to authenticate to a service. |
| 123 | + """ |
| 124 | + if not isinstance(name, str) or not isinstance(key, str): |
| 125 | + raise TypeError("Both name and key must be strings.") |
| 126 | + self._credential = ServiceNamedKey(name, key) |
| 127 | + |
| 128 | + |
| 129 | +@runtime_checkable |
| 130 | +class AsyncTokenCredential(Protocol, AsyncContextManager["AsyncTokenCredential"]): |
| 131 | + """Protocol for classes able to provide OAuth tokens.""" |
| 132 | + |
| 133 | + async def get_token(self, *scopes: str, claims: Optional[str] = None, **kwargs: Any) -> AccessToken: |
| 134 | + """Request an access token for `scopes`. |
| 135 | +
|
| 136 | + :param str scopes: The type of access needed. |
| 137 | +
|
| 138 | + :keyword str claims: Additional claims required in the token, such as those returned in a resource |
| 139 | + provider's claims challenge following an authorization failure. |
| 140 | +
|
| 141 | + :rtype: AccessToken |
| 142 | + :return: An AccessToken instance containing the token string and its expiration time in Unix time. |
| 143 | + """ |
| 144 | + ... |
| 145 | + |
| 146 | + async def close(self) -> None: |
| 147 | + pass |
| 148 | + |
| 149 | + async def __aexit__( |
| 150 | + self, |
| 151 | + exc_type: Optional[Type[BaseException]] = None, |
| 152 | + exc_value: Optional[BaseException] = None, |
| 153 | + traceback: Optional[TracebackType] = None, |
| 154 | + ) -> None: |
| 155 | + pass |
0 commit comments