-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathOAuth.php
More file actions
149 lines (127 loc) · 4.02 KB
/
OAuth.php
File metadata and controls
149 lines (127 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/**
* @author Dariusz Prząda <artdarek@gmail.com>
* @copyright Copyright (c) 2013
* @license http://www.opensource.org/licenses/mit-license.html MIT License
*/
namespace Artdarek\OAuth;
use Illuminate\Support\ServiceProvider;
use \Config;
use \URL;
use \OAuth\ServiceFactory;
use \OAuth\Common\Consumer\Credentials;
class OAuth
{
/**
* @var ServiceFactory
*/
private $_serviceFactory;
/**
* Storege name from config
* @var string
*/
private $_storage_name = 'Session';
/**
* Client ID from config
* @var string
*/
private $_client_id;
/**
* Client secret from config
* @var string
*/
private $_client_secret;
/**
* Scope from config
* @var array
*/
private $_scope = array();
/**
* Constructor
*
* @param ServiceFactory $serviceFactory - (Dependency injection) If not provided, a ServiceFactory instance will be constructed.
*/
public function __construct(ServiceFactory $serviceFactory = null)
{
if (null === $serviceFactory) {
// Create the service factory
$serviceFactory = new ServiceFactory();
}
$this->_serviceFactory = $serviceFactory;
}
/**
* Detect config and set data from it
*
* @param string $service
*/
public function setConfig( $service )
{
// if config/oauth-4-laravel.php exists use this one
if ( Config::get('oauth-4-laravel.consumers') != null ) {
$this->_storage_name = Config::get('oauth-4-laravel.storage', 'Session');
$this->_client_id = Config::get("oauth-4-laravel.consumers.$service.client_id");
$this->_client_secret = Config::get("oauth-4-laravel.consumers.$service.client_secret");
$this->_scope = Config::get("oauth-4-laravel.consumers.$service.scope", array() );
// else try to find config in packages configs
} else {
$this->_storage_name = Config::get('oauth-4-laravel::storage', 'Session');
$this->_client_id = Config::get("oauth-4-laravel::consumers.$service.client_id");
$this->_client_secret = Config::get("oauth-4-laravel::consumers.$service.client_secret");
$this->_scope = Config::get("oauth-4-laravel::consumers.$service.scope", array() );
}
}
/**
* Create storage instance
*
* @param string $storageName
* @return \OAuth\Common\Storage
*/
public function createStorageInstance($storageName)
{
if ($storageName == "LaravelSession") {
$storageClass = "\\Artdarek\\OAuth\\Common\\Storage\\$storageName";
} else {
$storageClass = "\\OAuth\\Common\\Storage\\$storageName";
}
$storage = new $storageClass();
return $storage;
}
/**
* Set the http client object
*
* @param string $httpClientName
* @return void
*/
public function setHttpClient($httpClientName)
{
$httpClientClass = "\\OAuth\\Common\\Http\\Client\\$httpClientName";
$this->_serviceFactory->setHttpClient(new $httpClientClass());
}
/**
* @param string $service
* @param string $url
* @param array $scope
* @return \OAuth\Common\Service\AbstractService
*/
public function consumer( $service, $url = null, $scope = null )
{
// get config
$this->setConfig( $service );
// get storage object
$storage = $this->createStorageInstance( $this->_storage_name );
// create credentials object
$credentials = new Credentials(
$this->_client_id,
$this->_client_secret,
$url ?: URL::current()
);
// check if scopes were provided
if (is_null($scope))
{
// get scope from config (default to empty array)
$scope = $this->_scope;
}
// return the service consumer object
return $this->_serviceFactory->createService($service, $credentials, $storage, $scope);
}
}