|
4 | 4 | import requests |
5 | 5 |
|
6 | 6 |
|
| 7 | +class AuthProviderBase: |
| 8 | + """ |
| 9 | + Base class for auth providers. |
| 10 | +
|
| 11 | + This abstract base class will analyze the authentication proposals of the openapi specs. |
| 12 | + Different authentication schemes should be implemented by subclasses. |
| 13 | + Returned auth objects need to be compatible with `requests.auth.AuthBase`. |
| 14 | + """ |
| 15 | + |
| 16 | + def __init__(self) -> None: |
| 17 | + self._oauth2_token: str | None = None |
| 18 | + self._oauth2_expires: datetime = datetime.now() |
| 19 | + |
| 20 | + def can_complete_http_basic(self) -> bool: |
| 21 | + return False |
| 22 | + |
| 23 | + def can_complete_mutualTLS(self) -> bool: |
| 24 | + return False |
| 25 | + |
| 26 | + def can_complete_oauth2_client_credentials(self, scopes: list[str]) -> bool: |
| 27 | + return False |
| 28 | + |
| 29 | + def can_complete_scheme(self, scheme: dict[str, t.Any], scopes: list[str]) -> bool: |
| 30 | + if scheme["type"] == "http": |
| 31 | + if scheme["scheme"] == "basic": |
| 32 | + return self.can_complete_http_basic() |
| 33 | + elif scheme["type"] == "mutualTLS": |
| 34 | + return self.can_complete_mutualTLS() |
| 35 | + elif scheme["type"] == "oauth2": |
| 36 | + for flow_name, flow in scheme["flows"].items(): |
| 37 | + if ( |
| 38 | + flow_name == "clientCredentials" |
| 39 | + and self.can_complete_oauth2_client_credentials(flow["scopes"]) |
| 40 | + ): |
| 41 | + return True |
| 42 | + return False |
| 43 | + |
| 44 | + def can_complete( |
| 45 | + self, proposal: dict[str, list[str]], security_schemes: dict[str, dict[str, t.Any]] |
| 46 | + ) -> bool: |
| 47 | + for name, scopes in proposal.items(): |
| 48 | + scheme = security_schemes.get(name) |
| 49 | + if scheme is None or not self.can_complete_scheme(scheme, scopes): |
| 50 | + return False |
| 51 | + # This covers the case where `[]` allows for no auth at all. |
| 52 | + return True |
| 53 | + |
| 54 | + async def auth_success_hook( |
| 55 | + self, proposal: dict[str, list[str]], security_schemes: dict[str, dict[str, t.Any]] |
| 56 | + ) -> None: |
| 57 | + pass |
| 58 | + |
| 59 | + async def auth_failure_hook( |
| 60 | + self, proposal: dict[str, list[str]], security_schemes: dict[str, dict[str, t.Any]] |
| 61 | + ) -> None: |
| 62 | + pass |
| 63 | + |
| 64 | + async def http_basic_credentials(self) -> tuple[bytes, bytes]: |
| 65 | + raise NotImplementedError() |
| 66 | + |
| 67 | + async def oauth2_client_credentials(self) -> tuple[bytes, bytes]: |
| 68 | + raise NotImplementedError() |
| 69 | + |
| 70 | + def tls_credentials(self) -> tuple[str, str | None]: |
| 71 | + raise NotImplementedError() |
| 72 | + |
| 73 | + |
| 74 | +class BasicAuthProvider(AuthProviderBase): |
| 75 | + """ |
| 76 | + AuthProvider providing basic auth with fixed `username`, `password`. |
| 77 | + """ |
| 78 | + |
| 79 | + def __init__(self, username: t.AnyStr, password: t.AnyStr): |
| 80 | + super().__init__() |
| 81 | + self.username: bytes = username.encode("latin1") if isinstance(username, str) else username |
| 82 | + self.password: bytes = password.encode("latin1") if isinstance(password, str) else password |
| 83 | + |
| 84 | + def can_complete_http_basic(self) -> bool: |
| 85 | + return True |
| 86 | + |
| 87 | + async def http_basic_credentials(self) -> tuple[bytes, bytes]: |
| 88 | + return self.username, self.password |
| 89 | + |
| 90 | + |
| 91 | +class GlueAuthProvider(AuthProviderBase): |
| 92 | + """ |
| 93 | + AuthProvider allowing to be used with prepared credentials. |
| 94 | + """ |
| 95 | + |
| 96 | + def __init__( |
| 97 | + self, |
| 98 | + *, |
| 99 | + username: t.AnyStr | None = None, |
| 100 | + password: t.AnyStr | None = None, |
| 101 | + client_id: t.AnyStr | None = None, |
| 102 | + client_secret: t.AnyStr | None = None, |
| 103 | + cert: str | None = None, |
| 104 | + key: str | None = None, |
| 105 | + ): |
| 106 | + super().__init__() |
| 107 | + self.username: bytes | None = None |
| 108 | + self.password: bytes | None = None |
| 109 | + self.client_id: bytes | None = None |
| 110 | + self.client_secret: bytes | None = None |
| 111 | + self.cert: str | None = cert |
| 112 | + self.key: str | None = key |
| 113 | + |
| 114 | + if username is not None: |
| 115 | + assert password is not None |
| 116 | + self.username = username.encode("latin1") if isinstance(username, str) else username |
| 117 | + self.password = password.encode("latin1") if isinstance(password, str) else password |
| 118 | + if client_id is not None: |
| 119 | + assert client_secret is not None |
| 120 | + self.client_id = client_id.encode("latin1") if isinstance(client_id, str) else client_id |
| 121 | + self.client_secret = ( |
| 122 | + client_secret.encode("latin1") if isinstance(client_secret, str) else client_secret |
| 123 | + ) |
| 124 | + |
| 125 | + if cert is None and key is not None: |
| 126 | + raise RuntimeError("Key can only be used together with a cert.") |
| 127 | + |
| 128 | + def can_complete_http_basic(self) -> bool: |
| 129 | + return self.username is not None |
| 130 | + |
| 131 | + def can_complete_oauth2_client_credentials(self, scopes: list[str]) -> bool: |
| 132 | + return self.client_id is not None |
| 133 | + |
| 134 | + def can_complete_mutualTLS(self) -> bool: |
| 135 | + return self.cert is not None |
| 136 | + |
| 137 | + async def http_basic_credentials(self) -> tuple[bytes, bytes]: |
| 138 | + assert self.username is not None |
| 139 | + assert self.password is not None |
| 140 | + return self.username, self.password |
| 141 | + |
| 142 | + async def oauth2_client_credentials(self) -> tuple[bytes, bytes]: |
| 143 | + assert self.client_id is not None |
| 144 | + assert self.client_secret is not None |
| 145 | + return self.client_id, self.client_secret |
| 146 | + |
| 147 | + def tls_credentials(self) -> tuple[str, str | None]: |
| 148 | + assert self.cert is not None |
| 149 | + return (self.cert, self.key) |
| 150 | + |
| 151 | + |
| 152 | +# ----------------------8<----8<------------------------ |
| 153 | + |
| 154 | + |
7 | 155 | class OAuth2ClientCredentialsAuth(requests.auth.AuthBase): |
8 | 156 | """ |
9 | 157 | This implements the OAuth2 ClientCredentials Grant authentication flow. |
|
0 commit comments