the browser-facing portion of osu!
at master 2.2 kB view raw
1<?php 2 3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0. 4// See the LICENCE file in the repository root for full licence text. 5 6declare(strict_types=1); 7 8namespace App\Libraries\SessionVerification; 9 10use App\Events\UserSessionEvent; 11use App\Interfaces\SessionVerificationInterface; 12use App\Mail\UserVerification as UserVerificationMail; 13use App\Models\LoginAttempt; 14use App\Models\User; 15 16class Helper 17{ 18 public static function currentSession(): ?SessionVerificationInterface 19 { 20 return is_api_request() ? oauth_token() : \Session::instance(); 21 } 22 23 public static function currentUserOrFail(): User 24 { 25 $user = \Auth::user(); 26 app('OsuAuthorize')->ensureLoggedIn($user); 27 28 return $user; 29 } 30 31 public static function issue(SessionVerificationInterface $session, User $user, bool $initial = false): void 32 { 33 if ($initial) { 34 if (State::fromSession($session) === null) { 35 static::logAttempt('input', 'new'); 36 } else { 37 return; 38 } 39 } 40 41 if (!is_valid_email_format($user->user_email)) { 42 return; 43 } 44 45 $state = State::create($session); 46 $keys = [ 47 'link' => $state->linkKey, 48 'main' => $state->key, 49 ]; 50 51 $request = \Request::instance(); 52 LoginAttempt::logAttempt($request->getClientIp(), $user, 'verify'); 53 54 $requestCountry = app('countries')->byCode(request_country($request) ?? '')?->name; 55 56 \Mail::to($user) 57 ->queue(new UserVerificationMail( 58 compact('keys', 'user', 'requestCountry') 59 )); 60 } 61 62 public static function logAttempt(string $source, string $type, string $reason = null): void 63 { 64 datadog_increment( 65 'verification.attempts', 66 compact('reason', 'source', 'type') 67 ); 68 } 69 70 public static function markVerified(SessionVerificationInterface $session, State $state) 71 { 72 $session->markVerified(); 73 $state->delete(); 74 UserSessionEvent::newVerified($session->userId(), $session->getKeyForEvent())->broadcast(); 75 } 76}