1 <?php namespace BookStack\Auth\Access;
3 use BookStack\Auth\Access;
4 use BookStack\Auth\User;
5 use BookStack\Auth\UserRepo;
6 use BookStack\Exceptions\SamlException;
7 use Illuminate\Contracts\Auth\Authenticatable;
8 use Illuminate\Database\Eloquent\Builder;
9 use Illuminate\Support\Facades\Log;
14 * Handles any app-specific SAML tasks.
15 * @package BookStack\Services
17 class Saml2Service extends Access\ExternalAuthService
25 * Saml2Service constructor.
26 * @param \BookStack\Auth\UserRepo $userRepo
28 public function __construct(UserRepo $userRepo, User $user)
30 $this->config = config('services.saml');
31 $this->userRepo = $userRepo;
33 $this->enabled = config('saml2_settings.enabled') === true;
37 * Check if groups should be synced.
40 public function shouldSyncGroups()
42 return $this->enabled && $this->config['user_to_groups'] !== false;
45 /** Calculate the display name
46 * @param array $samlAttributes
47 * @param string $defaultValue
50 protected function getUserDisplayName(array $samlAttributes, string $defaultValue)
52 $displayNameAttr = $this->config['display_name_attribute'];
55 foreach ($displayNameAttr as $dnAttr) {
56 $dnComponent = $this->getSamlResponseAttribute($samlAttributes, $dnAttr, null);
57 if ($dnComponent !== null) {
58 $displayName[] = $dnComponent;
62 if (count($displayName) == 0) {
63 $displayName = $defaultValue;
65 $displayName = implode(' ', $displayName);
71 protected function getUserName(array $samlAttributes, string $defaultValue)
73 $userNameAttr = $this->config['user_name_attribute'];
75 if ($userNameAttr === null) {
76 $userName = $defaultValue;
78 $userName = $this->getSamlResponseAttribute($samlAttributes, $userNameAttr, $defaultValue);
85 * Extract the details of a user from a SAML response.
87 * @param $samlAttributes
90 public function getUserDetails($samlID, $samlAttributes)
92 $emailAttr = $this->config['email_attribute'];
93 $userName = $this->getUserName($samlAttributes, $samlID);
97 'name' => $this->getUserDisplayName($samlAttributes, $userName),
99 'email' => $this->getSamlResponseAttribute($samlAttributes, $emailAttr, null),
104 * Get the groups a user is a part of from the SAML response.
105 * @param array $samlAttributes
108 public function getUserGroups($samlAttributes)
110 $groupsAttr = $this->config['group_attribute'];
111 $userGroups = $samlAttributes[$groupsAttr];
113 if (!is_array($userGroups)) {
121 * Get a property from an SAML response.
122 * Handles properties potentially being an array.
123 * @param array $userDetails
124 * @param string $propertyKey
125 * @param $defaultValue
128 protected function getSamlResponseAttribute(array $samlAttributes, string $propertyKey, $defaultValue)
130 if (isset($samlAttributes[$propertyKey])) {
131 $data = $samlAttributes[$propertyKey];
132 if (is_array($data)) {
133 if (count($data) == 0) {
134 $data = $defaultValue;
135 } else if (count($data) == 1) {
140 $data = $defaultValue;
147 * Register a user that is authenticated but not
148 * already registered.
149 * @param array $userDetails
152 protected function registerUser($userDetails)
154 // Create an array of the user data to create a new user instance
156 'name' => $userDetails['name'],
157 'email' => $userDetails['email'],
158 'password' => str_random(30),
159 'external_auth_id' => $userDetails['uid'],
160 'email_confirmed' => true,
163 $user = $this->user->forceCreate($userData);
164 $this->userRepo->attachDefaultRole($user);
165 $this->userRepo->downloadAndAssignUserAvatar($user);
170 * Get the user from the database for the specified details.
171 * @param array $userDetails
174 protected function getOrRegisterUser($userDetails)
176 $isRegisterEnabled = config('services.saml.auto_register') === true;
178 ->where('external_auth_id', $userDetails['uid'])
181 if ($user === null && $isRegisterEnabled) {
182 $user = $this->registerUser($userDetails);
189 * Process the SAML response for a user. Login the user when
190 * they exist, optionally registering them automatically.
191 * @param string $samlID
192 * @param array $samlAttributes
194 public function processLoginCallback($samlID, $samlAttributes)
196 $userDetails = $this->getUserDetails($samlID, $samlAttributes);
197 $isLoggedIn = auth()->check();
200 logger()->error("Already logged in");
202 $user = $this->getOrRegisterUser($userDetails);
203 if ($user === null) {
204 logger()->error("User does not exist");
206 auth()->login($user);