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.

referencesconsumer-ttls-and-social-grant.md

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

Consumer TTLs and Social Token Grants

Two deploy-time traps around Simple OAuth consumers and custom social login grants.

Contents

Consumer token TTLs are content-entity data, not config

consumer entities (from the Consumers module that Simple OAuth builds on) are content entities. Their fields, including access_token_expiration and refresh_token_expiration, live in the database. There is no consumer.consumer.*.yml in the config sync directory, so drush cim never deploys a TTL change. Editing the value in the UI on your local site changes only your local site.

Rule: change consumer settings on deployed environments with a hook_update_N() (it runs during drush updatedb).

Look the consumer up by client_id. The OAuth client id is unique and is what your apps send; the label and the numeric entity id are admin-editable or differ between environments.

/**
 * Raise the mobile app consumer's refresh token lifetime to 30 days.
 */
function my_module_update_10001() {
  $consumers = \Drupal::entityTypeManager()
    ->getStorage('consumer')
    ->loadByProperties(['client_id' => 'MOBILE_APP_CLIENT_ID']);

  if (!$consumers) {
    return 'No consumer with that client_id; nothing to do.';
  }

  /** @var \Drupal\consumers\Entity\Consumer $consumer */
  $consumer = reset($consumers);
  if ((int) $consumer->get('refresh_token_expiration')->value === 2592000) {
    return 'Consumer already up to date; nothing to do.';
  }

  $consumer->set('refresh_token_expiration', 2592000);
  $consumer->save();
  return 'Set refresh_token_expiration to 30 days.';
}

Make the hook idempotent (compare before writing) so it is safe on every environment, including ones that were already changed by hand.

Refresh token rotation races. Simple OAuth rotates the refresh token on every refresh. When an app fires two refreshes at once, or loses the response to the first, the second request presents an already-rotated token and the user is logged out. The contrib module Simple OAuth Refresh Token Buffer adds a per-consumer grace period during which a repeated refresh gets the same response as the first successful one. Its settings are also fields on the consumer (refresh_token_buffer_enabled, refresh_token_buffer_grace_period), so they deploy the same way: through an update hook, not config.

Social/Google token grant: validate aud against an allow-list

A common pattern for native "Sign in with Google" is a custom OAuth grant (for example grant_type=social) that accepts a Google token from the app, verifies it with Google (tokeninfo or JWKS), finds or creates the Drupal user, and issues a Simple OAuth token. The grant must check that the Google token was issued for your application by validating its audience (aud, and the authorized party azp where present).

The trap: a project normally has several Google OAuth client ids: one for the website's login button, and separate ones for each mobile and web client. Google reports aud/azp as whichever client minted the token. Tokens from the mobile apps therefore carry the app client ids, not the website client id stored in your social-auth module's settings. An audience check anchored on that single website client id rejects every app login, failing closed with no obvious error on the website.

Fix: validate against an allow-list of trusted audiences:

$trusted = $this->config('my_module.settings')->get('trusted_google_client_ids') ?: [];
if (!$trusted) {
  // Fail closed to the single web client id rather than accepting anything.
  $trusted = [$this->config('social_auth_google.settings')->get('client_id')];
}

// Collect whichever audience claims Google returned; a token with neither is rejected.
$claims = array_filter([
  $token_info['aud'] ?? NULL,
  $token_info['azp'] ?? NULL,
], 'is_string');

$accepted = FALSE;
foreach ($claims as $claim) {
  if (in_array($claim, $trusted, TRUE)) {
    $accepted = TRUE;
    break;
  }
}

if (!$accepted) {
  $this->logger->error('Google token audience not trusted (aud: @aud, azp: @azp).', [
    '@aud' => $token_info['aud'] ?? '(none)',
    '@azp' => $token_info['azp'] ?? '(none)',
  ]);
  throw OAuthServerException::accessDenied('Untrusted token audience.');
}

Deployment and observability notes:

  • The allow-list is config, so it needs a config import on every environment. Shipping the code without the import leaves each environment on the fallback, which still rejects app logins.
  • Log rejections at error level. If your error tracker ingests watchdog only at error and above, a ->warning() here never reaches it, and an app-login outage stays invisible.
  • Test with the token the app actually sends. Native apps often send a Google access token rather than an id_token. Exercise the grant with the same token type and client id your app uses; a test that mints through the website client passes while every app login fails.

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