Skip to content

Commit 9a036dd

Browse files
committed
2-legged OAuth2 using the client_credentials mautic#257
1 parent 9087dde commit 9a036dd

File tree

2 files changed

+152
-5
lines changed

2 files changed

+152
-5
lines changed

README.md

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ then copy the Client ID and Client Secret to the application that will be using
2525
## Authorization
2626

2727
### Obtaining an access token
28-
The first step is to obtain authorization. Mautic supports OAuth 1.0a and OAuth 2 however it is up to the administrator
29-
to decide which is enabled. Thus it is best to have a configuration option within your project for the administrator
28+
The first step is to obtain authorization. Mautic supports OAuth 2. Thus it is best to have a configuration option within your project for the administrator
3029
to choose what method should be used by your code.
3130

3231
```php
@@ -46,7 +45,6 @@ $callback = '';
4645
// ApiAuth->newAuth() will accept an array of Auth settings
4746
$settings = [
4847
'baseUrl' => '', // Base URL of the Mautic instance
49-
'version' => 'OAuth2', // Version of the OAuth can be OAuth2 or OAuth1a. OAuth2 is the default value.
5048
'clientKey' => '', // Client/Consumer key from Mautic
5149
'clientSecret' => '', // Client/Consumer secret key from Mautic
5250
'callback' => '', // Redirect URI/Callback URI for this script
@@ -55,7 +53,6 @@ $settings = [
5553
/*
5654
// If you already have the access token, et al, pass them in as well to prevent the need for reauthorization
5755
$settings['accessToken'] = $accessToken;
58-
$settings['accessTokenSecret'] = $accessTokenSecret; //for OAuth1.0a
5956
$settings['accessTokenExpires'] = $accessTokenExpires; //UNIX timestamp
6057
$settings['refreshToken'] = $refreshToken;
6158
*/
@@ -76,7 +73,6 @@ try {
7673
// refresh token
7774

7875
// $accessTokenData will have the following keys:
79-
// For OAuth1.0a: access_token, access_token_secret, expires
8076
// For OAuth2: access_token, expires, token_type, refresh_token
8177

8278
if ($auth->accessTokenUpdated()) {
@@ -90,6 +86,41 @@ try {
9086
}
9187
```
9288

89+
90+
### Using 2-legged OAuth2 using Client Credentials
91+
92+
The Client Credentials grant is used when applications request an access token to access their own resources, not on behalf of a user.
93+
94+
```php
95+
<?php
96+
97+
// Bootup the Composer autoloader
98+
include __DIR__ . '/vendor/autoload.php';
99+
100+
use Mautic\Auth\ApiAuth;
101+
102+
$settings = [
103+
'AuthMethod' => 'TwoLeggedOAuth2',
104+
'clientKey' => '',
105+
'clientSecret' => '',
106+
'baseUrl' => '',
107+
];
108+
109+
// $settings['accessToken'] = 'your stored access token';
110+
111+
112+
$initAuth = new ApiAuth();
113+
$auth = $initAuth->newAuth($settings, $settings['AuthMethod']);
114+
115+
if (!isset($settings['accessToken'])) {
116+
// store it for one hour and use it in $settings above
117+
$accessToken = $auth->getAccessToken();
118+
}
119+
120+
// Nothing else to do ... It's ready to use.
121+
// Just pass the auth object to the API context you are creating.
122+
```
123+
93124
### Using Basic Authentication Instead
94125
Instead of messing around with OAuth, you may simply elect to use BasicAuth instead.
95126

lib/Auth/TwoLeggedOAuth2.php

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
/*
4+
* @copyright 2021 Mautic Contributors. All rights reserved
5+
* @author Mautic, Inc.
6+
*
7+
* @link https://mautic.org
8+
*
9+
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10+
*/
11+
12+
namespace Mautic\Auth;
13+
14+
use Mautic\Exception\RequiredParameterMissingException;
15+
16+
class TwoLeggedOAuth2 extends AbstractAuth
17+
{
18+
/**
19+
* Password associated with Username.
20+
*
21+
* @var string
22+
*/
23+
private $clientSecret;
24+
25+
/**
26+
* Username or email, basically the Login Identifier.
27+
*
28+
* @var string
29+
*/
30+
private $clientKey;
31+
32+
/**
33+
* Access token returned by OAuth server.
34+
*
35+
* @var string
36+
*/
37+
protected $_access_token;
38+
39+
/**
40+
* @var string
41+
*/
42+
private $baseurl;
43+
44+
/**
45+
* @var string
46+
*/
47+
private $_access_token_url;
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function isAuthorized()
53+
{
54+
return !empty($this->clientKey) && !empty($this->clientSecret);
55+
}
56+
57+
/**
58+
* @param string $baseUrl
59+
* @param string $clientKey The username to use for Authentication *Required*
60+
* @param string $clientSecret The Password to use *Required*
61+
*
62+
* @throws RequiredParameterMissingException
63+
*/
64+
public function setup($baseUrl, $clientKey, $clientSecret, $accessToken = null)
65+
{
66+
// we MUST have the username and password. No Blanks allowed!
67+
//
68+
// remove blanks else Empty doesn't work
69+
$clientKey = trim($clientKey);
70+
$clientSecret = trim($clientSecret);
71+
72+
if (empty($clientKey) || empty($clientSecret)) {
73+
//Throw exception if the required parameters were not found
74+
$this->log('parameters did not include clientkey and/or clientSecret');
75+
throw new RequiredParameterMissingException('One or more required parameters was not supplied. Both clientKey and clientSecret required!');
76+
}
77+
78+
$this->baseurl = $baseUrl;
79+
$this->clientKey = $clientKey;
80+
$this->clientSecret = $clientSecret;
81+
$this->_access_token = $accessToken;
82+
83+
if (!$this->_access_token_url) {
84+
$this->_access_token_url = $baseUrl.'/oauth/v2/token';
85+
}
86+
}
87+
88+
/**
89+
* @param $url
90+
* @param $method
91+
*
92+
* @return array
93+
*/
94+
protected function prepareRequest($url, array $headers, array $parameters, $method, array $settings)
95+
{
96+
if (null !== $this->_access_token) {
97+
$headers = array_merge($headers, ['Authorization: Bearer '.$this->_access_token]);
98+
}
99+
100+
return [$headers, $parameters];
101+
}
102+
103+
public function getAccessToken(): string
104+
{
105+
$parameters = [
106+
'client_id' => $this->clientKey,
107+
'client_secret' => $this->clientSecret,
108+
'grant_type' => 'client_credentials',
109+
];
110+
$accessTokenData = $this->makeRequest($this->_access_token_url, $parameters, 'POST');
111+
//store access token data however you want
112+
$this->_access_token = $accessTokenData['access_token'] ?? null;
113+
114+
return $this->_access_token;
115+
}
116+
}

0 commit comments

Comments
 (0)