Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# df-oauth
DreamFactory OAuth support.


## Overview

DreamFactory is a secure, self-hosted enterprise data access platform that provides governed API access to any data source, connecting enterprise applications and on-prem LLMs with role-based access and identity passthrough.

## Configure DreamFactory OAuth Connector

1. Log in to the DreamFactory admin interface.
Expand Down
17 changes: 11 additions & 6 deletions src/Components/DfOAuthOneProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ public function redirect()
\Cache::put('oauth.temp', $temp, 180);
} else {
$temp = $this->server->getTemporaryCredentials();
\Cache::put('oauth_temp', serialize($temp), 180);
// Store as JSON to avoid insecure unserialize() on cache retrieval
\Cache::put('oauth_temp', json_encode([
'identifier' => $temp->getIdentifier(),
'secret' => $temp->getSecret(),
]), 180);
}

return new RedirectResponse($this->server->getAuthorizationUrl($temp));
Expand All @@ -49,10 +53,8 @@ public function getOAuthToken()
if (!$this->isStateless()) {
return \Cache::get('oauth.temp');
} else {
/** @var TemporaryCredentials $temp */
$temp = unserialize(\Cache::get('oauth_temp'));

return $temp->getIdentifier();
$data = json_decode(\Cache::get('oauth_temp'), true);
return $data['identifier'] ?? null;
}
}

Expand All @@ -68,7 +70,10 @@ protected function getToken()
$temp, $this->request->get('oauth_token'), $this->request->get('oauth_verifier')
);
} else {
$temp = unserialize(\Cache::pull('oauth_temp'));
$data = json_decode(\Cache::pull('oauth_temp'), true);
$temp = new TemporaryCredentials();
$temp->setIdentifier($data['identifier'] ?? '');
$temp->setSecret($data['secret'] ?? '');

return $this->server->getTokenCredentials(
$temp, $this->request->get('oauth_token'), $this->request->get('oauth_verifier')
Expand Down
7 changes: 2 additions & 5 deletions src/Resources/SSO.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,9 @@ protected function handlePOST()
} catch (\Exception $e) {
Log::error('OAuth callback failed:', ['error' => $e->getMessage()]);

// For OAuth callbacks, we need to redirect to an error page instead of returning JSON
$errorMessage = urlencode($e->getMessage());

// Check if we're in development mode (detect by checking if port 4200 is accessible)
$baseUrl = $this->getRedirectBaseUrl();
$redirectUrl = $baseUrl . "?error=" . $errorMessage;
// Use generic error message in redirect to avoid leaking internal details
$redirectUrl = $baseUrl . "?error=" . urlencode('Authentication failed. Please try again.');
Log::info('OAuth error redirect URL:', ['url' => $redirectUrl]);

return $this->respond()
Expand Down
29 changes: 15 additions & 14 deletions src/Services/BaseOAuthService.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public function loginOAuthUser(OAuthUserContract $user)
*/
public function createShadowOAuthUser(OAuthUserContract $OAuthUser)
{
$fullName = $OAuthUser->getName() || $OAuthUser->getNickname();
$fullName = $OAuthUser->getName() ?: $OAuthUser->getNickname();
@list($firstName, $lastName) = explode(' ', $fullName);

$email = $OAuthUser->getEmail();
Expand Down Expand Up @@ -337,23 +337,24 @@ protected function isValidRedirectUrl($url)
return false;
}

// Check against whitelist if configured
// Check against whitelist — default to own host if no whitelist configured
$whitelist = config('df.oauth.allowed_redirect_domains', []);
if (!empty($whitelist)) {
$host = $parsed['host'] ?? '';
$allowed = false;
foreach ($whitelist as $pattern) {
if (fnmatch($pattern, $host)) {
$allowed = true;
break;
}
}
if (!$allowed) {
return false;
if (empty($whitelist)) {
// No whitelist configured: only allow redirects to the same host
$appHost = parse_url(config('app.url', ''), PHP_URL_HOST);
$whitelist = $appHost ? [$appHost] : [];
}

$host = $parsed['host'] ?? '';
$allowed = false;
foreach ($whitelist as $pattern) {
if (fnmatch($pattern, $host)) {
$allowed = true;
break;
}
}

return true;
return $allowed;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Services/Google.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ protected function findRoleByGroupEmail($groupEmail)
*/
public function createShadowOAuthUser(OAuthUserContract $OAuthUser)
{
$fullName = $OAuthUser->getName() || $OAuthUser->getNickname();
$fullName = $OAuthUser->getName() ?: $OAuthUser->getNickname();
@list($firstName, $lastName) = explode(' ', $fullName);

$email = $OAuthUser->getEmail();
Expand Down
2 changes: 1 addition & 1 deletion src/Services/HerokuAddonSSOService.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private function checkToken($token, $timestamp, $resourceId)
$secret = $this->getConfig('secret');
}
}
if (sha1("{$resourceId}:{$secret}:{$timestamp}") !== $token) {
if (!hash_equals(sha1("{$resourceId}:{$secret}:{$timestamp}"), $token)) {
throw new ForbiddenException('Token invalid. Please provide valid token');
}
}
Expand Down