Skip to content

Commit cccdb6c

Browse files
committed
@W-18656128 OAuth Support
1 parent d248aa2 commit cccdb6c

8 files changed

Lines changed: 697 additions & 184 deletions

File tree

src/datacustomcode/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@
1414
# limitations under the License.
1515

1616
from datacustomcode.client import Client
17+
from datacustomcode.credentials import AuthType, Credentials
1718
from datacustomcode.io.reader.query_api import QueryAPIDataCloudReader
1819
from datacustomcode.io.writer.print import PrintDataCloudWriter
1920

20-
__all__ = ["Client", "QueryAPIDataCloudReader", "PrintDataCloudWriter"]
21+
__all__ = [
22+
"AuthType",
23+
"Client",
24+
"Credentials",
25+
"PrintDataCloudWriter",
26+
"QueryAPIDataCloudReader",
27+
]

src/datacustomcode/cli.py

Lines changed: 88 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,30 +43,102 @@ def version():
4343
click.echo("Version information not available")
4444

4545

46-
@cli.command()
47-
@click.option("--profile", default="default")
48-
@click.option("--username", prompt=True)
49-
@click.option("--password", prompt=True, hide_input=True)
50-
@click.option("--client-id", prompt=True)
51-
@click.option("--client-secret", prompt=True)
52-
@click.option("--login-url", prompt=True)
53-
def configure(
54-
username: str,
55-
password: str,
56-
client_id: str,
57-
client_secret: str,
46+
def _configure_username_password(
5847
login_url: str,
48+
client_id: str,
5949
profile: str,
6050
) -> None:
61-
from datacustomcode.credentials import Credentials
51+
"""Configure credentials for Username/Password authentication."""
52+
from datacustomcode.credentials import AuthType, Credentials
53+
54+
username = click.prompt("Username")
55+
password = click.prompt("Password", hide_input=True)
56+
client_secret = click.prompt("Client Secret")
6257

63-
Credentials(
58+
credentials = Credentials(
59+
login_url=login_url,
60+
client_id=client_id,
61+
auth_type=AuthType.USERNAME_PASSWORD,
6462
username=username,
6563
password=password,
66-
client_id=client_id,
6764
client_secret=client_secret,
65+
)
66+
credentials.update_ini(profile=profile)
67+
click.secho(
68+
f"Username/Password credentials saved to profile '{profile}' successfully",
69+
fg="green",
70+
)
71+
72+
73+
def _configure_oauth_tokens(
74+
login_url: str,
75+
client_id: str,
76+
profile: str,
77+
) -> None:
78+
"""Configure credentials for OAuth Tokens authentication."""
79+
from datacustomcode.credentials import AuthType, Credentials
80+
81+
client_secret = click.prompt("Client Secret")
82+
refresh_token = click.prompt("Refresh Token")
83+
core_token = click.prompt("Core Token (optional, press Enter to skip)", default="")
84+
85+
credentials = Credentials(
6886
login_url=login_url,
69-
).update_ini(profile=profile)
87+
client_id=client_id,
88+
auth_type=AuthType.OAUTH_TOKENS,
89+
client_secret=client_secret,
90+
refresh_token=refresh_token,
91+
core_token=core_token if core_token else None,
92+
)
93+
credentials.update_ini(profile=profile)
94+
click.secho(
95+
f"OAuth Tokens credentials saved to profile '{profile}' successfully",
96+
fg="green",
97+
)
98+
99+
100+
@cli.command()
101+
@click.option("--profile", default="default", help="Credential profile name")
102+
@click.option(
103+
"--auth-type",
104+
type=click.Choice(["oauth_tokens", "username_password"]),
105+
default=None,
106+
help="""Authentication method to use.
107+
108+
\b
109+
oauth_tokens - Refresh token-based authentication (default)
110+
username_password - Traditional username/password OAuth flow
111+
""",
112+
)
113+
def configure(profile: str, auth_type: str) -> None:
114+
from datacustomcode.credentials import AuthType
115+
116+
# If auth_type not specified, prompt for it
117+
if auth_type is None:
118+
click.echo("\nSelect authentication method:")
119+
click.echo(" 1. OAuth Tokens (refresh token) [default]")
120+
click.echo(" 2. Username/Password")
121+
choice = click.prompt(
122+
"Enter choice",
123+
type=click.Choice(["1", "2"]),
124+
default="1",
125+
)
126+
auth_type_map = {
127+
"1": "oauth_tokens",
128+
"2": "username_password",
129+
}
130+
auth_type = auth_type_map[choice]
131+
132+
# Common fields for all auth types
133+
click.echo(f"\nConfiguring {auth_type} authentication for profile '{profile}':\n")
134+
login_url = click.prompt("Login URL", default="https://login.salesforce.com")
135+
client_id = click.prompt("Client ID")
136+
137+
# Route to appropriate handler based on auth type
138+
if auth_type == AuthType.USERNAME_PASSWORD.value:
139+
_configure_username_password(login_url, client_id, profile)
140+
elif auth_type == AuthType.OAUTH_TOKENS.value:
141+
_configure_oauth_tokens(login_url, client_id, profile)
70142

71143

72144
@cli.command()

0 commit comments

Comments
 (0)