| 1 |
# coding: utf-8 |
| 2 |
|
| 3 |
# Copyright 2019, 2025 IBM All Rights Reserved. |
| 4 |
# |
| 5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
# you may not use this file except in compliance with the License. |
| 7 |
# You may obtain a copy of the License at |
| 8 |
# |
| 9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
# |
| 11 |
# Unless required by applicable law or agreed to in writing, software |
| 12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
# See the License for the specific language governing permissions and |
| 15 |
# limitations under the License. |
| 16 |
|
| 17 |
from abc import ABC, abstractmethod |
| 18 |
|
| 19 |
|
| 20 |
class Authenticator(ABC): |
| 21 |
"""This interface defines the common methods and constants associated with an Authenticator implementation.""" |
| 22 |
|
| 23 |
# Constants representing the various authenticator types. |
| 24 |
AUTHTYPE_BASIC = 'basic' |
| 25 |
AUTHTYPE_BEARERTOKEN = 'bearerToken' |
| 26 |
AUTHTYPE_IAM = 'iam' |
| 27 |
AUTHTYPE_IAM_ASSUME = 'iamAssume' |
| 28 |
AUTHTYPE_CONTAINER = 'container' |
| 29 |
AUTHTYPE_CP4D = 'cp4d' |
| 30 |
AUTHTYPE_VPC = 'vpc' |
| 31 |
AUTHTYPE_NOAUTH = 'noAuth' |
| 32 |
AUTHTYPE_MCSP = 'mcsp' |
| 33 |
AUTHTYPE_MCSPV2 = 'mcspv2' |
| 34 |
AUTHTYPE_UNKNOWN = 'unknown' |
| 35 |
|
| 36 |
@abstractmethod |
| 37 |
def authenticate(self, req: dict) -> None: |
| 38 |
"""Perform the necessary authentication steps for the specified request. |
| 39 |
|
| 40 |
Attributes: |
| 41 |
req (dict): Will be modified to contain the appropriate authentication information. |
| 42 |
|
| 43 |
To be implemented by subclasses. |
| 44 |
""" |
| 45 |
pass |
| 46 |
|
| 47 |
@abstractmethod |
| 48 |
def validate(self) -> None: |
| 49 |
"""Validates the current set of configuration information in the Authenticator. |
| 50 |
|
| 51 |
Raises: |
| 52 |
ValueError: The configuration information is not valid for service operations. |
| 53 |
|
| 54 |
To be implemented by subclasses. |
| 55 |
""" |
| 56 |
pass |
| 57 |
|
| 58 |
def authentication_type(self) -> str: |
| 59 |
"""Returns the authenticator's type. This method should be overridden by each authenticator implementation.""" |
| 60 |
return self.AUTHTYPE_UNKNOWN |