blob: e135c2dd2efb79cc6f2e434add2e46caf244446e [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001<?php
2
3namespace OAuth2\GrantType;
4
5use OAuth2\ClientAssertionType\HttpBasic;
6use OAuth2\ResponseType\AccessTokenInterface;
7use OAuth2\Storage\ClientCredentialsInterface;
8
9/**
10 * @author Brent Shaffer <bshafs at gmail dot com>
11 *
12 * @see HttpBasic
13 */
14class ClientCredentials extends HttpBasic implements GrantTypeInterface
15{
16 /**
17 * @var array
18 */
19 private $clientData;
20
21 /**
22 * @param ClientCredentialsInterface $storage
23 * @param array $config
24 */
25 public function __construct(ClientCredentialsInterface $storage, array $config = array())
26 {
27 /**
28 * The client credentials grant type MUST only be used by confidential clients
29 *
30 * @see http://tools.ietf.org/html/rfc6749#section-4.4
31 */
32 $config['allow_public_clients'] = false;
33
34 parent::__construct($storage, $config);
35 }
36
37 /**
38 * Get query string identifier
39 *
40 * @return string
41 */
42 public function getQueryStringIdentifier()
43 {
44 return 'client_credentials';
45 }
46
47 /**
48 * Get scope
49 *
50 * @return string|null
51 */
52 public function getScope()
53 {
54 $this->loadClientData();
55
56 return isset($this->clientData['scope']) ? $this->clientData['scope'] : null;
57 }
58
59 /**
60 * Get user id
61 *
62 * @return mixed
63 */
64 public function getUserId()
65 {
66 $this->loadClientData();
67
68 return isset($this->clientData['user_id']) ? $this->clientData['user_id'] : null;
69 }
70
71 /**
72 * Create access token
73 *
74 * @param AccessTokenInterface $accessToken
75 * @param mixed $client_id - client identifier related to the access token.
76 * @param mixed $user_id - user id associated with the access token
77 * @param string $scope - scopes to be stored in space-separated string.
78 * @return array
79 */
80 public function createAccessToken(AccessTokenInterface $accessToken, $client_id, $user_id, $scope)
81 {
82 /**
83 * Client Credentials Grant does NOT include a refresh token
84 *
85 * @see http://tools.ietf.org/html/rfc6749#section-4.4.3
86 */
87 $includeRefreshToken = false;
88
89 return $accessToken->createAccessToken($client_id, $user_id, $scope, $includeRefreshToken);
90 }
91
92 private function loadClientData()
93 {
94 if (!$this->clientData) {
95 $this->clientData = $this->storage->getClientDetails($this->getClientId());
96 }
97 }
98}