Skip to content

Commit c7b43d2

Browse files
committed
Initial commit
0 parents  commit c7b43d2

File tree

10 files changed

+268
-0
lines changed

10 files changed

+268
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor/
2+
composer.lock
3+
.idea/
4+
.DS_Store

LICENCE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Stackkit (info@stackkit.io)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<p align="center">
2+
<img src="/logo.png" width="400">
3+
</p>
4+
<p align="center">
5+
<img src="https://github.com/stackkit/laravel-google-cloud-tasks-queue/workflows/Run%20tests/badge.svg?branch=master" alt="Build Status">
6+
<a href="https://packagist.org/packages/stackkit/laravel-google-cloud-tasks-queue"><img src="https://poser.pugx.org/stackkit/laravel-google-cloud-tasks-queue/v/stable.svg" alt="Latest Stable Version"></a>
7+
<a href="https://packagist.org/packages/stackkit/laravel-google-cloud-tasks-queue"><img src="https://poser.pugx.org/stackkit/laravel-google-cloud-tasks-queue/license.svg" alt="License"></a>
8+
</p>
9+
10+
# Introduction
11+
12+
This package allows you to use Google Cloud Scheduler to schedule Laravel commands.
13+
14+
# How it works
15+
16+
# Requirements
17+
18+
This package requires Laravel 5.6 or higher.
19+
20+
Please check the table below for supported Laravel and PHP versions:
21+
22+
|Laravel Version| PHP Version |
23+
|---|---|
24+
| 5.6 | 7.2 or 7.3
25+
| 5.7 | 7.2 or 7.3
26+
| 5.8 | 7.2 or 7.3 or 7.4
27+
| 6.x | 7.2 or 7.3 or 7.4
28+
| 7.x | 7.2 or 7.3 or 7.4
29+
30+
# Installation
31+
32+
(1) Require the package using Composer
33+
34+
```bash
35+
composer require stackkit/laravel-google-cloud-scheduler
36+
```

composer.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "stackkit/laravel-google-cloud-scheduler",
3+
"license": "MIT",
4+
"authors": [
5+
{
6+
"name": "Marick van Tuil",
7+
"email": "info@marickvantuil.nl"
8+
}
9+
],
10+
"require": {
11+
"ext-json": "*",
12+
"google/cloud-scheduler": "^1.4",
13+
"firebase/php-jwt": "^5.2",
14+
"phpseclib/phpseclib": "~2.0"
15+
},
16+
"require-dev": {
17+
"mockery/mockery": "^1.2",
18+
"orchestra/testbench": "^3.5 || ^3.6 || ^3.7 || ^3.8 || ^4.0 || ^5.0",
19+
"symfony/console": "^4.4|^5.0"
20+
},
21+
"autoload": {
22+
"psr-4": {
23+
"Stackkit\\LaravelGoogleCloudTasksQueue\\": "src/"
24+
}
25+
},
26+
"autoload-dev": {
27+
"psr-4": {
28+
"Tests\\": "tests/"
29+
}
30+
},
31+
"extra": {
32+
"laravel": {
33+
"providers": [
34+
"Stackkit\\LaravelGoogleCloudTasksQueue\\CloudTasksServiceProvider"
35+
]
36+
}
37+
}
38+
}

logo.png

48.2 KB
Loading

phpunit.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false">
11+
12+
<testsuites>
13+
<testsuite name="Orchestra\Testbench Test Suite">
14+
<directory suffix="Test.php">./tests/</directory>
15+
</testsuite>
16+
</testsuites>
17+
<php>
18+
<env name="APP_DEBUG" value="1"/>
19+
<env name="APP_ENV" value="testing"/>
20+
<env name="APP_KEY" value="AckfSECXIvnK5r28GVIWUAxmbBSjTsmF"/>
21+
<env name="CACHE_DRIVER" value="array"/>
22+
<env name="SESSION_DRIVER" value="array"/>
23+
<env name="MAIL_DRIVER" value="log"/>
24+
</php>
25+
26+
<filter>
27+
<whitelist addUncoveredFilesFromWhitelist="false">
28+
<directory suffix=".php">src/</directory>
29+
</whitelist>
30+
</filter>
31+
</phpunit>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Stackkit\LaravelGoogleCloudScheduler;
4+
5+
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
6+
use Illuminate\Routing\Router;
7+
use Stackkit\LaravelGoogleCloudTasksQueue\TaskHandler;
8+
9+
class CloudSchedulerServiceProvider extends LaravelServiceProvider
10+
{
11+
public function boot(Router $router)
12+
{
13+
$this->registerRoutes($router);
14+
}
15+
16+
private function registerRoutes(Router $router)
17+
{
18+
$router->post('handle-task', [TaskHandler::class, 'handle']);
19+
}
20+
}

src/OpenIdVerificator.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Stackkit\LaravelGoogleCloudScheduler;
4+
5+
use Firebase\JWT\JWT;
6+
use GuzzleHttp\Client;
7+
use Illuminate\Support\Arr;
8+
use Illuminate\Support\Facades\Cache;
9+
use phpseclib\Crypt\RSA;
10+
use phpseclib\Math\BigInteger;
11+
12+
class OpenIdVerificator
13+
{
14+
private const V3_CERTS = 'GOOGLE_V3_CERTS';
15+
private const URL_OPENID_CONFIG = 'https://accounts.google.com/.well-known/openid-configuration';
16+
private const URL_TOKEN_INFO = 'https://www.googleapis.com/oauth2/v3/tokeninfo';
17+
18+
private $guzzle;
19+
private $rsa;
20+
21+
public function __construct(Client $guzzle, RSA $rsa)
22+
{
23+
$this->guzzle = $guzzle;
24+
$this->rsa = $rsa;
25+
}
26+
27+
public function getPublicKey($kid = null)
28+
{
29+
$v3Certs = Cache::rememberForever(self::V3_CERTS, function () {
30+
return $this->getv3Certs();
31+
});
32+
33+
$cert = $kid ? collect($v3Certs)->firstWhere('kid', '=', $kid) : $v3Certs[0];
34+
35+
return $this->extractPublicKeyFromCertificate($cert);
36+
}
37+
38+
private function getv3Certs()
39+
{
40+
$jwksUri = $this->callApiAndReturnValue(self::URL_OPENID_CONFIG, 'jwks_uri');
41+
42+
return $this->callApiAndReturnValue($jwksUri, 'keys');
43+
}
44+
45+
private function extractPublicKeyFromCertificate($certificate)
46+
{
47+
$modulus = new BigInteger(JWT::urlsafeB64Decode($certificate['n']), 256);
48+
$exponent = new BigInteger(JWT::urlsafeB64Decode($certificate['e']), 256);
49+
50+
$this->rsa->loadKey(compact('modulus', 'exponent'));
51+
52+
return $this->rsa->getPublicKey();
53+
}
54+
55+
public function getKidFromOpenIdToken($openIdToken)
56+
{
57+
return $this->callApiAndReturnValue(self::URL_TOKEN_INFO . '?id_token=' . $openIdToken, 'kid');
58+
}
59+
60+
private function callApiAndReturnValue($url, $value)
61+
{
62+
$response = $this->guzzle->get($url);
63+
64+
$data = json_decode($response->getBody(), true);
65+
66+
return Arr::get($data, $value);
67+
}
68+
69+
public function isCached()
70+
{
71+
return Cache::has(self::V3_CERTS);
72+
}
73+
}

src/TaskHandler.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Stackkit\LaravelGoogleCloudTasksQueue;
4+
5+
class TaskHandler
6+
{
7+
public function handle()
8+
{
9+
//
10+
}
11+
}

tests/TestCase.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
class TestCase extends \Orchestra\Testbench\TestCase
6+
{
7+
/**
8+
* Get package providers. At a minimum this is the package being tested, but also
9+
* would include packages upon which our package depends, e.g. Cartalyst/Sentry
10+
* In a normal app environment these would be added to the 'providers' array in
11+
* the config/app.php file.
12+
*
13+
* @param \Illuminate\Foundation\Application $app
14+
*
15+
* @return array
16+
*/
17+
protected function getPackageProviders($app)
18+
{
19+
return [
20+
\Stackkit\LaravelGoogleCloudScheduler\CloudSchedulerServiceProvider::class,
21+
];
22+
}
23+
24+
/**
25+
* Define environment setup.
26+
*
27+
* @param \Illuminate\Foundation\Application $app
28+
* @return void
29+
*/
30+
protected function getEnvironmentSetUp($app)
31+
{
32+
//
33+
}
34+
}

0 commit comments

Comments
 (0)