Google Services Integration Module via Oauth

Hello Everyone.

For a specific project aimed at managing schedules via Google Calendar, I had to create a Tryton module that allows the creation of credentials to authenticate with the Google platform.

Proposal

Motivation

Many businesses using Tryton require integration with Google Workspace services. Currently, there’s no standardized way to authenticate and interact with Google APIs within Tryton. This module aims to fill that gap by providing:

  • A reusable OAuth2 authentication framework
  • Secure credential management
  • An extensible architecture for multiple Google services
  • A reference implementation with Google Calendar

Features

Core Capabilities

1. OAuth2 Authentication Flow

  • Full OAuth2 implementation with PKCE (Proof Key for Code Exchange) for enhanced security
  • Automatic token refresh handling
  • Custom WSGI route for OAuth callback handling

2. Credential Management

The module implements a comprehensive state machine for managing Google credentials:

  • without_authorization - Initial state, no authorization requested
  • authorization_pending - Authorization URL generated, awaiting user consent
  • authorized - Successfully authenticated with valid tokens
  • authorization_expired - Tokens expired or revoked, re-authorization needed

3. User Interface

  • Intuitive form interface with three main tabs:
    • Configuration: OAuth client credentials setup (Client ID, Secret, Redirect URI)
    • Authentication: Authorization flow management with clickable authorization URL
    • Synchronization: Sync status and control panel
  • Tree view for managing multiple credential sets
  • One-click authorization button that generates the OAuth URL

4. Extensible Architecture

  • Abstract base class (GoogleService) for implementing different Google services
  • Template Method pattern for consistent service integration
  • Easy to extend with additional Google APIs (Drive, Gmail, Contacts, etc.)

Architecture & Design

Module Structure

google_integration/
β”œβ”€β”€ __init__.py              # Module registration
β”œβ”€β”€ credentials.py           # GoogleCredentials model
β”œβ”€β”€ google_service.py        # Abstract base class
β”œβ”€β”€ google_calendar.py       # Calendar service implementation
β”œβ”€β”€ routes.py               # OAuth callback handler
β”œβ”€β”€ credentials.xml         # Views and menu definitions
β”œβ”€β”€ view/
β”‚   β”œβ”€β”€ google_credential_form.xml
β”‚   └── google_credential_list.xml
β”œβ”€β”€ tests/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ test_module.py
β”‚   β”œβ”€β”€ test_module_api.py
β”‚   └── test_scenario.py
β”œβ”€β”€ doc/                    # Documentation structure
β”œβ”€β”€ tryton.cfg             # Module configuration
└── setup.py               # Package setup

Database Schema

The module introduces one main model: google_integration.google_credentials

Fields:

  • Configuration: name, client_id, client_secret, redirect_uri
  • OAuth Flow: oauth_state, code_verifier, authorization_url
  • Tokens: access_token, refresh_token, token_expiry
  • Synchronization: sync_enabled, last_sync
  • State: Current authorization status (selection field)

Methods:

  • get_credentials() - Returns Google Credentials object for API authentication
  • request_authorization() - Initiates OAuth2 flow (button method)
  • refresh_credentials() - Refreshes expired access tokens

Custom Routes

The module registers a custom WSGI route:

  • Route: /<database_name>/google/callback
  • Method: GET
  • Purpose: Handles OAuth2 callback from Google
  • Security: State validation, transaction context with system user
  • Response: User-friendly HTML confirmation page

Use Cases

  1. Calendar Synchronization

    • Sync Tryton events with Google Calendar
    • Two-way synchronization of appointments
    • Team calendar management
  2. Document Management (future)

    • Store Tryton attachments in Google Drive
    • Share documents with customers/suppliers
    • Collaborative document editing
  3. Email Integration (future)

    • Send emails through Gmail API
    • Track email communications
    • Automated email responses
  4. Contact Synchronization (future)

    • Sync Tryton parties with Google Contacts
    • Mobile access to business contacts
    • Unified contact management
  5. Multi-Service Integration

    • Single credential set for multiple Google services
    • Centralized permission management
    • Consistent authentication across services
1 Like

I do not really see why this should be a Tryton module.
For me it sounds more like a library to interact with Google services (which may already exists).