AuthController.php 2.87 KB
Newer Older
Jacob Priddy's avatar
Jacob Priddy committed
1
2
3
4
5
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
6
use Illuminate\Cookie\CookieJar;
Jacob Priddy's avatar
Jacob Priddy committed
7
use Illuminate\Http\JsonResponse;
8
9
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Cookie;
Jacob Priddy's avatar
Jacob Priddy committed
10
11
12
13
14
use Illuminate\Auth\AuthenticationException;
use Source\Exceptions\AuthorizationException;
use Source\Exceptions\EntityNotFoundException;
use Source\UseCases\Users\Authenticate\APIPresenter;
use Source\UseCases\Users\Authenticate\AuthenticateUseCase;
15
use Source\UseCases\Users\Authenticate\UserCreationException;
Jacob Priddy's avatar
Jacob Priddy committed
16

Jacob Priddy's avatar
Jacob Priddy committed
17
class AuthController extends ApiController {
Jacob Priddy's avatar
Jacob Priddy committed
18
19
    protected Request $request;

20
21
22
    protected CookieJar $cookieJar;

    public function __construct(Request $request, CookieJar $cookieJar) {
Jacob Priddy's avatar
Jacob Priddy committed
23
        $this->request = $request;
24
        $this->cookieJar = $cookieJar;
Jacob Priddy's avatar
Jacob Priddy committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    }

    /**
     * @param AuthenticateUseCase $authenticateUseCase
     * @return JsonResponse
     * @throws AuthenticationException
     * @throws EntityNotFoundException
     */
    public function login(AuthenticateUseCase $authenticateUseCase): JsonResponse {
        $presenter = new APIPresenter();

        try {
            $authenticateUseCase->attempt($presenter, $this->request->all());
        } catch (AuthorizationException $e) {
            throw new AuthenticationException();
        }

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
        return $this->respondWithData($presenter->getViewModel())->withCookie(
            cookie(
                'api_token',
                $presenter->getViewModel()['token']['value'],
                $presenter->getViewModel()['token']['minutes']
            )
        );
    }

    public function samlLogin(AuthenticateUseCase $authenticateUseCase): RedirectResponse {
        return redirect()->to($authenticateUseCase->handToSaml());
    }

    /**
     * @param AuthenticateUseCase $authenticateUseCase
     * @return mixed
     * @throws EntityNotFoundException
     */
    public function handle(AuthenticateUseCase $authenticateUseCase) {
        $presenter = new APIPresenter();

        try {
            $authenticateUseCase->handleSamlLogin($presenter);
        } catch (UserCreationException $e) {
            return $this->respondWithError(
                'There was an error authenticating the user. Please contact an administrator.'
            );
        }

        return redirect()->intended(url(config('saml.home_page')))->cookie(
Jacob Priddy's avatar
Jacob Priddy committed
72
73
74
75
            'api_token',
            $presenter->getViewModel()['token']['value'],
            $presenter->getViewModel()['token']['minutes']
        );
Jacob Priddy's avatar
Jacob Priddy committed
76
    }
77
78
79
80
81
82
83
84
85
86
87
88
89
90

    /**
     * @param AuthenticateUseCase $authenticateUseCase
     * @return RedirectResponse
     */
    public function samlLogout(AuthenticateUseCase $authenticateUseCase): RedirectResponse {
        Cookie::queue($this->cookieJar->forget('api_token'));

        return redirect()->to(
            $authenticateUseCase->samlLogout(
                $this->request->cookie('api_token')
            )
        );
    }
Jacob Priddy's avatar
Jacob Priddy committed
91
}