-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda-handler.php
More file actions
111 lines (102 loc) · 3.28 KB
/
lambda-handler.php
File metadata and controls
111 lines (102 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
declare(strict_types=1);
/**
* AWS Lambda + Bref handler that verifies an inbound Bearer JWT.
*
* Caching strategy on Lambda:
*
* - APCu (kernel shared memory): visible to every PHP-FPM worker in the
* same Lambda container. Survives warm invocations for the lifetime of
* the container. THIS IS THE RIGHT DEFAULT for Bref `php-fpm` runtime.
*
* - In-memory (per-process): only works if you keep the `Client` instance
* in a top-level static, AND each FPM worker fetches its own JWKS on
* first use. Lower hit rate than APCu when the container has >1 worker.
*
* - File cache (`/tmp/...`): also works (Lambda's /tmp is container-scoped
* and writable), but the only real reason to use it over APCu is if
* you've explicitly disabled APCu.
*
* The handler keeps the `Client` in a static so it's reused across warm
* invocations of the same container.
*/
require __DIR__ . '/../vendor/autoload.php';
use Stromcom\AuthClient\Client;
use Stromcom\AuthClient\Configuration;
use Stromcom\AuthClient\Exception\AuthorizationException;
use Stromcom\AuthClient\Exception\TokenVerificationException;
use Stromcom\AuthClient\Jwks\ApcuJwksCache;
use Stromcom\AuthClient\Jwks\InMemoryJwksCache;
use Stromcom\AuthClient\Jwks\JwksCacheInterface;
/**
* Lazy singleton — instantiated once per Lambda container, reused across
* warm invocations.
*/
function auth(): Client {
static $client = null;
if ($client === null) {
$client = new Client(
new Configuration(
clientId: (string) getenv('AUTH_CLIENT_ID'),
issuer: getenv('AUTH_ISSUER') ?: 'https://auth.stromcom.cz',
),
jwksCache: jwksCache(),
);
}
return $client;
}
function jwksCache(): JwksCacheInterface {
if (function_exists('apcu_enabled') && apcu_enabled()) {
return new ApcuJwksCache();
}
// Fallback: per-process memory. Still survives warm invocations because
// the Client is held in a static.
return new InMemoryJwksCache();
}
/**
* Bref-style API Gateway handler.
*
* In serverless.yml:
* functions:
* api:
* handler: examples/lambda-handler.php
* runtime: php-84-fpm
*
* @param array<string, mixed> $event
* @return array<string, mixed>
*/
return static function (array $event): array {
$authHeader = (string) (
$event['headers']['authorization']
?? $event['headers']['Authorization']
?? ''
);
if (!preg_match('/^Bearer\s+(.+)$/i', $authHeader, $m)) {
return response(401, ['error' => 'missing_bearer_token']);
}
try {
$claims = auth()->verify($m[1]);
$claims->requireUserToken();
$claims->requireGroup('translate-editor');
} catch (TokenVerificationException $e) {
return response(401, ['error' => 'invalid_token', 'message' => $e->getMessage()]);
} catch (AuthorizationException $e) {
return response(403, ['error' => 'forbidden', 'message' => $e->getMessage()]);
}
return response(200, [
'subject' => $claims->subject,
'email' => $claims->email,
'groups' => $claims->groups,
]);
};
/**
* @param array<string, mixed> $body
* @return array<string, mixed>
*/
function response(int $status, array $body): array {
return [
'statusCode' => $status,
'headers' => ['Content-Type' => 'application/json'],
'body' => json_encode($body, JSON_UNESCAPED_SLASHES),
];
}