|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * conjoon |
| 5 | + * php-lib-conjoon |
| 6 | + * Copyright (C) 2023 Thorsten Suckow-Homberg https://github.com/conjoon/php-lib-conjoon |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person |
| 9 | + * obtaining a copy of this software and associated documentation |
| 10 | + * files (the "Software"), to deal in the Software without restriction, |
| 11 | + * including without limitation the rights to use, copy, modify, merge, |
| 12 | + * publish, distribute, sublicense, and/or sell copies of the Software, |
| 13 | + * and to permit persons to whom the Software is furnished to do so, |
| 14 | + * subject to the following conditions: |
| 15 | + * |
| 16 | + * The above copyright notice and this permission notice shall be included |
| 17 | + * in all copies or substantial portions of the Software. |
| 18 | + * |
| 19 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 21 | + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 22 | + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 23 | + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 24 | + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 25 | + * USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | +declare(strict_types=1); |
| 29 | + |
| 30 | +namespace Conjoon\Illuminate\Auth\LocalMailAccount; |
| 31 | + |
| 32 | +use Conjoon\Illuminate\Auth\Imap\DefaultImapUserProvider; |
| 33 | +use Conjoon\Illuminate\Auth\ImapUser; |
| 34 | +use Conjoon\Mail\Client\Data\MailAccount; |
| 35 | +use Illuminate\Http\Request; |
| 36 | + |
| 37 | +/** |
| 38 | + * Auth provider that expects required mailserver configurations to be available |
| 39 | + * with the payload ("x-cnmail-data"-header). |
| 40 | + */ |
| 41 | +class LocalAccountProvider extends DefaultImapUserProvider |
| 42 | +{ |
| 43 | + /** |
| 44 | + * @var string |
| 45 | + */ |
| 46 | + private $payload; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var string |
| 50 | + */ |
| 51 | + private $mailAccountId; |
| 52 | + |
| 53 | + |
| 54 | + public function __construct(Request $request) |
| 55 | + { |
| 56 | + $this->payload = $request->header("x-cnmail-data"); |
| 57 | + $this->mailAccountId = $request->route('mailAccountId'); |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + public function getUser(string $username, string $password): ?ImapUser |
| 62 | + { |
| 63 | + $config = $this->decodePayload($this->payload); |
| 64 | + $mailAccount = $this->createMailAccount($username, $password, $config); |
| 65 | + |
| 66 | + return new ImapUser($username, $password, $mailAccount); |
| 67 | + } |
| 68 | + |
| 69 | + private function createMailAccount(string $username, string $password, array $config): MailAccount |
| 70 | + { |
| 71 | + $merged = array_merge($config, [ |
| 72 | + "id" => $this->mailAccountId, |
| 73 | + "name" => $this->mailAccountId, |
| 74 | + "inbox_user" => $username, |
| 75 | + "inbox_password" => $password |
| 76 | + ]); |
| 77 | + return new MailAccount($merged); |
| 78 | + } |
| 79 | + |
| 80 | + private function decodePayload(string $payload): array |
| 81 | + { |
| 82 | + return json_decode(base64_decode($payload), true); |
| 83 | + } |
| 84 | +} |
0 commit comments