Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:ALwrity ALwrity OAuthProviderBase

From Leeroopedia
Knowledge Sources
Domains OAuth, Security, Integration
Last Updated 2026-10-24 00:00 GMT

Overview

Concrete tool for Fernet-encrypted OAuth token storage, decryption, and plaintext migration, shared across all ALwrity OAuth provider integrations.

Description

The OAuthProviderBase class is the abstract base class for all OAuth integrations in ALwrity (Bing, LinkedIn/Unipile, Wix, WordPress). It provides three core capabilities: (1) Fernet initialisation — resolves the encryption key from provider-specific env vars (BING_TOKEN_ENCRYPTION_KEY, LINKEDIN_TOKEN_ENCRYPTION_KEY) with a shared OAUTH_TOKEN_ENCRYPTION_KEY fallback, failing fast at construction if the key is missing or invalid; (2) token encrypt/decrypt — encrypts plaintext tokens and decrypts Fernet blobs for use in API calls; (3) plaintext migration — a one-time, per-user-per-process migration that re-encrypts legacy plaintext token rows in the per-user SQLite database, gated by a _migration_done set.

Subclasses (e.g. BingOAuthService, WixOAuthService) inherit these methods and define provider-specific SQL (_select_plaintext_tokens_sql, _update_token_sql) and the token_encryption_key.

Usage

Import this base class when:

  • Building a new OAuth provider integration that needs encrypted token storage.
  • Migrating legacy plaintext tokens to encrypted blobs.
  • Decrypting tokens for use in provider API calls.

Subclasses must define self.token_encryption_key, self._migration_done (set), self._select_plaintext_tokens_sql, and self._update_token_sql.

Code Reference

Source Location

  • Repository: ALwrity
  • File: backend/services/integrations/oauth_provider_base.py (L75-195)

Signature

class OAuthProviderBase:
    """Shared Fernet encryption + plaintext migration for OAuth tokens."""

    def _initialize_fernet(self) -> Fernet:
        """Initialize Fernet, raising on missing or invalid key."""

    def _encrypt_token(self, token: Optional[str]) -> Optional[str]:
        """Encrypt a plaintext token to a Fernet blob."""

    def _decrypt_token(self, token_blob: Optional[str]) -> Optional[str]:
        """Decrypt a Fernet blob to a plaintext token."""

    def _is_likely_encrypted_blob(self, value: Optional[str]) -> bool:
        """Check if a value looks like a Fernet blob (starts with 'gAAAAA')."""

    def _migrate_plaintext_tokens_if_needed(
        self, conn: sqlite3.Connection, user_id: str,
    ) -> None:
        """One-time migration: re-encrypt plaintext token rows."""

    def _get_db_path(self, user_id: str) -> str:
        """Resolve per-user SQLite DB path."""


def resolve_encryption_key(provider_name: str) -> Optional[str]:
    """Resolve the Fernet key for a provider (provider-specific env, then shared fallback)."""

Import

from services.integrations.oauth_provider_base import OAuthProviderBase, resolve_encryption_key

I/O Contract

Inputs

Name Type Required Description
token Optional[str] Yes (encrypt) Plaintext token to encrypt
token_blob Optional[str] Yes (decrypt) Fernet blob to decrypt
conn sqlite3.Connection Yes (migrate) Per-user DB connection
user_id str Yes (migrate) Clerk user ID (migration gate)

Outputs

Name Type Description
_encrypt_token Optional[str] Fernet-encrypted blob (or None if input is None)
_decrypt_token Optional[str] Plaintext token (or None if input is None)
_migrate_plaintext_tokens_if_needed None Re-encrypts plaintext rows; logs count migrated

Usage Examples

Encrypting and decrypting a token

from services.integrations.oauth_provider_base import OAuthProviderBase

class MyProvider(OAuthProviderBase):
    def __init__(self):
        from services.integrations.oauth_provider_base import resolve_encryption_key
        self.token_encryption_key = resolve_encryption_key("bing")
        self._fernet = self._initialize_fernet()

provider = MyProvider()
blob = provider._encrypt_token("ya29.access_token_value")
plaintext = provider._decrypt_token(blob)
assert plaintext == "ya29.access_token_value"

Running plaintext migration

import sqlite3
conn = sqlite3.connect(provider._get_db_path("clerk_user_123"))
provider._migrate_plaintext_tokens_if_needed(conn, user_id="clerk_user_123")
# Plaintext token rows are now Fernet-encrypted; subsequent calls are no-ops.

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment