From b891a48dadb0fa5cfd270eeda8389a22c215f7b2 Mon Sep 17 00:00:00 2001 From: David Barnett Date: Sun, 15 Sep 2024 14:52:20 -0600 Subject: [PATCH] config: Add support for auth.client-id --- data/config-schema.json | 22 ++++++++++++++++++++++ gcalcli/config.py | 26 ++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/data/config-schema.json b/data/config-schema.json index 543a1cc..99caa9c 100644 --- a/data/config-schema.json +++ b/data/config-schema.json @@ -4,6 +4,25 @@ "description": "User configuration for gcalcli command-line tool.\n\nSee https://pypi.org/project/gcalcli/.", "type": "object", "$defs": { + "AuthSection": { + "title": "Settings for authentication", + "type": "object", + "properties": { + "client-id": { + "title": "Client ID for Google auth token", + "description": "Google client ID for your auth client project.\n\nDetails: https://github.com/insanum/gcalcli/blob/HEAD/docs/api-auth.md", + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null + } + } + }, "CalendarsSection": { "title": "Settings about the set of calendars gcalcli operates on", "type": "object", @@ -45,6 +64,9 @@ } }, "properties": { + "auth": { + "$ref": "#/$defs/AuthSection" + }, "calendars": { "$ref": "#/$defs/CalendarsSection" }, diff --git a/gcalcli/config.py b/gcalcli/config.py index 86aa96d..3f484b3 100644 --- a/gcalcli/config.py +++ b/gcalcli/config.py @@ -4,7 +4,7 @@ from collections import OrderedDict from enum import Enum import sys -from typing import Any, List, Mapping +from typing import Any, List, Mapping, Optional if sys.version_info[:2] < (3, 11): import toml as tomllib @@ -15,6 +15,24 @@ from pydantic.json_schema import GenerateJsonSchema +class AuthSection(BaseModel): + """Configuration for settings like client-id used in auth flow. + + Note that client-secret can't be configured here and should be passed on + command-line instead for security reasons. + """ + + model_config = ConfigDict(title='Settings for authentication') + + client_id: Optional[str] = Field( + alias='client-id', + title='Client ID for Google auth token', + description="""Google client ID for your auth client project.\n +Details: https://github.com/insanum/gcalcli/blob/HEAD/docs/api-auth.md""", + default=None, + ) + + class CalendarsSection(BaseModel): model_config = ConfigDict( title='Settings about the set of calendars gcalcli operates on' @@ -48,7 +66,8 @@ class OutputSection(BaseModel): week_start: WeekStart = Field( alias='week-start', title='Weekday to treat as start of week', - default=WeekStart.SUNDAY) + default=WeekStart.SUNDAY, + ) class Config(BaseModel): @@ -62,6 +81,7 @@ class Config(BaseModel): json_schema_extra={'$schema': GenerateJsonSchema.schema_dialect}, ) + auth: AuthSection = Field(default_factory=AuthSection) calendars: CalendarsSection = Field(default_factory=CalendarsSection) output: OutputSection = Field(default_factory=OutputSection) @@ -72,6 +92,8 @@ def from_toml(cls, config_file): def to_argparse_namespace(self) -> argparse.Namespace: kwargs = {} + if self.auth: + kwargs.update(vars(self.auth)) if self.calendars: kwargs.update(vars(self.calendars)) if self.output: