Skip to content

EspoCRM – Open Source CRM Application

OAuth metadata

Yuriib770f3de6d84
diff --git a/application/Espo/Core/Utils/OAuth/OAuthRouteUtil.php b/application/Espo/Core/Utils/OAuth/OAuthRouteUtil.php
index 859f3c3b9a..e0f0a504dc 100644
--- a/application/Espo/Core/Utils/OAuth/OAuthRouteUtil.php
+++ b/application/Espo/Core/Utils/OAuth/OAuthRouteUtil.php
@@ -44,7 +44,10 @@ class OAuthRouteUtil
/** @var string $scriptName */
$scriptName = parse_url($serverScriptName , PHP_URL_PATH);
- $scriptNameModified = str_replace('public/oauth/', 'oauth/', $scriptName);
+ $scriptNameModified = strtr($scriptName, [
+ '/public/oauth/' => '/oauth/',
+ '/public/.well-known/' => '/.well-known/',
+ ]);
$scriptDir = dirname($scriptNameModified);
diff --git a/application/Espo/Resources/metadata/app/entryPoints.json b/application/Espo/Resources/metadata/app/entryPoints.json
index d88128bcec..7a70b1239f 100644
--- a/application/Espo/Resources/metadata/app/entryPoints.json
+++ b/application/Espo/Resources/metadata/app/entryPoints.json
@@ -25,5 +25,15 @@
"noAuth": true,
"notExposed": true,
"allowedMethods": ["post"]
+ },
+ "oAuthProtectedResourceMetadata": {
+ "className": "Espo\\Tools\\OAuthServer\\EntryPoints\\ProtectedResourceMetadata",
+ "noAuth": true,
+ "notExposed": true
+ },
+ "oAuthAuthorizationServerMetadata": {
+ "className": "Espo\\Tools\\OAuthServer\\EntryPoints\\AuthorizationServerMetadata",
+ "noAuth": true,
+ "notExposed": true
}
}
diff --git a/application/Espo/Tools/OAuthServer/EntryPoints/AuthorizationServerMetadata.php b/application/Espo/Tools/OAuthServer/EntryPoints/AuthorizationServerMetadata.php
new file mode 100644
index 0000000000..e6b4713ad7
--- /dev/null
+++ b/application/Espo/Tools/OAuthServer/EntryPoints/AuthorizationServerMetadata.php
@@ -0,0 +1,53 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Tools\OAuthServer\EntryPoints;
+
+use Espo\Core\Api\Request;
+use Espo\Core\Api\Response;
+use Espo\Core\EntryPoint\EntryPoint;
+use Espo\Core\Utils\Json;
+use Espo\Tools\OAuthServer\Metadata\AuthorizationServerMetadataProvider;
+
+/**
+ * @noinspection PhpUnused
+ */
+class AuthorizationServerMetadata implements EntryPoint
+{
+ public function __construct(
+ private AuthorizationServerMetadataProvider $provider,
+ ) {}
+
+ public function run(Request $request, Response $response): void
+ {
+ $data = $this->provider->get();
+
+ $response->writeBody(Json::encode($data, JSON_PRETTY_PRINT));
+ }
+}
diff --git a/application/Espo/Tools/OAuthServer/EntryPoints/ProtectedResourceMetadata.php b/application/Espo/Tools/OAuthServer/EntryPoints/ProtectedResourceMetadata.php
new file mode 100644
index 0000000000..a560d923d6
--- /dev/null
+++ b/application/Espo/Tools/OAuthServer/EntryPoints/ProtectedResourceMetadata.php
@@ -0,0 +1,53 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Tools\OAuthServer\EntryPoints;
+
+use Espo\Core\Api\Request;
+use Espo\Core\Api\Response;
+use Espo\Core\EntryPoint\EntryPoint;
+use Espo\Core\Utils\Json;
+use Espo\Tools\OAuthServer\Metadata\ProtectedResourceMetadataProvider;
+
+/**
+ * @noinspection PhpUnused
+ */
+class ProtectedResourceMetadata implements EntryPoint
+{
+ public function __construct(
+ private ProtectedResourceMetadataProvider $provider,
+ ) {}
+
+ public function run(Request $request, Response $response): void
+ {
+ $data = $this->provider->get();
+
+ $response->writeBody(Json::encode($data, JSON_PRETTY_PRINT));
+ }
+}
diff --git a/application/Espo/Tools/OAuthServer/Metadata/AuthorizationServerMetadataProvider.php b/application/Espo/Tools/OAuthServer/Metadata/AuthorizationServerMetadataProvider.php
new file mode 100644
index 0000000000..c961edff2f
--- /dev/null
+++ b/application/Espo/Tools/OAuthServer/Metadata/AuthorizationServerMetadataProvider.php
@@ -0,0 +1,78 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Tools\OAuthServer\Metadata;
+
+use Espo\Core\Utils\Cache\DataCacheAccess;
+use Espo\Core\Utils\Config\ApplicationConfig;
+use stdClass;
+
+class AuthorizationServerMetadataProvider
+{
+ private const string CACHE_KEY = 'oAuthServer/authorizationServerMetadata';
+
+ /**
+ * @param DataCacheAccess<stdClass> $dataCacheAccess
+ */
+ public function __construct(
+ private DataCacheAccess $dataCacheAccess,
+ private ApplicationConfig $applicationConfig,
+ ) {
+ $this->dataCacheAccess->init(
+ key: self::CACHE_KEY,
+ loader: fn () => $this->buildData(),
+ );
+ }
+
+ public function get(): stdClass
+ {
+ return $this->dataCacheAccess->get();
+ }
+
+ private function buildData(): stdClass
+ {
+ return (object) [
+ 'issuer' => self::serverIdentifierFromUrl($this->applicationConfig->getSiteUrl()),
+ 'authorization_endpoint' => $this->applicationConfig->getSiteUrl() . '/oauth/authorize',
+ 'token_endpoint' => $this->applicationConfig->getSiteUrl() . '/oauth/token',
+ 'revocation_endpoint' => $this->applicationConfig->getSiteUrl() . '/oauth/token/revoke',
+ ];
+ }
+
+ private static function serverIdentifierFromUrl(string $url): string
+ {
+ $parsed = parse_url($url);
+
+ $scheme = isset($parsed['scheme']) ? $parsed['scheme'] . '://' : '';
+ $host = $parsed['host'] ?? '';
+ $port = isset($parsed['port']) ? ':' . $parsed['port'] : '';
+
+ return $scheme . $host . $port;
+ }
+}
diff --git a/application/Espo/Tools/OAuthServer/Metadata/ProtectedResourceMetadataProvider.php b/application/Espo/Tools/OAuthServer/Metadata/ProtectedResourceMetadataProvider.php
new file mode 100644
index 0000000000..2fa5989798
--- /dev/null
+++ b/application/Espo/Tools/OAuthServer/Metadata/ProtectedResourceMetadataProvider.php
@@ -0,0 +1,81 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Tools\OAuthServer\Metadata;
+
+use Espo\Core\Utils\Cache\DataCacheAccess;
+use Espo\Core\Utils\Config\ApplicationConfig;
+use stdClass;
+
+class ProtectedResourceMetadataProvider
+{
+ private const string CACHE_KEY = 'oAuthServer/protectedResourceMetadata';
+
+ /**
+ * @param DataCacheAccess<stdClass> $dataCacheAccess
+ */
+ public function __construct(
+ private DataCacheAccess $dataCacheAccess,
+ private ApplicationConfig $applicationConfig,
+ ) {
+ $this->dataCacheAccess->init(
+ key: self::CACHE_KEY,
+ loader: fn () => $this->buildData(),
+ );
+ }
+
+ public function get(): stdClass
+ {
+ return $this->dataCacheAccess->get();
+ }
+
+ private function buildData(): stdClass
+ {
+ return (object) [
+ 'resource' => $this->applicationConfig->getSiteUrl(),
+ 'authorization_servers' => [
+ self::serverIdentifierFromUrl($this->applicationConfig->getSiteUrl()),
+ ],
+ 'bearer_methods_supported' => [
+ 'header',
+ ],
+ ];
+ }
+
+ private static function serverIdentifierFromUrl(string $url): string
+ {
+ $parsed = parse_url($url);
+
+ $scheme = isset($parsed['scheme']) ? $parsed['scheme'] . '://' : '';
+ $host = $parsed['host'] ?? '';
+ $port = isset($parsed['port']) ? ':' . $parsed['port'] : '';
+
+ return $scheme . $host . $port;
+ }
+}
diff --git a/public/.well-known/.htaccess b/public/.well-known/.htaccess
new file mode 100644
index 0000000000..c506092e1b
--- /dev/null
+++ b/public/.well-known/.htaccess
@@ -0,0 +1,10 @@
+RewriteEngine On
+
+# Some hosts may require you to use the `RewriteBase` directive.
+# If you need to use the `RewriteBase` directive, it should be the
+# absolute physical path to the directory that contains this htaccess file.
+#
+# RewriteBase /
+
+RewriteCond %{REQUEST_FILENAME} !-f
+RewriteRule ^ index.php [QSA,L]
diff --git a/public/.well-known/index.php b/public/.well-known/index.php
new file mode 100644
index 0000000000..5b814dc05b
--- /dev/null
+++ b/public/.well-known/index.php
@@ -0,0 +1,61 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+require_once('../../bootstrap.php');
+
+use Espo\Core\Application;
+use Espo\Core\Application\Runner\Params;
+use Espo\Core\ApplicationRunners\EntryPoint;
+use Espo\Core\Utils\OAuth\OAuthRouteUtil;
+
+$app = new Application();
+
+$map = [
+ 'oauth-protected-resource' => 'oAuthProtectedResourceMetadata',
+ 'oauth-authorization-server' => 'oAuthAuthorizationServerMetadata',
+];
+
+$path = OAuthRouteUtil::detectPath();
+
+$entryPoint = $map[$path] ?? null;
+
+if (!$entryPoint) {
+ http_response_code(404);
+
+ exit;
+}
+
+$basePath = OAuthRouteUtil::detectBasePast();
+
+$app->setClientBasePath($basePath);
+
+$app->run(
+ EntryPoint::class,
+ Params::create()->with(EntryPoint::PARAM_ENTRY_POINT, $entryPoint)
+);