Open up Marketplace API

added following APIs
* Public Cloud API
* Private Cloud API
* Consultants API

Change-Id: Ib6adb1b10507446ea2f8c15939d33e748d307939
Implements: blueprint oauth2-marketplace-api
This commit is contained in:
Sebastian Marcet 2015-03-17 14:49:30 -03:00
parent 02218a989d
commit 407201d6bc
59 changed files with 2356 additions and 101 deletions

View File

@ -42,6 +42,7 @@ return array(
'OAuth2_AuthorizationCode_Lifetime' => 240,
'OAuth2_AccessToken_Lifetime' => 3600,
'OAuth2_RefreshToken_Lifetime' => 0,
'OAuth2_Enable' => true,
//oauth2 security policy configuration
'OAuth2SecurityPolicy_MinutesWithoutExceptions' => 2,
'OAuth2SecurityPolicy_MaxBearerTokenDisclosureAttempts' => 5,

View File

@ -75,6 +75,7 @@ class ApiEndpointController extends AbstractRESTController implements ICRUDContr
'route' => 'required|route',
'http_method' => 'required|httpmethod',
'api_id' => 'required|integer',
'rate_limit' => 'required|integer',
);
// Creates a Validator instance and validates the data.
@ -92,7 +93,8 @@ class ApiEndpointController extends AbstractRESTController implements ICRUDContr
$new_api_endpoint['allow_cors'],
$new_api_endpoint['route'],
$new_api_endpoint['http_method'],
$new_api_endpoint['api_id']
$new_api_endpoint['api_id'],
$new_api_endpoint['rate_limit']
);
return $this->created(array('api_endpoint_id' => $new_api_endpoint_model->id));
}
@ -135,6 +137,7 @@ class ApiEndpointController extends AbstractRESTController implements ICRUDContr
'allow_cors' => 'sometimes|required|boolean',
'route' => 'sometimes|required|route',
'http_method' => 'sometimes|required|httpmethod',
'rate_limit' => 'required|integer',
);
// Creates a Validator instance and validates the data.

View File

@ -16,26 +16,57 @@ abstract class JsonController extends BaseController {
protected function error500(Exception $ex){
$this->log_service->error($ex);
return Response::json(array('error' => 'server error'), 500);
return Response::json(array('message' => 'server error'), 500);
}
protected function created($data='ok'){
return Response::json($data, 201);
$res = Response::json($data, 201);
//jsonp
if(Input::has('callback'))
$res->setCallback(Input::get('callback'));
return $res;
}
protected function deleted($data='ok'){
return Response::json($data, 204);
$res = Response::json($data, 204);
//jsonp
if(Input::has('callback'))
$res->setCallback(Input::get('callback'));
return $res;
}
protected function ok($data='ok'){
return Response::json($data, 200);
$res = Response::json($data, 200);
//jsonp
if(Input::has('callback'))
$res->setCallback(Input::get('callback'));
return $res;
}
protected function error400($data){
return Response::json($data, 400);
}
protected function error404($data){
protected function error404($data = array('message' => 'Entity Not Found')){
return Response::json($data, 404);
}
/**
* {
"message": "Validation Failed",
"errors": [
{
"resource": "Issue",
"field": "title",
"code": "missing_field"
}
]
}
* @param $messages
* @return mixed
*/
protected function error412($messages){
return Response::json(array('message' => 'Validation Failed', 'errors' => $messages), 412);
}
}

View File

@ -11,6 +11,8 @@ abstract class OAuth2ProtectedController extends JsonController {
protected $resource_server_context;
protected $repository;
public function __construct(IResourceServerContext $resource_server_context, ILogService $log_service)
{
parent::__construct($log_service);

View File

@ -0,0 +1,81 @@
<?php
/**
* Copyright 2015 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use oauth2\IResourceServerContext;
use utils\services\ILogService;
use models\marketplace\repositories\ICloudServiceRepository;
/**
* Class OAuth2CloudApiController
*/
abstract class OAuth2CloudApiController extends OAuth2CompanyServiceApiController {
/**
* query string params:
* page: You can specify further pages
* per_page: custom page size up to 100 ( min 10)
* status: cloud status ( active , not active, all)
* order_by: order by field
* order_dir: order direction
* @return mixed
*/
public function getClouds()
{
return $this->getCompanyServices();
}
/**
* @param $id
* @return mixed
*/
public function getCloud($id)
{
return $this->getCompanyService($id);
}
/**
* @param $id
* @return mixed
*/
public function getCloudDataCenters($id)
{
try{
$cloud = $this->repository->getById($id);
if(!$cloud)
return $this->error404();
$data_center_regions = $cloud->datacenters_regions();
$res = array();
foreach($data_center_regions as $region){
$data = $region->toArray();
$locations = $region->locations();
$data_locations = array();
foreach($locations as $loc){
array_push($data_locations, $loc->toArray());
}
$data['locations'] = $data_locations;
array_push($res, $data);
}
return $this->ok(array('datacenters' => $res ));
}
catch(Exception $ex){
$this->log_service->error($ex);
return $this->error500($ex);
}
}
}

View File

@ -0,0 +1,133 @@
<?php
/**
* Copyright 2015 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use models\marketplace\repositories\ICompanyServiceRepository;
use oauth2\IResourceServerContext;
use utils\services\ILogService;
/**
* Class OAuth2CompanyServiceApiController
*/
abstract class OAuth2CompanyServiceApiController extends OAuth2ProtectedController{
/**
* @var ICompanyServiceRepository
*/
protected $repository;
public function __construct (IResourceServerContext $resource_server_context, ILogService $log_service){
parent::__construct($resource_server_context,$log_service);
Validator::extend('status', function($attribute, $value, $parameters)
{
return $value == ICompanyServiceRepository::Status_All ||
$value == ICompanyServiceRepository::Status_non_active ||
$value == ICompanyServiceRepository::Status_active;
});
Validator::extend('order', function($attribute, $value, $parameters)
{
return $value == ICompanyServiceRepository::Order_date ||
$value == ICompanyServiceRepository::Order_name ;
});
Validator::extend('order_dir', function($attribute, $value, $parameters)
{
return $value == 'desc' ||
$value == 'asc';
});
}
/**
* query string params:
* page: You can specify further pages
* per_page: custom page size up to 100 ( min 10)
* status: cloud status ( active , not active, all)
* order_by: order by field
* order_dir: order direction
* @return mixed
*/
public function getCompanyServices()
{
try{
//default params
$page = 1;
$per_page = 10;
$status = ICompanyServiceRepository::Status_All;
$order_by = ICompanyServiceRepository::Order_date;
$order_dir = 'asc';
//validation of optional parameters
$values = Input::all();
$messages = array(
'status' => 'The :attribute field is does not has a valid value (all, active, non_active).',
'order' => 'The :attribute field is does not has a valid value (date, name).',
'order_dir' => 'The :attribute field is does not has a valid value (desc, asc).',
);
$rules = array(
'page' => 'integer|min:1',
'per_page' => 'required_with:page|integer|min:10|max:100',
'status' => 'status',
'order_by' => 'order',
'order_dir' => 'required_with:order_by|order_dir',
);
// Creates a Validator instance and validates the data.
$validation = Validator::make($values, $rules, $messages);
if ($validation->fails()) {
$messages = $validation->messages()->toArray();
return $this->error412($messages);
}
if(Input::has('page')){
$page = intval(Input::get('page'));
$per_page = intval(Input::get('per_page'));
}
if(Input::has('status')){
$status = Input::get('status');
}
if(Input::has('order_by')){
$order_by = Input::get('order_by');
$order_dir = Input::get('order_dir');
}
$data = $this->repository->getAll($page, $per_page, $status, $order_by, $order_dir);
return $this->ok($data);
}
catch(Exception $ex){
$this->log_service->error($ex);
return $this->error500($ex);
}
}
/**
* @param $id
* @return mixed
*/
public function getCompanyService($id)
{
try{
$data = $this->repository->getById($id);
return ($data)? $this->ok($data) : $this->error404();
}
catch(Exception $ex){
$this->log_service->error($ex);
return $this->error500($ex);
}
}
}

View File

@ -0,0 +1,82 @@
<?php
/**
* Copyright 2015 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use models\marketplace\repositories\IConsultantRepository;
use oauth2\IResourceServerContext;
use utils\services\ILogService;
/**
* Class OAuth2ConsultantsApiController
*/
class OAuth2ConsultantsApiController extends OAuth2CompanyServiceApiController {
/**
* @param IConsultantRepository $repository
* @param IResourceServerContext $resource_server_context
* @param ILogService $log_service
*/
public function __construct (IConsultantRepository $repository, IResourceServerContext $resource_server_context, ILogService $log_service){
$this->repository = $repository;
parent::__construct($resource_server_context,$log_service);
}
/**
* query string params:
* page: You can specify further pages
* per_page: custom page size up to 100 ( min 10)
* status: cloud status ( active , not active, all)
* order_by: order by field
* order_dir: order direction
* @return mixed
*/
public function getConsultants()
{
return $this->getCompanyServices();
}
/**
* @param $id
* @return mixed
*/
public function getConsultant($id)
{
return $this->getCompanyService($id);
}
/**
* @param $id
* @return mixed
*/
public function getOffices($id)
{
try{
$consultant = $this->repository->getById($id);
if(!$consultant)
return $this->error404();
$offices = $consultant->offices();
$res = array();
foreach($offices as $office){
array_push($res, $office->toArray());
}
return $this->ok(array('offices' => $res));
}
catch(Exception $ex){
$this->log_service->error($ex);
return $this->error500($ex);
}
}
}

View File

@ -0,0 +1,27 @@
<?php
/**
* Copyright 2014 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use models\marketplace\repositories\IPrivateCloudServiceRepository;
use oauth2\IResourceServerContext;
use utils\services\ILogService;
/**
* Class OAuth2PrivateCloudApiController
*/
final class OAuth2PrivateCloudApiController extends OAuth2CloudApiController {
public function __construct (IPrivateCloudServiceRepository $repository, IResourceServerContext $resource_server_context, ILogService $log_service){
parent::__construct($resource_server_context,$log_service);
$this->repository = $repository;
}
}

View File

@ -0,0 +1,29 @@
<?php
/**
* Copyright 2015 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use models\marketplace\repositories\IPublicCloudServiceRepository;
use oauth2\IResourceServerContext;
use utils\services\ILogService;
/**
* Class OAuth2PublicCloudApiController
*/
final class OAuth2PublicCloudApiController extends OAuth2CloudApiController {
public function __construct (IPublicCloudServiceRepository $repository, IResourceServerContext $resource_server_context, ILogService $log_service){
parent::__construct($resource_server_context,$log_service);
$this->repository = $repository;
}
}

View File

@ -0,0 +1,35 @@
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AlterTableOauth2ApiEndpoint extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('oauth2_api_endpoint', function($table)
{
$table->bigInteger("rate_limit")->unsigned()->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('oauth2_api_endpoint', function($table)
{
$table->dropColumn('rate_limit');
});
}
}

View File

@ -1,4 +1,8 @@
<?php
/**
* Class ApiEndpointSeeder
*/
class ApiEndpointSeeder extends Seeder
{
@ -8,6 +12,9 @@ class ApiEndpointSeeder extends Seeder
DB::table('oauth2_api_endpoint_api_scope')->delete();
DB::table('oauth2_api_endpoint')->delete();
$this->seedUsersEndpoints();
$this->seedPublicCloudsEndpoints();
$this->seedPrivateCloudsEndpoints();
$this->seedConsultantsEndpoints();
}
private function seedUsersEndpoints()
@ -24,8 +31,9 @@ class ApiEndpointSeeder extends Seeder
'http_method' => 'GET'
)
);
$profile_scope = ApiScope::where('name', '=', 'profile')->first();
$email_scope = ApiScope::where('name', '=', 'email')->first();
$email_scope = ApiScope::where('name', '=', 'email')->first();
$address_scope = ApiScope::where('name', '=', 'address')->first();
$get_user_info_endpoint = ApiEndpoint::where('name', '=', 'get-user-info')->first();
@ -34,5 +42,146 @@ class ApiEndpointSeeder extends Seeder
$get_user_info_endpoint->scopes()->attach($address_scope->id);
}
private function seedPublicCloudsEndpoints(){
$public_clouds = Api::where('name','=','public-clouds')->first();
$current_realm = Config::get('app.url');
// endpoints scopes
ApiEndpoint::create(
array(
'name' => 'get-public-clouds',
'active' => true,
'api_id' => $public_clouds->id,
'route' => '/api/v1/marketplace/public-clouds',
'http_method' => 'GET'
)
);
ApiEndpoint::create(
array(
'name' => 'get-public-cloud',
'active' => true,
'api_id' => $public_clouds->id,
'route' => '/api/v1/marketplace/public-clouds/{id}',
'http_method' => 'GET'
)
);
ApiEndpoint::create(
array(
'name' => 'get-public-cloud-datacenters',
'active' => true,
'api_id' => $public_clouds->id,
'route' => '/api/v1/marketplace/public-clouds/{id}/data-centers',
'http_method' => 'GET'
)
);
$public_cloud_read_scope = ApiScope::where('name','=',sprintf('%s/public-clouds/read',$current_realm))->first();
$endpoint_get_public_clouds = ApiEndpoint::where('name','=','get-public-clouds')->first();
$endpoint_get_public_clouds->scopes()->attach($public_cloud_read_scope->id);
$endpoint_get_public_cloud = ApiEndpoint::where('name','=','get-public-cloud')->first();
$endpoint_get_public_cloud->scopes()->attach($public_cloud_read_scope->id);
$endpoint_get_public_cloud_datacenters = ApiEndpoint::where('name','=','get-public-cloud-datacenters')->first();
$endpoint_get_public_cloud_datacenters->scopes()->attach($public_cloud_read_scope->id);
}
private function seedPrivateCloudsEndpoints(){
$private_clouds = Api::where('name','=','private-clouds')->first();
$current_realm = Config::get('app.url');
// endpoints scopes
ApiEndpoint::create(
array(
'name' => 'get-private-clouds',
'active' => true,
'api_id' => $private_clouds->id,
'route' => '/api/v1/marketplace/private-clouds',
'http_method' => 'GET'
)
);
ApiEndpoint::create(
array(
'name' => 'get-private-cloud',
'active' => true,
'api_id' => $private_clouds->id,
'route' => '/api/v1/marketplace/private-clouds/{id}',
'http_method' => 'GET'
)
);
ApiEndpoint::create(
array(
'name' => 'get-private-cloud-datacenters',
'active' => true,
'api_id' => $private_clouds->id,
'route' => '/api/v1/marketplace/private-clouds/{id}/data-centers',
'http_method' => 'GET'
)
);
$private_cloud_read_scope = ApiScope::where('name','=',sprintf('%s/private-clouds/read',$current_realm))->first();
$endpoint_get_private_clouds = ApiEndpoint::where('name','=','get-private-clouds')->first();
$endpoint_get_private_clouds->scopes()->attach($private_cloud_read_scope->id);
$endpoint_get_private_cloud = ApiEndpoint::where('name','=','get-private-cloud')->first();
$endpoint_get_private_cloud->scopes()->attach($private_cloud_read_scope->id);
$endpoint_get_private_cloud_datacenters = ApiEndpoint::where('name','=','get-private-cloud-datacenters')->first();
$endpoint_get_private_cloud_datacenters->scopes()->attach($private_cloud_read_scope->id);
}
private function seedConsultantsEndpoints(){
$consultants = Api::where('name','=','consultants')->first();
$current_realm = Config::get('app.url');
// endpoints scopes
ApiEndpoint::create(
array(
'name' => 'get-consultants',
'active' => true,
'api_id' => $consultants->id,
'route' => '/api/v1/marketplace/consultants',
'http_method' => 'GET'
)
);
ApiEndpoint::create(
array(
'name' => 'get-consultant',
'active' => true,
'api_id' => $consultants->id,
'route' => '/api/v1/marketplace/consultants/{id}',
'http_method' => 'GET'
)
);
ApiEndpoint::create(
array(
'name' => 'get-consultant-offices',
'active' => true,
'api_id' => $consultants->id,
'route' => '/api/v1/marketplace/consultants/{id}/offices',
'http_method' => 'GET'
)
);
$consultant_read_scope = ApiScope::where('name','=',sprintf('%s/consultants/read',$current_realm))->first();
$endpoint = ApiEndpoint::where('name','=','get-consultants')->first();
$endpoint->scopes()->attach($consultant_read_scope->id);
$endpoint = ApiEndpoint::where('name','=','get-consultant')->first();
$endpoint->scopes()->attach($consultant_read_scope->id);
$endpoint = ApiEndpoint::where('name','=','get-consultant-offices')->first();
$endpoint->scopes()->attach($consultant_read_scope->id);
}
}

View File

@ -1,5 +1,8 @@
<?php
/**
* Class ApiScopeSeeder
*/
class ApiScopeSeeder extends Seeder {
@ -9,6 +12,9 @@ class ApiScopeSeeder extends Seeder {
DB::table('oauth2_client_api_scope')->delete();
DB::table('oauth2_api_scope')->delete();
$this->seedUsersScopes();
$this->seedPublicCloudScopes();
$this->seedPrivateCloudScopes();
$this->seedConsultantScopes();
}
private function seedUsersScopes(){
@ -46,4 +52,54 @@ class ApiScopeSeeder extends Seeder {
);
}
private function seedPublicCloudScopes(){
$current_realm = Config::get('app.url');
$public_clouds = Api::where('name','=','public-clouds')->first();
ApiScope::create(
array(
'name' => sprintf('%s/public-clouds/read',$current_realm),
'short_description' => 'Get Public Clouds',
'description' => 'Grants read only access for Public Clouds',
'api_id' => $public_clouds->id,
'system' => false,
)
);
}
private function seedPrivateCloudScopes(){
$current_realm = Config::get('app.url');
$private_clouds = Api::where('name','=','private-clouds')->first();
ApiScope::create(
array(
'name' => sprintf('%s/private-clouds/read',$current_realm),
'short_description' => 'Get Private Clouds',
'description' => 'Grants read only access for Private Clouds',
'api_id' => $private_clouds->id,
'system' => false,
)
);
}
private function seedConsultantScopes(){
$current_realm = Config::get('app.url');
$consultants = Api::where('name','=','consultants')->first();
ApiScope::create(
array(
'name' => sprintf('%s/consultants/read',$current_realm),
'short_description' => 'Get Consultants',
'description' => 'Grants read only access for Consultants',
'api_id' => $consultants->id,
'system' => false,
)
);
}
}

View File

@ -1,6 +1,10 @@
<?php
/**
* Class ApiSeeder
*/
class ApiSeeder extends Seeder {
public function run()
{
DB::table('oauth2_api_endpoint_api_scope')->delete();
@ -10,6 +14,7 @@ class ApiSeeder extends Seeder {
$resource_server = ResourceServer::first();
// users
Api::create(
array(
'name' => 'users',
@ -20,5 +25,38 @@ class ApiSeeder extends Seeder {
'logo' => asset('img/apis/server.png')
)
);
// public clouds
Api::create(
array(
'name' => 'public-clouds',
'logo' => null,
'active' => true,
'Description' => 'Marketplace Public Clouds',
'resource_server_id' => $resource_server->id,
'logo' => asset('img/apis/server.png')
)
);
// private clouds
Api::create(
array(
'name' => 'private-clouds',
'logo' => null,
'active' => true,
'Description' => 'Marketplace Private Clouds',
'resource_server_id' => $resource_server->id,
'logo' => asset('img/apis/server.png')
)
);
// consultants
Api::create(
array(
'name' => 'consultants',
'logo' => null,
'active' => true,
'Description' => 'Marketplace Consultants',
'resource_server_id' => $resource_server->id,
'logo' => asset('img/apis/server.png')
)
);
}
}

View File

@ -1,5 +1,8 @@
<?php
/**
* Class DatabaseSeeder
*/
class DatabaseSeeder extends Seeder {
/**

View File

@ -56,13 +56,19 @@ class TestSeeder extends Seeder {
$this->seedApiEndpointScopes();
$this->seedApiScopeScopes();
$this->seedUsersScopes();
$this->seedPublicCloudScopes();
$this->seedPrivateCloudScopes();
$this->seedConsultantScopes();
//endpoints
$this->seedResourceServerEndpoints();
$this->seedApiEndpoints();
$this->seedApiEndpointEndpoints();
$this->seedScopeEndpoints();
$this->seedUsersEndpoints();
$this->seedPublicCloudsEndpoints();
$this->seedPrivateCloudsEndpoints();
$this->seedConsultantsEndpoints();
//clients
$this->seedTestUsersAndClients();
}
@ -474,6 +480,40 @@ class TestSeeder extends Seeder {
'logo' => asset('img/apis/server.png')
)
);
Api::create(
array(
'name' => 'public-clouds',
'logo' => null,
'active' => true,
'Description' => 'Marketplace Public Clouds',
'resource_server_id' => $resource_server->id,
'logo' => asset('img/apis/server.png')
)
);
Api::create(
array(
'name' => 'private-clouds',
'logo' => null,
'active' => true,
'Description' => 'Marketplace Private Clouds',
'resource_server_id' => $resource_server->id,
'logo' => asset('img/apis/server.png')
)
);
Api::create(
array(
'name' => 'consultants',
'logo' => null,
'active' => true,
'Description' => 'Marketplace Consultants',
'resource_server_id' => $resource_server->id,
'logo' => asset('img/apis/server.png')
)
);
}
private function seedResourceServerScopes(){
@ -708,6 +748,7 @@ class TestSeeder extends Seeder {
}
private function seedApiScopeScopes(){
$current_realm = Config::get('app.url');
$api_scope = Api::where('name','=','api-scope')->first();
@ -806,7 +847,55 @@ class TestSeeder extends Seeder {
'system' => false,
)
);
}
private function seedPublicCloudScopes(){
$current_realm = Config::get('app.url');
$public_clouds = Api::where('name','=','public-clouds')->first();
ApiScope::create(
array(
'name' => sprintf('%s/public-clouds/read',$current_realm),
'short_description' => 'Get Public Clouds',
'description' => 'Get Public Clouds',
'api_id' => $public_clouds->id,
'system' => false,
)
);
}
private function seedPrivateCloudScopes(){
$current_realm = Config::get('app.url');
$private_clouds = Api::where('name','=','private-clouds')->first();
ApiScope::create(
array(
'name' => sprintf('%s/private-clouds/read',$current_realm),
'short_description' => 'Get Private Clouds',
'description' => 'Get Private Clouds',
'api_id' => $private_clouds->id,
'system' => false,
)
);
}
private function seedConsultantScopes(){
$current_realm = Config::get('app.url');
$consultants = Api::where('name','=','consultants')->first();
ApiScope::create(
array(
'name' => sprintf('%s/consultants/read',$current_realm),
'short_description' => 'Get Consultants',
'description' => 'Get Consultants',
'api_id' => $consultants->id,
'system' => false,
)
);
}
private function seedResourceServerEndpoints(){
@ -1273,4 +1362,147 @@ class TestSeeder extends Seeder {
$get_user_info_endpoint->scopes()->attach($email_scope->id);
$get_user_info_endpoint->scopes()->attach($address_scope->id);
}
private function seedPublicCloudsEndpoints(){
$public_clouds = Api::where('name','=','public-clouds')->first();
$current_realm = Config::get('app.url');
// endpoints scopes
ApiEndpoint::create(
array(
'name' => 'get-public-clouds',
'active' => true,
'api_id' => $public_clouds->id,
'route' => '/api/v1/marketplace/public-clouds',
'http_method' => 'GET'
)
);
ApiEndpoint::create(
array(
'name' => 'get-public-cloud',
'active' => true,
'api_id' => $public_clouds->id,
'route' => '/api/v1/marketplace/public-clouds/{id}',
'http_method' => 'GET'
)
);
ApiEndpoint::create(
array(
'name' => 'get-public-cloud-datacenters',
'active' => true,
'api_id' => $public_clouds->id,
'route' => '/api/v1/marketplace/public-clouds/{id}/data-centers',
'http_method' => 'GET'
)
);
$public_cloud_read_scope = ApiScope::where('name','=',sprintf('%s/public-clouds/read',$current_realm))->first();
$endpoint_get_public_clouds = ApiEndpoint::where('name','=','get-public-clouds')->first();
$endpoint_get_public_clouds->scopes()->attach($public_cloud_read_scope->id);
$endpoint_get_public_cloud = ApiEndpoint::where('name','=','get-public-cloud')->first();
$endpoint_get_public_cloud->scopes()->attach($public_cloud_read_scope->id);
$endpoint_get_public_cloud_datacenters = ApiEndpoint::where('name','=','get-public-cloud-datacenters')->first();
$endpoint_get_public_cloud_datacenters->scopes()->attach($public_cloud_read_scope->id);
}
private function seedPrivateCloudsEndpoints(){
$private_clouds = Api::where('name','=','private-clouds')->first();
$current_realm = Config::get('app.url');
// endpoints scopes
ApiEndpoint::create(
array(
'name' => 'get-private-clouds',
'active' => true,
'api_id' => $private_clouds->id,
'route' => '/api/v1/marketplace/private-clouds',
'http_method' => 'GET'
)
);
ApiEndpoint::create(
array(
'name' => 'get-private-cloud',
'active' => true,
'api_id' => $private_clouds->id,
'route' => '/api/v1/marketplace/private-clouds/{id}',
'http_method' => 'GET'
)
);
ApiEndpoint::create(
array(
'name' => 'get-private-cloud-datacenters',
'active' => true,
'api_id' => $private_clouds->id,
'route' => '/api/v1/marketplace/private-clouds/{id}/data-centers',
'http_method' => 'GET'
)
);
$private_cloud_read_scope = ApiScope::where('name','=',sprintf('%s/private-clouds/read',$current_realm))->first();
$endpoint_get_private_clouds = ApiEndpoint::where('name','=','get-private-clouds')->first();
$endpoint_get_private_clouds->scopes()->attach($private_cloud_read_scope->id);
$endpoint_get_private_cloud = ApiEndpoint::where('name','=','get-private-cloud')->first();
$endpoint_get_private_cloud->scopes()->attach($private_cloud_read_scope->id);
$endpoint_get_private_cloud_datacenters = ApiEndpoint::where('name','=','get-private-cloud-datacenters')->first();
$endpoint_get_private_cloud_datacenters->scopes()->attach($private_cloud_read_scope->id);
}
private function seedConsultantsEndpoints(){
$consultants = Api::where('name','=','consultants')->first();
$current_realm = Config::get('app.url');
// endpoints scopes
ApiEndpoint::create(
array(
'name' => 'get-consultants',
'active' => true,
'api_id' => $consultants->id,
'route' => '/api/v1/marketplace/consultants',
'http_method' => 'GET'
)
);
ApiEndpoint::create(
array(
'name' => 'get-consultant',
'active' => true,
'api_id' => $consultants->id,
'route' => '/api/v1/marketplace/consultants/{id}',
'http_method' => 'GET'
)
);
ApiEndpoint::create(
array(
'name' => 'get-consultant-offices',
'active' => true,
'api_id' => $consultants->id,
'route' => '/api/v1/marketplace/consultants/{id}/offices',
'http_method' => 'GET'
)
);
$consultant_read_scope = ApiScope::where('name','=',sprintf('%s/consultants/read',$current_realm))->first();
$endpoint = ApiEndpoint::where('name','=','get-consultants')->first();
$endpoint->scopes()->attach($consultant_read_scope->id);
$endpoint = ApiEndpoint::where('name','=','get-consultant')->first();
$endpoint->scopes()->attach($consultant_read_scope->id);
$endpoint = ApiEndpoint::where('name','=','get-consultant-offices')->first();
$endpoint->scopes()->attach($consultant_read_scope->id);
}
}

View File

@ -216,6 +216,12 @@ Route::filter('is.current.user',function($route, $request){
Route::filter('oauth2.protected.endpoint','OAuth2BearerAccessTokenRequestValidator');
Route::filter('oauth2.rate.limiter','ApiEndpointRateLimiter');
Route::filter('oauth2.rate.limiter.headers','ApiEndpointRateLimiterHeaders');
Route::filter('oauth2.etag','ETagChecker');
//oauth2 server admin filter
Route::filter('oauth2.server.admin.json',function(){

View File

@ -0,0 +1,84 @@
<?php
/**
* Copyright 2015 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use oauth2\services\IApiEndpointService;
use utils\services\ILogService;
use utils\services\ICheckPointService;
use utils\services\ICacheService;
/**
* Class ApiEndpointRateLimiter
*/
class ApiEndpointRateLimiter {
/**
* @var IApiEndpointService
*/
private $api_endpoint_service;
/**
* @var ILogService
*/
private $log_service;
/**
* @var ICheckPointService
*/
private $checkpoint_service;
/**
* @var ICacheService
*/
private $cache_service;
/**
* @param IApiEndpointService $api_endpoint_service
* @param ILogService $log_service
* @param ICheckPointService $checkpoint_service
* @param ICacheService $cache_service
*/
public function __construct(IApiEndpointService $api_endpoint_service, ILogService $log_service, ICheckPointService $checkpoint_service, ICacheService $cache_service){
$this->api_endpoint_service = $api_endpoint_service;
$this->log_service = $log_service;
$this->checkpoint_service = $checkpoint_service;
$this->cache_service = $cache_service;
}
/**
* @param $route
* @param $request
* @return mixed
*/
public function filter($route, $request)
{
$url = $route->getPath();
if(strpos($url, '/') != 0){
$url = '/'.$url;
}
$method = $request->getMethod();
try {
$endpoint = $this->api_endpoint_service->getApiEndpointByUrlAndMethod($url, $method);
if(!is_null($endpoint->rate_limit) && (int)$endpoint->rate_limit > 0){
//do rate limit checking
$key = sprintf('rate.limit.%s_%s_%s',$url,$method,$request->getClientIp());
$res = (int)$this->cache_service->getSingleValue($key);
if($res >= (int)$endpoint->rate_limit)
return Response::json(array('message' => "You have triggered an abuse detection mechanism and have been temporarily blocked from content creation. Please retry your request again later."), 403);
$this->cache_service->incCounter($key, (3600 * 60));
}
}
catch(Exception $ex){
$this->log_service->error($ex);
$this->checkpoint_service->trackException($ex);
}
}
}

View File

@ -0,0 +1,87 @@
<?php
/**
* Copyright 2015 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use oauth2\services\IApiEndpointService;
use utils\services\ILogService;
use utils\services\ICheckPointService;
use utils\services\ICacheService;
/**
* Class ApiEndpointRateLimiterHeaders
*/
class ApiEndpointRateLimiterHeaders {
/**
* @var IApiEndpointService
*/
private $api_endpoint_service;
/**
* @var ILogService
*/
private $log_service;
/**
* @var ICheckPointService
*/
private $checkpoint_service;
/**
* @var ICacheService
*/
private $cache_service;
/**
* @param IApiEndpointService $api_endpoint_service
* @param ILogService $log_service
* @param ICheckPointService $checkpoint_service
* @param ICacheService $cache_service
*/
public function __construct(IApiEndpointService $api_endpoint_service, ILogService $log_service, ICheckPointService $checkpoint_service, ICacheService $cache_service){
$this->api_endpoint_service = $api_endpoint_service;
$this->log_service = $log_service;
$this->checkpoint_service = $checkpoint_service;
$this->cache_service = $cache_service;
}
/**
* @param $route
* @param $request
* @param $response
*/
public function filter($route, $request, $response)
{
$url = $route->getPath();
if(strpos($url, '/') != 0){
$url = '/'.$url;
}
$method = $request->getMethod();
try {
$endpoint = $this->api_endpoint_service->getApiEndpointByUrlAndMethod($url, $method);
if(!is_null($endpoint->rate_limit) && (int)$endpoint->rate_limit > 0){
//do rate limit checking
$key = sprintf('rate.limit.%s_%s_%s',$url,$method,$request->getClientIp());
$res = (int)$this->cache_service->getSingleValue($key);
if($res <= (int)$endpoint->rate_limit)
{
$response->headers->set('X-Ratelimit-Limit', $endpoint->rate_limit, false);
$response->headers->set('X-Ratelimit-Remaining', $endpoint->rate_limit-(int)$res, false);
$response->headers->set('X-RateLimit-Reset', $this->cache_service->ttl(($key)) , false);
}
}
}
catch(Exception $ex){
$this->log_service->error($ex);
$this->checkpoint_service->trackException($ex);
}
}
}

View File

@ -0,0 +1,36 @@
<?php
/**
* Copyright 2015 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
/**
* Class ETagChecker
*/
class ETagChecker {
/**
* @param $route
* @param $request
* @param $response
*/
public function filter($route, $request, $response)
{
if($response->getStatusCode()!= 200) return;
$etag = md5($response->getContent());
$requestETag = str_replace('"', '', $request->getETags());
if($requestETag && $requestETag[0] == $etag){
$response->setNotModified();
}
$response->setEtag($etag);
}
}

View File

@ -48,9 +48,10 @@ interface IApiEndpointService {
* @param string $route
* @param string $http_method
* @param int $api_id
* @param int $rate_limit
* @return IApiEndpoint
*/
public function add($name, $description, $active, $allow_cors, $route, $http_method, $api_id);
public function add($name, $description, $active, $allow_cors, $route, $http_method, $api_id, $rate_limit);
/**

View File

@ -2,13 +2,14 @@
namespace utils\model;
use Eloquent;
use ReflectionClass;
/**
* Class BaseModelEloquent
* @package utils\model
*/
abstract class BaseModelEloquent extends Eloquent {
private $class = null;
/**
* @param $query
* @param array $filters
@ -20,4 +21,41 @@ abstract class BaseModelEloquent extends Eloquent {
}
return $query;
}
public function __construct($attributes = array())
{
parent::__construct($attributes);
$this->class = new ReflectionClass(get_class($this));
if ($this->useSti()) {
$this->setAttribute($this->stiClassField, $this->class->getName());
}
}
private function useSti() {
return ($this->stiClassField && $this->stiBaseClass);
}
public function newQuery($excludeDeleted = true)
{
$builder = parent::newQuery($excludeDeleted);
// If I am using STI, and I am not the base class,
// then filter on the class name.
if ($this->useSti() && get_class(new $this->stiBaseClass) !== get_class($this)) {
$builder->where($this->stiClassField, "=", $this->class->getShortName());
}
return $builder;
}
public function newFromBuilder($attributes = array())
{
if ($this->useSti() && $attributes->{$this->stiClassField}) {
$class = $this->class->getName();
$instance = new $class;
$instance->exists = true;
$instance->setRawAttributes((array) $attributes, true);
return $instance;
} else {
return parent::newFromBuilder($attributes);
}
}
}

View File

@ -88,4 +88,10 @@ interface ICacheService {
public function setKeyExpiration($key, $ttl);
public function boot();
/**Returns the remaining time to live of a key that has a timeout.
* @param string $key
* @return int
*/
public function ttl($key);
}

View File

@ -9,6 +9,7 @@ use Exception;
* @package utils\services
*/
interface ILogService {
public function error(Exception $exception);
public function warning(Exception $exception);

View File

@ -0,0 +1,24 @@
<?php
/**
* Copyright 2015 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
/**
* Interface IBaseRepository
*/
interface IBaseRepository {
/**
* @param int $id
* @return IEntity
*/
public function getById($id);
}

23
app/models/IEntity.php Normal file
View File

@ -0,0 +1,23 @@
<?php
/**
* Copyright 2015 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
/**
* Interface IEntity
*/
interface IEntity {
/**
* @return int
*/
public function getIdentifier();
}

View File

@ -0,0 +1,40 @@
<?php
/**
* Copyright 2015 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
namespace models\marketplace;
use utils\model\BaseModelEloquent;
/**
* Class CompanyService
* @package model\marketplace
*/
class CompanyService extends BaseModelEloquent implements \IEntity{
protected $hidden = array('ClassName', 'MarketPlaceTypeID', 'EditedByID');
protected $table = 'CompanyService';
protected $connection = 'os_members';
protected $stiClassField = 'ClassName';
protected $stiBaseClass = 'models\marketplace\CompanyService';
/**
* @return int
*/
public function getIdentifier()
{
return (int)$this->ID;
}
}

View File

@ -0,0 +1,29 @@
<?php
/**
* Copyright 2015 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
namespace models\marketplace;
/**
* Class Consultant
* @package models\marketplace
*/
class Consultant extends CompanyService implements IConsultant {
/**
* @return Office[]
*/
public function offices(){
return $this->hasMany('models\marketplace\Office', 'ConsultantID', 'ID')->get();
}
}

View File

@ -0,0 +1,36 @@
<?php
/**
* Copyright 2015 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
namespace models\marketplace;
use utils\model\BaseModelEloquent;
/**
* Class DataCenterLocation
* @package model\marketplace
*/
class DataCenterLocation extends BaseModelEloquent {
protected $table = 'DataCenterLocation';
protected $connection = 'os_members';
protected $hidden = array('ClassName','CloudServiceID','DataCenterRegionID');
/**
* @return DataCenterRegion
*/
public function region(){
return $this->belongsTo('models\marketplace\DataCenterRegion', 'DataCenterRegionID');
}
}

View File

@ -0,0 +1,35 @@
<?php
/**
* Copyright 2015 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
namespace models\marketplace;
use utils\model\BaseModelEloquent;
/**
* Class DataCenterRegion
* @package model\marketplace
*/
class DataCenterRegion extends BaseModelEloquent {
protected $table = 'DataCenterRegion';
protected $connection = 'os_members';
protected $hidden = array('ClassName','CloudServiceID','PublicCloudID');
/**
* @return DataCenterLocation[]
*/
public function locations(){
return $this->hasMany('models\marketplace\DataCenterLocation','DataCenterRegionID','ID')->get();
}
}

View File

@ -0,0 +1,27 @@
<?php
/**
* Copyright 2015 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
namespace models\marketplace;
/**
* Interface ICloudService
* @package models\marketplace
*/
interface ICloudService {
/**
* @return DataCenterRegion[]
*/
public function datacenters_regions();
}

View File

@ -0,0 +1,26 @@
<?php
/**
* Copyright 2015 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
namespace models\marketplace;
/**
* Interface IConsultant
* @package models\marketplace
*/
interface IConsultant {
/**
* @return Office[]
*/
public function offices();
}

View File

@ -0,0 +1,38 @@
<?php
/**
* Copyright 2015 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
namespace models\marketplace;
use utils\model\BaseModelEloquent;
/**
* Class Office
* @package models\marketplace
*/
class Office extends BaseModelEloquent {
protected $table = 'Office';
protected $connection = 'os_members';
protected $hidden = array('ClassName','Order','ConsultantID');
/**
* @return Consultant
*/
public function region(){
return $this->belongsTo('models\marketplace\Consultant', 'ConsultantID');
}
}

View File

@ -0,0 +1,32 @@
<?php
/**
* Copyright 2015 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
namespace models\marketplace;
/**
* Class PrivateCloudService
* @package models\marketplace
*/
class PrivateCloudService extends CompanyService implements ICloudService
{
/**
* @return DataCenterRegion[]
*/
public function datacenters_regions()
{
return $this->hasMany('models\marketplace\DataCenterRegion', 'CloudServiceID', 'ID')->get();
}
}

View File

@ -0,0 +1,29 @@
<?php
/*
* Copyright 2015 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
namespace models\marketplace;
/**
* Class PublicCloudService
* @package model\marketplace
*/
class PublicCloudService extends CompanyService implements ICloudService {
/**
* @return DataCenterRegion[]
*/
public function datacenters_regions()
{
return $this->hasMany('models\marketplace\DataCenterRegion','CloudServiceID', 'ID')->get();
}
}

View File

@ -0,0 +1,25 @@
<?php
/**
* Copyright 2015 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
namespace models\marketplace\repositories;
use models\marketplace\ICloudService;
/**
* Interface ICloudServiceRepository
* @package models\marketplace\repositories
*/
interface ICloudServiceRepository extends ICompanyServiceRepository {
}

View File

@ -0,0 +1,36 @@
<?php
/**
* Copyright 2015 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
namespace models\marketplace\repositories;
use IBaseRepository;
interface ICompanyServiceRepository extends IBaseRepository {
const Status_All = 'all';
const Status_active = 'active';
const Status_non_active = 'non_active';
const Order_date = 'date';
const Order_name = 'name';
/**
* @param int $page
* @param int $per_page
* @param string $status
* @param string $order_by
* @param string $order_dir
* @return \IEntity[]
*/
public function getAll($page = 1, $per_page = 1000, $status = ICompanyServiceRepository::Status_All, $order_by = ICompanyServiceRepository::Order_date, $order_dir = 'asc');
}

View File

@ -0,0 +1,22 @@
<?php
/**
* Copyright 2015 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
namespace models\marketplace\repositories;
/**
* Interface IConsultantRepository
* @package models\marketplace\repositories
*/
interface IConsultantRepository extends ICompanyServiceRepository{
}

View File

@ -0,0 +1,22 @@
<?php
/**
* Copyright 2015 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
namespace models\marketplace\repositories;
/**
* Interface IPrivateCloudServiceRepository
* @package models\marketplace\repositories
*/
interface IPrivateCloudServiceRepository extends ICloudServiceRepository {
}

View File

@ -0,0 +1,22 @@
<?php
/**
* Copyright 2015 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
namespace models\marketplace\repositories;
/**
* Interface IPublicCloudServiceRepository
* @package models\marketplace\repositories
*/
interface IPublicCloudServiceRepository extends ICloudServiceRepository {
}

View File

@ -7,7 +7,7 @@ class ApiEndpoint extends BaseModelEloquent implements IApiEndpoint{
protected $table = 'oauth2_api_endpoint';
protected $fillable = array( 'description','active','allow_cors', 'name','route', 'http_method', 'api_id');
protected $fillable = array( 'description','active','allow_cors', 'name','route', 'http_method', 'api_id', 'rate_limit');
public function getActiveAttribute(){
return (bool) $this->attributes['active'];

View File

@ -21,5 +21,8 @@ class RepositoriesProvider extends ServiceProvider
App::singleton('openid\repositories\IOpenIdTrustedSiteRepository', 'repositories\EloquentOpenIdTrustedSiteRepository');
App::singleton('auth\IUserRepository', 'repositories\EloquentUserRepository');
App::singleton('auth\IMemberRepository', 'repositories\EloquentMemberRepository');
App::singleton('models\marketplace\repositories\IPublicCloudServiceRepository', 'repositories\marketplace\EloquentPublicCloudServiceRepository');
App::singleton('models\marketplace\repositories\IPrivateCloudServiceRepository', 'repositories\marketplace\EloquentPrivateCloudServiceRepository');
App::singleton('models\marketplace\repositories\IConsultantRepository', 'repositories\marketplace\EloquentConsultantRepository');
}
}

View File

@ -0,0 +1,96 @@
<?php
/**
* Copyright 2015 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
namespace repositories\marketplace;
use IEntity;
use models\marketplace\repositories\ICompanyServiceRepository;
use utils\services\ILogService;
use DB;
/**
* Class EloquentCompanyServiceRepository
* @package repositories\marketplace
*/
abstract class EloquentCompanyServiceRepository implements ICompanyServiceRepository{
/**
* @var IEntity
*/
protected $entity;
/**
* @var ILogService
*/
protected $log_service;
/**
* @param int $id
* @return IEntity
*/
public function getById($id)
{
return $this->entity->find($id);
}
/**
* @param int $page
* @param int $per_page
* @param string $status
* @param string $order_by
* @param string $order_dir
* @return \IEntity[]
*/
public function getAll($page = 1, $per_page = 1000, $status = ICompanyServiceRepository::Status_All, $order_by = ICompanyServiceRepository::Order_date, $order_dir = 'asc')
{
$fields = array('*');
$filters = array();
switch($status){
case ICompanyServiceRepository::Status_active:
array_push($filters,
array(
'name'=>'Active',
'op' => '=',
'value'=> true
)
);
break;
case ICompanyServiceRepository::Status_non_active:
array_push($filters,
array(
'name'=>'Active',
'op' => '=',
'value'=> false
)
);
break;
}
DB::getPaginator()->setCurrentPage($page);
$query = $this->entity->Filter($filters);
switch($order_by){
case ICompanyServiceRepository::Order_date:
$query = $query->orderBy('Created', $order_dir);
break;
case ICompanyServiceRepository::Order_name:
$query = $query->orderBy('Name', $order_dir);
break;
}
return $query->paginate($per_page, $fields)->toArray();
}
}

View File

@ -0,0 +1,36 @@
<?php
/**
* Copyright 2015 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
namespace repositories\marketplace;
use models\marketplace\Consultant;
use models\marketplace\IConsultant;
use models\marketplace\repositories\IConsultantRepository;
use utils\services\ILogService;
/**
* Class EloquentConsultantRepository
* @package repositories\marketplace
*/
class EloquentConsultantRepository extends EloquentCompanyServiceRepository implements IConsultantRepository {
/**
* @param Consultant $consultant
* @param ILogService $log_service
*/
public function __construct(Consultant $consultant, ILogService $log_service){
$this->entity = $consultant;
$this->log_service = $log_service;
}
}

View File

@ -0,0 +1,36 @@
<?php
/**
* Copyright 2014 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
namespace repositories\marketplace;
use models\marketplace\PrivateCloudService;
use models\marketplace\repositories\IPrivateCloudServiceRepository;
use utils\services\ILogService;
/**
* Class EloquentPrivateCloudServiceRepository
* @package repositories\marketplace
*/
class EloquentPrivateCloudServiceRepository extends EloquentCompanyServiceRepository
implements IPrivateCloudServiceRepository {
/**
* @param PrivateCloudService $private_cloud
* @param ILogService $log_service
*/
public function __construct(PrivateCloudService $private_cloud, ILogService $log_service){
$this->entity = $private_cloud;
$this->log_service = $log_service;
}
}

View File

@ -0,0 +1,41 @@
<?php
/**
* Copyright 2015 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
namespace repositories\marketplace;
use models\marketplace\PublicCloudService;
use utils\services\ILogService;
use models\marketplace\repositories\ICloudServiceRepository;
use models\marketplace\repositories\IPublicCloudServiceRepository;
use models\marketplace\ICloudService;
use repositories\marketplace\EloquentCloudServiceRepository;
use DB;
/**
* Class EloquentPublicCloudServiceRepository
* @package repositories\marketplace
*/
class EloquentPublicCloudServiceRepository extends EloquentCompanyServiceRepository
implements IPublicCloudServiceRepository {
/**
* @param PublicCloudService $public_cloud
* @param ILogService $log_service
*/
public function __construct(PublicCloudService $public_cloud, ILogService $log_service){
$this->entity = $public_cloud;
$this->log_service = $log_service;
}
}

View File

@ -180,9 +180,33 @@ Route::group(array('prefix' => 'admin/api/v1', 'before' => 'ssl|auth'), function
});
//OAuth2 Protected API
Route::group(array('prefix' => 'api/v1', 'before' => 'ssl|oauth2.enabled|oauth2.cors.before|oauth2.protected.endpoint'), function()
Route::group(array('prefix' => 'api/v1',
'before' => 'ssl|oauth2.enabled|oauth2.rate.limiter|oauth2.cors.before|oauth2.protected.endpoint',
'after' => 'oauth2.rate.limiter.headers|oauth2.etag'), function()
{
Route::group(array('prefix' => 'users'), function(){
Route::get('/me','OAuth2UserApiController@me');
});
Route::group(array('prefix' => 'marketplace'), function(){
Route::group(array('prefix' => 'public-clouds'), function(){
Route::get('','OAuth2PublicCloudApiController@getClouds');
Route::get('/{id}','OAuth2PublicCloudApiController@getCloud');
Route::get('/{id}/data-centers','OAuth2PublicCloudApiController@getCloudDataCenters');
});
Route::group(array('prefix' => 'private-clouds'), function(){
Route::get('','OAuth2PrivateCloudApiController@getClouds');
Route::get('/{id}','OAuth2PrivateCloudApiController@getCloud');
Route::get('/{id}/data-centers','OAuth2PrivateCloudApiController@getCloudDataCenters');
});
Route::group(array('prefix' => 'consultants'), function(){
Route::get('','OAuth2ConsultantsApiController@getConsultants');
Route::get('/{id}','OAuth2ConsultantsApiController@getConsultant');
Route::get('/{id}/offices','OAuth2ConsultantsApiController@getOffices');
});
});
});

View File

@ -74,13 +74,14 @@ class ApiEndpointService implements IApiEndpointService {
* @param string $route
* @param string $http_method
* @param integer $api_id
* @param integer $rate_limit
* @return IApiEndpoint
*/
public function add($name, $description, $active,$allow_cors, $route, $http_method, $api_id)
public function add($name, $description, $active,$allow_cors, $route, $http_method, $api_id, $rate_limit)
{
$instance = null;
$this->tx_service->transaction(function () use ($name, $description, $active,$allow_cors, $route, $http_method, $api_id, &$instance) {
$this->tx_service->transaction(function () use ($name, $description, $active,$allow_cors, $route, $http_method, $api_id, $rate_limit, &$instance) {
//check that does not exists an endpoint with same http method and same route
if(ApiEndpoint::where('http_method','=',$http_method)->where('route','=',$route)->count()>0)
@ -94,7 +95,8 @@ class ApiEndpointService implements IApiEndpointService {
'route' => $route,
'http_method' => $http_method,
'api_id' => $api_id,
'allow_cors' => $allow_cors
'allow_cors' => $allow_cors,
'rate_limit' => (int)$rate_limit,
)
);
$instance->Save();
@ -118,7 +120,7 @@ class ApiEndpointService implements IApiEndpointService {
if(is_null($endpoint))
throw new InvalidApiEndpoint(sprintf('api endpoint id %s does not exists!',$id));
$allowed_update_params = array('name','description','active','route','http_method','allow_cors');
$allowed_update_params = array('name','description','active','route','http_method','allow_cors', 'rate_limit');
foreach($allowed_update_params as $param){
if(array_key_exists($param,$params)){
$endpoint->{$param} = $params[$param];

View File

@ -130,4 +130,13 @@ class RedisCacheService implements ICacheService {
public function setKeyExpiration($key, $ttl){
$this->redis->expire($key, intval($ttl));
}
/**Returns the remaining time to live of a key that has a timeout.
* @param string $key
* @return int
*/
public function ttl($key)
{
return (int)$this->redis->ttl($key);
}
}

View File

@ -0,0 +1,112 @@
<?php
/**
* Copyright 2014 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use models\marketplace\repositories\ICompanyServiceRepository;
/**
* Class OAuth2ConsultantApiTest
*/
class OAuth2ConsultantApiTest extends OAuth2ProtectedApiTest {
protected function getScopes()
{
$scope = array(
sprintf('%s/consultants/read',$this->current_realm)
);
return $scope;
}
public function testGetConsultants(){
$params = array(
'page' => 1 ,
'per_page' => 10,
'status' => ICompanyServiceRepository::Status_active,
);
$headers = array("HTTP_Authorization" => " Bearer " .$this->access_token);
$response = $this->action("GET", "OAuth2ConsultantsApiController@getConsultants",
$params,
array(),
array(),
$headers);
$content = $response->getContent();
$consultants = json_decode($content);
$this->assertResponseStatus(200);
}
public function testGetConsultantNotFound(){
$params = array(
'id' => 0
);
$headers = array("HTTP_Authorization" => " Bearer " .$this->access_token);
$response = $this->action("GET", "OAuth2ConsultantsApiController@getConsultant",
$params,
array(),
array(),
$headers);
$content = $response->getContent();
$res = json_decode($content);
$this->assertResponseStatus(404);
}
public function testGetConsultantFound(){
$params = array(
'id' => 18
);
$headers = array("HTTP_Authorization" => " Bearer " .$this->access_token);
$response = $this->action("GET", "OAuth2ConsultantsApiController@getConsultant",
$params,
array(),
array(),
$headers);
$content = $response->getContent();
$res = json_decode($content);
$this->assertResponseStatus(200);
}
public function testGetOffices(){
$params = array(
'id' => 19
);
$headers = array("HTTP_Authorization" => " Bearer " .$this->access_token);
$response = $this->action("GET", "OAuth2ConsultantsApiController@getOffices",
$params,
array(),
array(),
$headers);
$content = $response->getContent();
$res = json_decode($content);
$this->assertResponseStatus(200);
}
}

View File

@ -0,0 +1,113 @@
<?php
/**
* Copyright 2015 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use models\marketplace\repositories\ICompanyServiceRepository;
/**
* Class OAuth2PrivateCloudApiTest
*/
class OAuth2PrivateCloudApiTest extends OAuth2ProtectedApiTest {
protected function getScopes()
{
$scope = array(
sprintf('%s/private-clouds/read',$this->current_realm)
);
return $scope;
}
public function testGetPrivateClouds(){
$params = array(
'page' => 1 ,
'per_page' => 10,
'status' => ICompanyServiceRepository::Status_active,
);
$headers = array("HTTP_Authorization" => " Bearer " .$this->access_token);
$response = $this->action("GET", "OAuth2PrivateCloudApiController@getClouds",
$params,
array(),
array(),
$headers);
$content = $response->getContent();
$clouds = json_decode($content);
$this->assertResponseStatus(200);
}
public function testGetPrivateCloudNotFound(){
$params = array(
'id' => 0
);
$headers = array("HTTP_Authorization" => " Bearer " .$this->access_token);
$response = $this->action("GET", "OAuth2PrivateCloudApiController@getCloud",
$params,
array(),
array(),
$headers);
$content = $response->getContent();
$res = json_decode($content);
$this->assertResponseStatus(404);
}
public function testGetPrivateCloudFound(){
$params = array(
'id' => 60
);
$headers = array("HTTP_Authorization" => " Bearer " .$this->access_token);
$response = $this->action("GET", "OAuth2PrivateCloudApiController@getCloud",
$params,
array(),
array(),
$headers);
$content = $response->getContent();
$res = json_decode($content);
$this->assertResponseStatus(200);
}
public function testGetDataCenterRegions(){
$params = array(
'id' => 60
);
$headers = array("HTTP_Authorization" => " Bearer " .$this->access_token);
$response = $this->action("GET", "OAuth2PrivateCloudApiController@getCloudDataCenters",
$params,
array(),
array(),
$headers);
$content = $response->getContent();
$res = json_decode($content);
$this->assertResponseStatus(200);
}
}

View File

@ -0,0 +1,100 @@
<?php
/**
* Copyright 2014 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use oauth2\OAuth2Protocol;
use auth\User;
use utils\services\IAuthService;
/**
* Class OAuth2ProtectedApiTest
*/
abstract class OAuth2ProtectedApiTest extends TestCase {
protected $access_token;
protected $client_id;
protected $client_secret;
protected $current_realm;
abstract protected function getScopes();
protected function prepareForTests()
{
parent::prepareForTests();
Route::enableFilters();
$this->current_realm = Config::get('app.url');
$user = User::where('external_id', '=', 'smarcet@gmail.com')->first();
$this->be($user);
Session::start();
$scope = $this->getScopes();
$this->client_id = 'Jiz87D8/Vcvr6fvQbH4HyNgwTlfSyQ3x.openstack.client';
$this->client_secret = 'ITc/6Y5N7kOtGKhg';
$params = array(
'client_id' => $this->client_id,
'redirect_uri' => 'https://www.test.com/oauth2',
'response_type' => OAuth2Protocol::OAuth2Protocol_ResponseType_Code,
'scope' => implode(' ',$scope),
OAuth2Protocol::OAuth2Protocol_AccessType =>OAuth2Protocol::OAuth2Protocol_AccessType_Offline,
);
Session::set("openid.authorization.response", IAuthService::AuthorizationResponse_AllowOnce);
$response = $this->action("POST", "OAuth2ProviderController@authorize",
$params,
array(),
array(),
array());
$status = $response->getStatusCode();
$url = $response->getTargetUrl();
$content = $response->getContent();
$comps = @parse_url($url);
$query = $comps['query'];
$output = array();
parse_str($query, $output);
$params = array(
'code' => $output['code'],
'redirect_uri' => 'https://www.test.com/oauth2',
'grant_type' => OAuth2Protocol::OAuth2Protocol_GrantType_AuthCode,
);
$response = $this->action("POST", "OAuth2ProviderController@token",
$params,
array(),
array(),
// Symfony interally prefixes headers with "HTTP", so
array("HTTP_Authorization" => " Basic " . base64_encode($this->client_id . ':' . $this->client_secret)));
$status = $response->getStatusCode();
$this->assertResponseStatus(200);
$content = $response->getContent();
$response = json_decode($content);
$access_token = $response->access_token;
$refresh_token = $response->refresh_token;
$this->access_token = $access_token;
}
}

View File

@ -0,0 +1,113 @@
<?php
/**
* Copyright 2015 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use models\marketplace\repositories\ICompanyServiceRepository;
/**
* Class OAuth2PublicCloudApiTest
*/
class OAuth2PublicCloudApiTest extends OAuth2ProtectedApiTest {
public function testGetPublicClouds(){
$params = array(
'page' => 1 ,
'per_page' => 10,
'status' => ICompanyServiceRepository::Status_active,
);
$headers = array("HTTP_Authorization" => " Bearer " .$this->access_token);
$response = $this->action("GET", "OAuth2PublicCloudApiController@getClouds",
$params,
array(),
array(),
$headers);
$content = $response->getContent();
$clouds = json_decode($content);
$this->assertResponseStatus(200);
}
public function testGetPublicCloudNotFound(){
$params = array(
'id' => 0
);
$headers = array("HTTP_Authorization" => " Bearer " .$this->access_token);
$response = $this->action("GET", "OAuth2PublicCloudApiController@getCloud",
$params,
array(),
array(),
$headers);
$content = $response->getContent();
$res = json_decode($content);
$this->assertResponseStatus(404);
}
public function testGetPublicCloudFound(){
$params = array(
'id' => 17
);
$headers = array("HTTP_Authorization" => " Bearer " .$this->access_token);
$response = $this->action("GET", "OAuth2PublicCloudApiController@getCloud",
$params,
array(),
array(),
$headers);
$content = $response->getContent();
$res = json_decode($content);
$this->assertResponseStatus(200);
}
public function testGetDataCenterRegions(){
$params = array(
'id' => 53
);
$headers = array("HTTP_Authorization" => " Bearer " .$this->access_token);
$response = $this->action("GET", "OAuth2PublicCloudApiController@getCloudDataCenters",
$params,
array(),
array(),
$headers);
$content = $response->getContent();
$res = json_decode($content);
$this->assertResponseStatus(200);
}
protected function getScopes()
{
$scope = array(
sprintf('%s/public-clouds/read',$this->current_realm)
);
return $scope;
}
}

View File

@ -1,93 +1,12 @@
<?php
use oauth2\resource_server\IUserService;
use oauth2\OAuth2Protocol;
use auth\User;
use utils\services\IAuthService;
/**
* Class OAuth2UserServiceApiTest
*/
class OAuth2UserServiceApiTest extends TestCase {
class OAuth2UserServiceApiTest extends OAuth2ProtectedApiTest {
private $access_token;
private $client_id;
private $client_secret;
private $current_realm;
protected function prepareForTests()
{
parent::prepareForTests();
Route::enableFilters();
$this->current_realm = Config::get('app.url');
$user = User::where('external_id', '=', 'smarcet@gmail.com')->first();
$this->be($user);
Session::start();
$scope = array(
IUserService::UserProfileScope_Address,
IUserService::UserProfileScope_Email,
IUserService::UserProfileScope_Profile
);
$this->client_id = 'Jiz87D8/Vcvr6fvQbH4HyNgwTlfSyQ3x.openstack.client';
$this->client_secret = 'ITc/6Y5N7kOtGKhg';
$params = array(
'client_id' => $this->client_id,
'redirect_uri' => 'https://www.test.com/oauth2',
'response_type' => OAuth2Protocol::OAuth2Protocol_ResponseType_Code,
'scope' => implode(' ',$scope),
OAuth2Protocol::OAuth2Protocol_AccessType =>OAuth2Protocol::OAuth2Protocol_AccessType_Offline,
);
Session::set("openid.authorization.response", IAuthService::AuthorizationResponse_AllowOnce);
$response = $this->action("POST", "OAuth2ProviderController@authorize",
$params,
array(),
array(),
array());
$status = $response->getStatusCode();
$url = $response->getTargetUrl();
$content = $response->getContent();
$comps = @parse_url($url);
$query = $comps['query'];
$output = array();
parse_str($query, $output);
$params = array(
'code' => $output['code'],
'redirect_uri' => 'https://www.test.com/oauth2',
'grant_type' => OAuth2Protocol::OAuth2Protocol_GrantType_AuthCode,
);
$response = $this->action("POST", "OAuth2ProviderController@token",
$params,
array(),
array(),
// Symfony interally prefixes headers with "HTTP", so
array("HTTP_Authorization" => " Basic " . base64_encode($this->client_id . ':' . $this->client_secret)));
$status = $response->getStatusCode();
$this->assertResponseStatus(200);
$content = $response->getContent();
$response = json_decode($content);
$access_token = $response->access_token;
$refresh_token = $response->refresh_token;
$this->access_token = $access_token;
}
/**
* @covers OAuth2UserApiController::get()
@ -119,4 +38,15 @@ class OAuth2UserServiceApiTest extends TestCase {
$content = $response->getContent();
$user_info = json_decode($content);
}
protected function getScopes()
{
$scope = array(
IUserService::UserProfileScope_Address,
IUserService::UserProfileScope_Email,
IUserService::UserProfileScope_Profile
);
return $scope;
}
}

View File

@ -230,6 +230,9 @@
<label for="route">Route</label>
<input type="text" name="route" id="route">
<label for="rate_limit">Rate Limit (Per Hour)</label>
<input type="text" name="rate_limit" id="rate_limit">
<label for="http_method">HTTP Method</label>
<select name="http_method" id="http_method">
<option value="GET">GET</option>

View File

@ -31,6 +31,13 @@
</div>
</div>
<div class="control-group">
<label class="control-label" for="rate_limit">Rate Limit (Per Hour)</label>
<div class="controls">
<input type="text" name="rate_limit" id="rate_limit" value="{{ $endpoint->rate_limit }}">
</div>
</div>
<div class="control-group">
<label class="control-label" for="http_method">HTTP Method</label>
<div class="controls">

View File

@ -387,7 +387,8 @@ $(document).ready(function() {
rules: {
"name" : {required: true, nowhitespace:true,rangelength: [1, 255]},
"description":{required: true, free_text:true,rangelength: [1, 1024]},
"route": {required: true,endpointroute:true,rangelength: [1, 1024]}
"route": {required: true,endpointroute:true,rangelength: [1, 1024]},
"rate_limit": {required: true, number:true}
}
});

View File

@ -8,7 +8,8 @@ jQuery(document).ready(function($){
rules: {
"name" : {required: true, nowhitespace:true,rangelength: [1, 255]},
"description":{required: true, free_text:true,rangelength: [1, 1024]},
"route": {required: true, endpointroute:true,rangelength: [1, 1024]}
"route": {required: true, endpointroute:true,rangelength: [1, 1024]},
"rate_limit": {required: true, number:true}
}
});

View File

@ -26,7 +26,7 @@ function updateAccessTokenList(){
'td.lifetime':'token.lifetime',
'a@href':function(arg){
var token_value = arg.item.value;
var href = TokensUrls.AccessTokenUrls.de.ete;
var href = TokensUrls.AccessTokenUrls.delete;
return href.replace('-1',token_value);
},
'a@data-value' :'token.value'

View File

@ -11,6 +11,7 @@
run following commands on root folder
* curl -s https://getcomposer.org/installer | php
* php composer.phar install --prefer-dist
* php composer.phar dump-autoload --optimize
* php artisan migrate --env=YOUR_ENVIRONMENT
* php artisan db:seed --env=YOUR_ENVIRONMENT