openstackid/tests/OAuth2ProtectedApiTest.php
smarcet@gmail.com ebed7b15b4 Passwordlress Flow
* API endpoints for embedded login flow
* unit tests
* UI Tweaks
* Universal login implementation

Signed-off-by: smarcet@gmail.com <smarcet@gmail.com>
Change-Id: Ib09f1486f5d9419ee1df64a9d1c41dc7c9a4a65c
Depends-on: https://review.opendev.org/c/osf/openstackid/+/791306
2021-07-01 16:02:27 -03:00

114 lines
3.7 KiB
PHP

<?php namespace Tests;
/**
* 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\OAuth2Protocol;
use Auth\User;
use Utils\Services\IAuthService;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Config;
use LaravelDoctrine\ORM\Facades\EntityManager;
/**
* Class OAuth2ProtectedApiTest
*/
abstract class OAuth2ProtectedApiTest extends OpenStackIDBaseTest {
/**
* @var string
*/
protected $access_token;
/**
* @var string
*/
protected $client_id;
/**
* @var string
*/
protected $client_secret;
/**
* @var string
*/
protected $current_realm;
abstract protected function getScopes();
protected function prepareForTests()
{
parent::prepareForTests();
$this->current_realm = Config::get('app.url');
//already logged user
$user_repository = EntityManager::getRepository(User::class);
$user = $user_repository->findOneBy(["identifier" => 'sebastian.marcet']);
$this->be($user, 'web');
Session::start();
$scope = $this->getScopes();
$this->client_id = '.-_~87D8/Vcvr6fvQbH4HyNgwTlfSyQ3x.openstack.client';
$this->client_secret = 'ITc/6Y5N7kOtGKhgITc/6Y5N7kOtGKhgITc/6Y5N7kOtGKhgITc/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::put("openid.authorization.response", IAuthService::AuthorizationResponse_AllowOnce);
$response = $this->action("POST", "OAuth2\OAuth2ProviderController@auth",
$params,
[],
[],
[]);
$status = $response->getStatusCode();
$url = $response->getTargetUrl();
$content = $response->getContent();
$comps = @parse_url($url);
$query = $comps['query'];
$output = [];
parse_str($query, $output);
$params = [
'code' => $output['code'],
'redirect_uri' => 'https://www.test.com/oauth2',
'grant_type' => OAuth2Protocol::OAuth2Protocol_GrantType_AuthCode,
];
$response = $this->action
(
$method = "POST",
$action = "OAuth2\OAuth2ProviderController@token",
$params,
[],
[],
[],
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;
}
}