Skip to main content
Drupal is a registered trademark of Dries Buytaert
Release: Views Bootstrap 5.5.4 — Minor update available for module views_bootstrap (5.5.4). Release: Rightup theme 1.0.3 — Minor update available for theme vartheme_bs5_rightup (1.0.3). Release: CommentOn 1.0.6 — Minor update available for module commenton (1.0.6). Release: ChatGPT Ads 1.0.3 — Minor update available for module chatgpt_ads (1.0.3). Release: Opensolr Search 5.1.3 — Minor update available for module opensolr_search (5.1.3). Release: ServiceM8 Webform Integration 1.2.1 — Minor update available for module servicem8_webform (1.2.1). Release: PostfixAdmin 1.0.0 — Initial release available for module postfix_admin (1.0.0)! Release: ip_analyser 1.0.3 — Minor update available for module ip_analyser (1.0.3). Module Revived: Swagger-PHP OpenAPI 3 documentation generator 1.0.0 — Module swagger_php updated after 10 months of inactivity (1.0.0). Security Coverage: Microsoft Azure AI — Module ai_provider_azure now has official Drupal security advisory coverage.

Social auth drupal simple oauth

No security coverage

Part of the Social ecosystem · 37 projects

View on drupal.org

Social auth Drupal simple OAuth module integrates social_auth and simple_oauth module, Provide single sign on function. server need install simple_oauth

install

composer require "drupal/social_auth_drupal_simple_oauth"

HOOK

use Symfony\Component\HttpFoundation\RedirectResponse;

function mymodule_social_auth_simple_oauth($data, $sdk) {
  $userId = $data->getId();
  $httpClient = \Drupal::httpClient();
  $config = \Drupal::configFactory()->get('social_auth_drupal_simple_oauth.settings');

  // server need enable restui module.
  // http://myserver.com/user/1
   $userInfo = $httpClient
    ->get($config->get('base_url') . '/user/' . $userId, [
      'query' => [
        '_format'      => 'json',
      ],
      'headers' => [
        'Authorization' => 'Bearer ' . $sdk->getAccessToken()->__toString()
      ]
    ])
    ->getBody()->getContents();

  $userInfo = \GuzzleHttp\json_decode($userInfo, TRUE);

  if (isset($userInfo['@context']['@language'])) {
    $userInfo = $userInfo[$userInfo['@context']['@language']];
  }

  $userInfo = array_map(function ($info) {
    if (count($info) == 1 && isset($info[0]['value'])) {
      return $info[0]['value'];
    }
  }, $userInfo);

  if (!isset($userInfo['mail'])) {
    exit('can\t get mail field');
  }

  $user = autoCreateUser($userInfo);

  if (!$user) {
    exit('user not found');
  }

  user_login_finalize($user);

  $destination = $controller->getDataHander()->get('login_destination');

  // If there was a destination parameter.
  if ($destination) {
    // Deletes the session key.
    $controller->dataHandler->set('login_destination', NULL);

    return new RedirectResponse(Url::fromUserInput($destination)->toString());
  }

  $response = new RedirectResponse(\Drupal\Core\Url::fromRoute('user.page')->toString());
  $response->send();
}

function autoCreateUser($userInfo) {
  $mail = $userInfo['mail'];
  $name = $userInfo['name'];
  $timezone = $userInfo['timezone'];

  $user = user_load_by_mail($mail);

  if (!$user) {
    if (user_load_by_name($name)) {
      $name = explode('@', $mail)[0] . $name;
    }
    $user = \Drupal\user\Entity\User::create([
      'name' => $name,
      'mail' => $mail,
      'status' => 1,
      'timezone' => $timezone,
    ]);
    $user->save();
  }
  return $user;
}

Depends on

Dependencies of the latest stable release

No dependencies recorded for this project.

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

Tracked releases
1
Tracked since
Jun 2023
Latest release
3 years ago
Releases (12 mo)
0
Maintenance
Dormant

Releases

Version Type Core Release date
2.0.x-dev Dev Jun 28, 2023