Index of every page on this site: llms.txt
All skills
grasmash avatar

/drupal-simple-oauth

@19514f4
Run it yourself

Explains OAuth2 authentication in Drupal with the simple_oauth module - TokenAuthUser AND-permission logic, scope/role intersection, OAuth2 scope entities and the dynamic scope provider, mobile app password-grant token requests, field_permissions and JSON:API field access under a token, Bearer-token CSRF 403s, consumer TTLs, and debugging token permission denials. Use when an OAuth/Bearer token is denied a permission the user has, configuring consumers, clients or scopes, requesting tokens from a mobile or decoupled app, or getting 403s on JSON:API requests made with a Bearer token.

Use this Skill: https://skilld.dev/gh/grasmash/drupal-claude-skills/drupal-simple-oauth

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesbearer-csrf-bypass.md

≈733 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Bearer CSRF Bypass Module

Avoiding false CSRF 403s when a request carries both a Bearer token and a session cookie, moved from SKILL.md.

Bearer CSRF Bypass Module

The Problem: CSRF Validation with Bearer Tokens

When a client (especially React Native apps) sends a request with BOTH a valid Bearer token AND a session cookie, Drupal's CsrfRequestHeaderAccessCheck incorrectly triggers CSRF validation. This happens because:

  1. Drupal's session_configuration service detects a session based on cookies alone
  2. It ignores the Bearer token completely
  3. This causes 403 Forbidden errors even though the Bearer token is valid

Drupal core issue: #3055260

The Solution: Custom CSRF Bypass Module

Create a custom module (e.g., oauth_csrf_bypass) that decorates the session_configuration service to return FALSE for hasSession() when a valid Bearer token is present, preventing unnecessary CSRF checks.

Location: <webroot>/modules/custom/{module_name}/ (<webroot> is web/ or docroot/)

How It Works

// src/Session/BearerSessionConfiguration.php
public function hasSession(Request $request): bool {
  $auth_header = $request->headers->get('Authorization', '');

  if (str_starts_with($auth_header, 'Bearer ')) {
    // Validate the token is actually legitimate
    if ($this->isValidBearerToken($request)) {
      return FALSE; // No session = no CSRF check
    }
  }

  return $this->inner->hasSession($request);
}

Security: The module validates the Bearer token using Simple OAuth's ResourceServer before bypassing CSRF, ensuring invalid tokens don't bypass security.

Service Definition

# {module_name}.services.yml
services:
  {module_name}.session_configuration:
    class: Drupal\{module_name}\Session\BearerSessionConfiguration
    decorates: session_configuration
    decoration_priority: 10
    arguments:
      - '@{module_name}.session_configuration.inner'
      - '@simple_oauth.server.resource_server.factory'
      - '@psr7.http_message_factory'

When You Need This

Enable this module when:

  • Mobile apps send Bearer tokens but browsers/webviews also set session cookies
  • You get 403 CSRF errors despite having valid Bearer tokens
  • React Native or similar hybrid apps have authentication issues

Testing the Fix

# Test with Bearer token only - should succeed
curl -X GET "https://yoursite.ddev.site/jsonapi" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Accept: application/vnd.api+json"

# Test with Bearer + session cookie (the bug scenario) - should also succeed
curl -X GET "https://yoursite.ddev.site/jsonapi" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Cookie: SESSxxxxxxxxxx=fake_session_value" \
  -H "Accept: application/vnd.api+json"

Module Dependencies

  • simple_oauth:simple_oauth - Required for ResourceServer token validation

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts1mo4 checks · Risk SAFE
  • Gen Agent Trust Hub1mo

    This skill provides technical documentation and patterns for Drupal OAuth2 authentication. It includes a technical pattern for bypassing CSRF checks when using Bearer tokens, which is a standard requirement for decoupled applications but creates a security surface that relies on the provided token validation logic.

  • Socket1mo

    No alerts

  • Snyk1mo

    Risk: LOW · No issues

  • ZeroLeaks7mo

    1 finding · Score: 82/100

Signed by skilld at 19514f4. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Steadyupdated 2 months ago

README badge

README badge for grasmash/drupal-claude-skills/drupal-simple-oauth