blob: b10c2dd095b9ccee0f38c4e1370256dfab263783 [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001<?php
2
3namespace OAuth2\GrantType;
4
5use OAuth2\Storage\UserCredentialsInterface;
6use OAuth2\ResponseType\AccessTokenInterface;
7use OAuth2\RequestInterface;
8use OAuth2\ResponseInterface;
9use LogicException;
10
11/**
12 * @author Brent Shaffer <bshafs at gmail dot com>
13 */
14class UserCredentials implements GrantTypeInterface
15{
16 /**
17 * @var array
18 */
19 private $userInfo;
20
21 /**
22 * @var UserCredentialsInterface
23 */
24 protected $storage;
25
26 /**
27 * @param UserCredentialsInterface $storage - REQUIRED Storage class for retrieving user credentials information
28 */
29 public function __construct(UserCredentialsInterface $storage)
30 {
31 $this->storage = $storage;
32 }
33
34 /**
35 * @return string
36 */
37 public function getQueryStringIdentifier()
38 {
39 return 'password';
40 }
41
42 /**
43 * @param RequestInterface $request
44 * @param ResponseInterface $response
45 * @return bool|mixed|null
46 *
47 * @throws LogicException
48 */
49 public function validateRequest(RequestInterface $request, ResponseInterface $response)
50 {
51 if (!$request->request("password") || !$request->request("username")) {
52 $response->setError(400, 'invalid_request', 'Missing parameters: "username" and "password" required');
53
54 return null;
55 }
56
57 if (!$this->storage->checkUserCredentials($request->request("username"), $request->request("password"))) {
58 $response->setError(401, 'invalid_grant', 'Invalid username and password combination');
59
60 return null;
61 }
62
63 $userInfo = $this->storage->getUserDetails($request->request("username"));
64
65 if (empty($userInfo)) {
66 $response->setError(400, 'invalid_grant', 'Unable to retrieve user information');
67
68 return null;
69 }
70
71 if (!isset($userInfo['user_id'])) {
72 throw new \LogicException("you must set the user_id on the array returned by getUserDetails");
73 }
74
75 $this->userInfo = $userInfo;
76
77 return true;
78 }
79
80 /**
81 * Get client id
82 *
83 * @return mixed|null
84 */
85 public function getClientId()
86 {
87 return null;
88 }
89
90 /**
91 * Get user id
92 *
93 * @return mixed
94 */
95 public function getUserId()
96 {
97 return $this->userInfo['user_id'];
98 }
99
100 /**
101 * Get scope
102 *
103 * @return null|string
104 */
105 public function getScope()
106 {
107 return isset($this->userInfo['scope']) ? $this->userInfo['scope'] : null;
108 }
109
110 /**
111 * Create access token
112 *
113 * @param AccessTokenInterface $accessToken
114 * @param mixed $client_id - client identifier related to the access token.
115 * @param mixed $user_id - user id associated with the access token
116 * @param string $scope - scopes to be stored in space-separated string.
117 * @return array
118 */
119 public function createAccessToken(AccessTokenInterface $accessToken, $client_id, $user_id, $scope)
120 {
121 return $accessToken->createAccessToken($client_id, $user_id, $scope);
122 }
123}