Skip to content

Commit

Permalink
Lowcode CDK: implement single use refresh token oauth authenticator (#…
Browse files Browse the repository at this point in the history
…24891)

* #24658 Lowcode CDK: implement single use refresh token oauth authenticator

* #24658 lowcode cdk: review fixes
  • Loading branch information
davydov-d committed Apr 6, 2023
1 parent cc18aba commit 30e2447
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,65 @@ definitions:
$parameters:
type: object
additionalProperties: true
SingleUseRefreshTokenOAuthAuthenticator:
description: Authenticator for requests using OAuth 2.0 authentication with a single use refresh token
type: object
required:
- type
- token_refresh_endpoint
properties:
type:
type: string
enum: [SingleUseRefreshTokenOAuthAuthenticator]
token_refresh_endpoint:
type: string
client_id_config_path:
type: array
items:
type: string
default: ["credentials", "client_id"]
client_secret_config_path:
type: array
items:
type: string
default: ["credentials", "client_secret"]
access_token_config_path:
type: array
items:
type: string
default: ["credentials", "access_token"]
refresh_token_config_path:
type: array
items:
type: string
default: ["credentials", "refresh_token"]
token_expiry_date_config_path:
type: array
items:
type: string
default: ["credentials", "token_expiry_date"]
access_token_name:
type: string
default: "access_token"
refresh_token_name:
type: string
default: "refresh_token"
expires_in_name:
type: string
default: "expires_in"
grant_type:
type: string
default: "refresh_token"
refresh_request_body:
type: object
additionalProperties: true
scopes:
type: array
items:
type: string
$parameters:
type: object
additionalProperties: true
DeclarativeStream:
description: A stream whose behavior is described by a set of declarative low code components
type: object
Expand Down Expand Up @@ -618,6 +677,7 @@ definitions:
- "$ref": "#/definitions/BearerAuthenticator"
- "$ref": "#/definitions/CustomAuthenticator"
- "$ref": "#/definitions/OAuthAuthenticator"
- "$ref": "#/definitions/SingleUseRefreshTokenOAuthAuthenticator"
- "$ref": "#/definitions/NoAuth"
- "$ref": "#/definitions/SessionTokenAuthenticator"
error_handler:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,26 @@ class OAuthAuthenticator(BaseModel):
parameters: Optional[Dict[str, Any]] = Field(None, alias="$parameters")


class SingleUseRefreshTokenOAuthAuthenticator(BaseModel):
type: Literal["SingleUseRefreshTokenOAuthAuthenticator"]
token_refresh_endpoint: str
client_id_config_path: Optional[List[str]] = ["credentials", "client_id"]
client_secret_config_path: Optional[List[str]] = ["credentials", "client_secret"]
access_token_config_path: Optional[List[str]] = ["credentials", "access_token"]
refresh_token_config_path: Optional[List[str]] = ["credentials", "refresh_token"]
token_expiry_date_config_path: Optional[List[str]] = [
"credentials",
"token_expiry_date",
]
access_token_name: Optional[str] = "access_token"
refresh_token_name: Optional[str] = "refresh_token"
expires_in_name: Optional[str] = "expires_in"
grant_type: Optional[str] = "refresh_token"
refresh_request_body: Optional[Dict[str, Any]] = None
scopes: Optional[List[str]] = None
parameters: Optional[Dict[str, Any]] = Field(None, alias="$parameters")


class ExponentialBackoffStrategy(BaseModel):
type: Literal["ExponentialBackoffStrategy"]
factor: Optional[Union[float, str]] = 5
Expand Down Expand Up @@ -508,6 +528,7 @@ class HttpRequester(BaseModel):
BearerAuthenticator,
CustomAuthenticator,
OAuthAuthenticator,
SingleUseRefreshTokenOAuthAuthenticator,
NoAuth,
SessionTokenAuthenticator,
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
from airbyte_cdk.sources.declarative.stream_slicers.cartesian_product_stream_slicer import CartesianProductStreamSlicer
from airbyte_cdk.sources.declarative.transformations import RemoveFields
from airbyte_cdk.sources.declarative.transformations.add_fields import AddedFieldDefinition, AddFields
from airbyte_cdk.sources.streams.http.requests_native_auth.oauth import SingleUseRefreshTokenOauth2Authenticator

"""
CLASS_TYPES_REGISTRY contains a mapping of developer-friendly string -> class to abstract the specific class referred to
Expand Down Expand Up @@ -84,6 +85,7 @@
"NoAuth": NoAuth,
"NoPagination": NoPagination,
"OAuthAuthenticator": DeclarativeOauth2Authenticator,
"SingleUseRefreshTokenOAuthAuthenticator": SingleUseRefreshTokenOauth2Authenticator,
"OffsetIncrement": OffsetIncrement,
"PageIncrement": PageIncrement,
"ParentStreamConfig": ParentStreamConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@
from airbyte_cdk.sources.declarative.models.declarative_component_schema import RequestPath as RequestPathModel
from airbyte_cdk.sources.declarative.models.declarative_component_schema import SessionTokenAuthenticator as SessionTokenAuthenticatorModel
from airbyte_cdk.sources.declarative.models.declarative_component_schema import SimpleRetriever as SimpleRetrieverModel
from airbyte_cdk.sources.declarative.models.declarative_component_schema import (
SingleUseRefreshTokenOAuthAuthenticator as SingleUseRefreshTokenOAuthAuthenticatorModel,
)
from airbyte_cdk.sources.declarative.models.declarative_component_schema import Spec as SpecModel
from airbyte_cdk.sources.declarative.models.declarative_component_schema import SubstreamPartitionRouter as SubstreamPartitionRouterModel
from airbyte_cdk.sources.declarative.models.declarative_component_schema import WaitTimeFromHeader as WaitTimeFromHeaderModel
Expand Down Expand Up @@ -98,6 +101,7 @@
from airbyte_cdk.sources.declarative.transformations import AddFields, RemoveFields
from airbyte_cdk.sources.declarative.transformations.add_fields import AddedFieldDefinition
from airbyte_cdk.sources.declarative.types import Config
from airbyte_cdk.sources.streams.http.requests_native_auth.oauth import SingleUseRefreshTokenOauth2Authenticator
from pydantic import BaseModel

ComponentDefinition: Union[Literal, Mapping, List]
Expand Down Expand Up @@ -149,6 +153,7 @@ def _init_mappings(self):
NoAuthModel: self.create_no_auth,
NoPaginationModel: self.create_no_pagination,
OAuthAuthenticatorModel: self.create_oauth_authenticator,
SingleUseRefreshTokenOAuthAuthenticatorModel: self.create_single_use_refresh_token_oauth_authenticator,
OffsetIncrementModel: self.create_offset_increment,
PageIncrementModel: self.create_page_increment,
ParentStreamConfigModel: self.create_parent_stream_config,
Expand Down Expand Up @@ -658,6 +663,26 @@ def create_oauth_authenticator(model: OAuthAuthenticatorModel, config: Config, *
parameters=model.parameters,
)

@staticmethod
def create_single_use_refresh_token_oauth_authenticator(
model: SingleUseRefreshTokenOAuthAuthenticatorModel, config: Config, **kwargs
) -> SingleUseRefreshTokenOauth2Authenticator:
return SingleUseRefreshTokenOauth2Authenticator(
config,
model.token_refresh_endpoint,
access_token_name=model.access_token_name,
refresh_token_name=model.refresh_token_name,
expires_in_name=model.expires_in_name,
client_id_config_path=model.client_id_config_path,
client_secret_config_path=model.client_secret_config_path,
access_token_config_path=model.access_token_config_path,
refresh_token_config_path=model.refresh_token_config_path,
token_expiry_date_config_path=model.token_expiry_date_config_path,
grant_type=model.grant_type,
refresh_request_body=model.refresh_request_body,
scopes=model.scopes,
)

@staticmethod
def create_offset_increment(model: OffsetIncrementModel, config: Config, **kwargs) -> OffsetIncrement:
return OffsetIncrement(page_size=model.page_size, config=config, parameters=model.parameters)
Expand Down

0 comments on commit 30e2447

Please sign in to comment.