You are here

public function OpenIDConnect::generateUsername in OpenID Connect / OAuth client 2.x

Same name and namespace in other branches
  1. 8 src/OpenIDConnect.php \Drupal\openid_connect\OpenIDConnect::generateUsername()

Generate a username for a new account.

Parameters

string $sub: The subject identifier.

array $userinfo: The user claims.

string $client_name: The client identifier.

Return value

string A unique username.

1 call to OpenIDConnect::generateUsername()
OpenIDConnect::createUser in src/OpenIDConnect.php
Create a user indicating sub-id and login provider.

File

src/OpenIDConnect.php, line 544

Class

OpenIDConnect
Main service of the OpenID Connect module.

Namespace

Drupal\openid_connect

Code

public function generateUsername(string $sub, array $userinfo, string $client_name) : string {
  $name = 'oidc_' . $client_name . '_' . md5($sub);
  $candidates = [
    'preferred_username',
    'name',
  ];
  foreach ($candidates as $candidate) {
    if (!empty($userinfo[$candidate])) {
      $name = trim($userinfo[$candidate]);
      break;
    }
  }

  // Ensure there are no duplicates.
  for ($original = $name, $i = 1; $this
    ->usernameExists($name); $i++) {
    $name = $original . '_' . $i;
  }
  return $name;
}