Skip to content

Commit c44e1e9

Browse files
authored
Merge branch 'main' into feature/lv11-version
2 parents 82db533 + 937a65a commit c44e1e9

87 files changed

Lines changed: 2627 additions & 356 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,5 @@ model.sql
4949
/public/assets/*.png
5050
/public/assets/*.txt
5151
/.env.local
52-
/.phpunit.cache/
52+
/.phpunit.cache/
53+
docker-compose/mysql/model/*.sql

LOCAL_DEVELOPMENT_HOWTO.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
Run Local Dev Server
2+
====================
3+
4+
1. Create [.env](.env) file with following properties
5+
6+
```dotenv
7+
GITHUB_OAUTH_TOKEN="<GITHUB TOKEN FROM YOUR GITHUB ACCOUNT>"
8+
9+
APP_ENV=local
10+
APP_DEBUG=true
11+
APP_KEY=<YOUR LV APP KEY>
12+
DEV_EMAIL_TO=smarcet@gmail.com
13+
APP_URL=http://localhost
14+
DB_CONNECTION=mysql
15+
DB_HOST=db
16+
DB_PORT=3306
17+
DB_DATABASE=idp_local
18+
DB_USERNAME=idp_user
19+
DB_PASSWORD=1qaz2wsx!
20+
REDIS_HOST=redis
21+
REDIS_PORT=6379
22+
REDIS_DB=0
23+
REDIS_PASSWORD=1qaz2wsx!
24+
REDIS_DATABASES=16
25+
SSL_ENABLED=false
26+
```
27+
2.( optional ) Drop here [docker-compose/mysql/model](docker-compose/mysql/model) the database dump *.sql file
28+
3.Install docker and docker compose see
29+
[https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-compose-on-ubuntu-22-04](https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-compose-on-ubuntu-22-04) and [https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-22-04](https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-22-04)
30+
4.Run script ./start_local_server.sh (http://localhost:8001/)
31+
32+
Redump the database
33+
===================
34+
35+
````bash
36+
mysql -u root -h 127.0.0.1 -P 30780 --password=<DB_PASSWORD> < mydump.sql
37+
````
38+
39+
Useful Commands
40+
===============
41+
42+
check containers health status
43+
44+
````bash
45+
docker inspect --format "{{json .State.Health }}" www-openstack-model-db-local | jq '.
46+
````

PHPSTORM_DOCKER_CONFIG.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Debug config at PHPSTORM
2+
==========================
3+
4+
1. Add a new CLI interpreter using [Docker]
5+
1. choose image www-openstack:latest
6+
2. click [OK]
7+
3. select new CLI interpreter
8+
4. click [Apply]
9+
10+
2. Edit network settings at container
11+
1. goto Settings->PHP and locate "Docker Container" input.
12+
2. Click on Folder icon.
13+
3. a new popup titled "Edit Docker Container Settings" will open.
14+
4. fill the "Network Mode" input with the bridge name, to find it out run ```$docker network list``` command.
15+
and put the name of the bridge there.
16+
17+
3. Create new server
18+
1. goto Settings->PHP->Servers
19+
2. click on [+]
20+
3. fill up Name with "Docker"
21+
4. fill up Host with "0.0.0.0"
22+
5. fill up Port with "80"
23+
6. click use map mappings
24+
7. map root to /var/www
25+
26+
4. Create a remote debug configuration profile
27+
1. goto Run->Debug->Edit Configurations
28+
2. create a new "PHP Remote debug" Profile
29+
3. set name as "Docker"
30+
4. check "Filter debug connection by IDE key"
31+
5. set IDE KEY as "PHPSTORM"
32+
6. set Server as "Docker"

app/Console/Commands/CleanOAuth2StaleData.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,29 @@ public function handle()
5858
$res = DB::table('oauth2_access_token')
5959
->whereRaw("DATE_ADD(created_at, INTERVAL lifetime second) <= UTC_TIMESTAMP()")
6060
->delete();
61-
6261
Log::debug(sprintf("CleanOAuth2StaleData::handle %s rows where deleted from oauth2_access_token", $res));
62+
$this->info(sprintf("CleanOAuth2StaleData::handle %s rows where deleted from oauth2_access_token", $res));
63+
}
64+
65+
if (Schema::hasTable('oauth2_otp')) {
66+
$res = DB::table('oauth2_otp')
67+
->whereRaw("lifetime > 0 and DATE_ADD(created_at, INTERVAL lifetime second) <= UTC_TIMESTAMP()")
68+
->delete();
69+
70+
Log::debug(sprintf("CleanOAuth2StaleData::handle %s rows where deleted from oauth2_otp", $res));
71+
$this->info(sprintf("CleanOAuth2StaleData::handle %s rows where deleted from oauth2_otp", $res));
6372
}
6473

74+
75+
if (Schema::hasTable('oauth2_refresh_token')) {
76+
$res = DB::table('oauth2_refresh_token')
77+
->whereRaw("void = 1")
78+
->delete();
79+
80+
Log::debug(sprintf("CleanOAuth2StaleData::handle %s rows where deleted from oauth2_refresh_token", $res));
81+
$this->info(sprintf("CleanOAuth2StaleData::handle %s rows where deleted from oauth2_refresh_token", $res));
82+
}
83+
84+
6585
}
6686
}

app/Console/Commands/CreateSuperAdmin.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,12 @@ public function handle()
8181
EntityManager::flush();
8282
}
8383

84-
$user->addToGroup($group);
84+
try {
85+
$user->addToGroup($group);
86+
}
87+
catch (\Exception $ex){
88+
// already in group
89+
}
8590
EntityManager::persist($user);
8691
EntityManager::flush();
8792
}

app/Http/Controllers/Api/ClientApiController.php

Lines changed: 101 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@
1212
* limitations under the License.
1313
**/
1414
use App\Http\Controllers\APICRUDController;
15+
use App\Http\Controllers\ParametrizedGetAll;
16+
use App\Http\Exceptions\HTTP401UnauthorizedException;
1517
use App\Http\Utils\PagingConstants;
1618
use App\ModelSerializers\SerializerRegistry;
1719
use Exception;
1820
use Illuminate\Support\Facades\Auth;
1921
use Illuminate\Support\Facades\Request;
2022
use Illuminate\Support\Facades\Validator;
2123
use models\exceptions\EntityNotFoundException;
24+
use models\utils\IBaseRepository;
25+
use OAuth2\IResourceServerContext;
2226
use OAuth2\Repositories\IAccessTokenRepository;
2327
use OAuth2\Repositories\IClientRepository;
2428
use OAuth2\Repositories\IRefreshTokenRepository;
@@ -39,6 +43,8 @@
3943
final class ClientApiController extends APICRUDController
4044
{
4145

46+
use ParametrizedGetAll;
47+
4248
/**
4349
* @var IApiScopeService
4450
*/
@@ -495,61 +501,97 @@ public function getRefreshTokens($id)
495501

496502
/**
497503
* @return mixed
504+
* @throws HTTP401UnauthorizedException
498505
*/
499506
public function getAccessTokensByCurrentUser()
500507
{
501-
$values = Request::all();
502-
$rules = [
503-
504-
'page' => 'integer|min:1',
505-
'per_page' => sprintf('required_with:page|integer|min:%s|max:%s', PagingConstants::MinPageSize, PagingConstants::MaxPageSize),
506-
];
507-
508-
try {
509-
$validation = Validator::make($values, $rules);
510-
511-
if ($validation->fails()) {
512-
$ex = new ValidationException();
513-
throw $ex->setMessages($validation->messages()->toArray());
508+
$user = $this->auth_service->getCurrentUser();
509+
if(is_null($user))
510+
throw new HTTP401UnauthorizedException();
511+
512+
return $this->_getAll(
513+
function () {
514+
return [
515+
'client_name' => ['=@', '==','@@'],
516+
'device_info' => ['=@', '==','@@'],
517+
'from_ip' => ['=@', '==','@@'],
518+
'scope' => ['=@', '==','@@'],
519+
];
520+
},
521+
function () {
522+
return [
523+
'client_name' => 'sometimes|string',
524+
'device_info' => 'sometimes|string',
525+
'from_ip' => 'sometimes|string',
526+
'scope' => 'sometimes|string',
527+
];
528+
},
529+
function () {
530+
return [
531+
'client_name',
532+
'created_at',
533+
'device_info',
534+
'from_ip',
535+
'scope'
536+
];
537+
},
538+
function ($filter) use($user) {
539+
if($filter instanceof Filter){
540+
$filter->addFilterCondition(FilterElement::makeEqual('owner_id', $user->getId()));
541+
$filter->addFilterCondition(FilterElement::makeEqual('is_valid', true));
542+
}
543+
return $filter;
544+
},
545+
function () {
546+
return SerializerRegistry::SerializerType_Public;
514547
}
548+
);
515549

516-
// default values
517-
$page = 1;
518-
$per_page = PagingConstants::DefaultPageSize;;
550+
}
519551

520-
if (Request::has('page')) {
521-
$page = intval(Request::input('page'));
522-
$per_page = intval(Request::input('per_page'));
552+
/**
553+
* @return mixed
554+
*/
555+
public function getAllAccessTokens()
556+
{
557+
return $this->_getAll(
558+
function () {
559+
return [
560+
'owner_id' => ['=='],
561+
'client_name' => ['=@', '==','@@'],
562+
'device_info' => ['=@', '==','@@'],
563+
'from_ip' => ['=@', '==','@@'],
564+
'scope' => ['=@', '==','@@'],
565+
];
566+
},
567+
function () {
568+
return [
569+
'owner_id' => 'required|int',
570+
'client_name' => 'sometimes|string',
571+
'device_info' => 'sometimes|string',
572+
'from_ip' => 'sometimes|string',
573+
'scope' => 'sometimes|string',
574+
];
575+
},
576+
function () {
577+
return [
578+
'client_name',
579+
'created_at',
580+
'device_info',
581+
'from_ip',
582+
'scope'
583+
];
584+
},
585+
function ($filter) {
586+
if($filter instanceof Filter){
587+
$filter->addFilterCondition(FilterElement::makeEqual('is_valid', true));
588+
}
589+
return $filter;
590+
},
591+
function () {
592+
return SerializerRegistry::SerializerType_Public;
523593
}
524-
525-
$user = $this->auth_service->getCurrentUser();
526-
527-
$data = $this->access_token_repository->getAllValidByUserId($user->getId(), new PagingInfo($page, $per_page));
528-
return $this->ok
529-
(
530-
$data->toArray
531-
(
532-
Request::input('expand', ''),
533-
[],
534-
[],
535-
[]
536-
)
537-
);
538-
}
539-
catch (ValidationException $ex1)
540-
{
541-
Log::warning($ex1);
542-
return $this->error412(array($ex1->getMessage()));
543-
}
544-
catch (EntityNotFoundException $ex2)
545-
{
546-
Log::warning($ex2);
547-
return $this->error404(array('message' => $ex2->getMessage()));
548-
}
549-
catch (Exception $ex) {
550-
Log::error($ex);
551-
return $this->error500($ex);
552-
}
594+
);
553595
}
554596

555597
/**
@@ -679,6 +721,7 @@ protected function getUpdatePayloadValidationRules(): array
679721
'otp_enabled' => 'sometimes|boolean',
680722
'otp_length' => 'sometimes|integer|min:4|max:8',
681723
'otp_lifetime' => 'sometimes|integer|min:60|max:600',
724+
'max_allowed_user_sessions' => 'sometimes|integer|min:0',
682725
];
683726
}
684727

@@ -695,4 +738,14 @@ protected function getCreatePayloadValidationRules(): array
695738
'admin_users' => 'nullable|int_array',
696739
];
697740
}
741+
742+
protected function getResourceServerContext(): IResourceServerContext
743+
{
744+
// TODO: Implement getResourceServerContext() method.
745+
}
746+
747+
protected function getRepository(): IBaseRepository
748+
{
749+
return $this->access_token_repository;
750+
}
698751
}

app/Http/Controllers/Api/OAuth2/OAuth2UserApiController.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,39 @@ protected function curateUpdatePayload(array $payload): array
160160
]);
161161
}
162162

163+
private function _create(){
164+
try {
165+
166+
if(!Request::isJson()) return $this->error400();
167+
168+
$payload = Request::json()->all();
169+
// Creates a Validator instance and validates the data.
170+
$validation = Validator::make($payload, UserValidationRulesFactory::build($payload));
171+
if ($validation->fails()) {
172+
$ex = new ValidationException();
173+
throw $ex->setMessages($validation->messages()->toArray());
174+
}
175+
176+
$user = $this->openid_user_service->create($payload);
177+
178+
return $this->created(SerializerRegistry::getInstance()->getSerializer($user, SerializerRegistry::SerializerType_Private)->serialize());
179+
}
180+
catch (ValidationException $ex1)
181+
{
182+
Log::warning($ex1);
183+
return $this->error412($ex1->getMessages());
184+
}
185+
catch (EntityNotFoundException $ex2)
186+
{
187+
Log::warning($ex2);
188+
return $this->error404(['message' => $ex2->getMessage()]);
189+
}
190+
catch (Exception $ex) {
191+
Log::error($ex);
192+
return $this->error500($ex);
193+
}
194+
}
195+
163196
private function _update($id){
164197
try {
165198

@@ -193,6 +226,10 @@ private function _update($id){
193226
}
194227
}
195228

229+
public function create(){
230+
return $this->_create();
231+
}
232+
196233
public function updateMe(){
197234
return $this->_update($this->resource_server_context->getCurrentUserId());
198235
}

0 commit comments

Comments
 (0)