Unable to Generate OAuth2 Private Keys - "Directory is not a valid directory" Error

Hi FarmOS team,

I’m having trouble configuring OAuth2 authentication for API access and could really use your help. I’m trying to integrate our FarmOS instance with a Laravel admin dashboard but getting stuck on the OAuth2 private key generation.

Our Setup:

  • FarmOS URL: https://farmos.middleworldfarms.org

  • Environment: Plesk hosting environment on Linux

  • Web Server: Nginx with www-data user

  • OAuth Consumer: Already created successfully with client_id Qw7!pZ2rT9@xL6vB1#eF4sG8uJ0mN5cD

  • Goal: Authenticate with farmOS API to retrieve land asset geometry data

The Problem:

When trying to generate OAuth2 private keys in Configuration > Web Services > Consumers > Settings, FarmOS rejects every directory path I try with the error:


Directory "/path/to/directory" is not a valid directory.

Directories I’ve Tested:

All of these directories exist, have proper www-data ownership (755/644 permissions), and are writable:

  1. /tmp/oauth-keys

  2. /tmp/farmos-oauth-keys

  3. /opt/sites/keys

  4. /var/oauth-keys

  5. /opt/sites/farmos.middleworldfarms.org/sites/default/files/private/oauth

I’ve verified each directory exists and is writable by the web server:


# All directories created with:

sudo mkdir -p /path/to/directory

sudo chown www-data:www-data /path/to/directory

sudo chmod 755 /path/to/directory

# Tested write access:

sudo -u www-data touch /path/to/directory/test.txt # Works fine

Current API Status:

Interestingly, our OAuth consumer IS being recognized by the API:


curl "https://farmos.middleworldfarms.org/api/asset/land"

Returns:

  • :white_check_mark: HTTP 200 OK

  • :white_check_mark: X-Consumer-ID: Qw7!pZ2rT9@xL6vB1#eF4sG8uJ0mN5cD header present

  • :white_check_mark: Land assets detected but “insufficient authorization” (expected without OAuth token)

However, OAuth token requests fail:


curl -X POST "https://farmos.middleworldfarms.org/oauth/token" \

-H "Content-Type: application/x-www-form-urlencoded" \

-d 'grant_type=password&username=martin@middleworldfarms.org&password=PASSWORD&client_id=Qw7!pZ2rT9@xL6vB1#eF4sG8uJ0mN5cD&client_secret=SECRET'

Returns:


{

"error": "server_error",

"error_description": "You need to set the OAuth2 private key."

}

Questions:

  1. What directory path format does FarmOS expect? Is there a specific structure or naming convention?

  2. Are there file system permissions beyond standard www-data ownership that FarmOS requires?

  3. Is there a way to manually configure the OAuth2 private key paths in configuration files rather than through the UI?

  4. Could this be related to Plesk hosting environment or any PHP security settings?

  5. Are there any logs I can check to see why the directory validation is failing?

Environment Details:

  • PHP version: 8.2.28

  • Drupal version: 10

  • Simple OAuth module (latest version)

  • FarmOS (latest version)

Any guidance would be greatly appreciated! The OAuth consumer setup is working, but we’re blocked on the private key generation step.

Thanks in advance for your help!


Additional Context:

This is for integrating farmOS with a Laravel admin dashboard to display farm geometry on interactive maps for delivery route planning. We’ve successfully connected to other APIs but farmOS’s OAuth setup is proving challenging.

1 Like

@MiddleWorld If you’re having trouble with the “Generate keys” button, it’s also possible to manually generate the keys.

Here are the commands that the official simple_oauth module docs recommend:

openssl genrsa -out private.key 2048
openssl rsa -in private.key -pubout > public.key

https://www.drupal.org/docs/extending-drupal/contributed-modules/contributed-modules/contributed-modules-for-authentication-and-login/simple-oauth-oauth2/simple-oauth-8x-2x

Awesome idea! If you get this working please consider making a forum topic to share what you built/learned along the way! Could be useful to others…

Sorry maybe I read too quickly and the “Generate” button isn’t the issue.

I’m not sure about Plesk hosting environments, but the way I generally set things up is:

  • farmOS in /opt/drupal (this is the default for the farmOS Docker image)
  • Webroot is /opt/drupal/web.
  • Keys in /opt/drupal/keys (so it is outside of the webroot).
  • Keys set to ../keys/public.key and ../keys/private.key in the UI.

Hope that helps!