|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Auth; |
| 4 | + |
| 5 | +use App\Contracts\AuthProvider; |
| 6 | +use Illuminate\Http\Request; |
| 7 | + |
| 8 | +final class ConfiguredAuthProvider implements AuthProvider |
| 9 | +{ |
| 10 | + private const ROLE_WORKER = 'worker'; |
| 11 | + |
| 12 | + private const ROLE_OPERATOR = 'operator'; |
| 13 | + |
| 14 | + private const ROLE_ADMIN = 'admin'; |
| 15 | + |
| 16 | + private const ROLES = [ |
| 17 | + self::ROLE_WORKER, |
| 18 | + self::ROLE_OPERATOR, |
| 19 | + self::ROLE_ADMIN, |
| 20 | + ]; |
| 21 | + |
| 22 | + public function authenticate(Request $request): Principal |
| 23 | + { |
| 24 | + $driver = (string) config('server.auth.driver', 'none'); |
| 25 | + |
| 26 | + return match ($driver) { |
| 27 | + 'none' => Principal::role(self::ROLE_ADMIN, 'none', legacyFullAccess: true, subject: 'anonymous'), |
| 28 | + 'token' => $this->authenticateToken($request), |
| 29 | + 'signature' => $this->authenticateSignature($request), |
| 30 | + default => throw AuthException::configuration("Unknown auth driver: {$driver}"), |
| 31 | + }; |
| 32 | + } |
| 33 | + |
| 34 | + public function authorize(Principal $principal, string $action, array $resource = []): bool |
| 35 | + { |
| 36 | + if ($principal->legacyFullAccess()) { |
| 37 | + return true; |
| 38 | + } |
| 39 | + |
| 40 | + $allowedRoles = $resource['allowed_roles'] ?? []; |
| 41 | + |
| 42 | + if (! is_array($allowedRoles) || $allowedRoles === []) { |
| 43 | + return false; |
| 44 | + } |
| 45 | + |
| 46 | + foreach ($allowedRoles as $role) { |
| 47 | + if (is_string($role) && $principal->hasRole($role)) { |
| 48 | + return true; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + private function authenticateToken(Request $request): Principal |
| 56 | + { |
| 57 | + $token = config('server.auth.token'); |
| 58 | + $roleTokens = $this->configuredRoleSecrets('server.auth.role_tokens'); |
| 59 | + $hasRoleTokens = $roleTokens !== []; |
| 60 | + $hasLegacyToken = is_string($token) && $token !== ''; |
| 61 | + $backwardCompatible = (bool) config('server.auth.backward_compatible', true); |
| 62 | + |
| 63 | + if (! $hasRoleTokens && (! $backwardCompatible || ! $hasLegacyToken)) { |
| 64 | + throw AuthException::configuration('Auth driver is set to "token" but DW_AUTH_TOKEN is not configured.'); |
| 65 | + } |
| 66 | + |
| 67 | + $provided = $request->bearerToken(); |
| 68 | + |
| 69 | + if (! $provided) { |
| 70 | + throw AuthException::unauthenticated('Invalid or missing authentication token.'); |
| 71 | + } |
| 72 | + |
| 73 | + foreach ($roleTokens as $role => $secret) { |
| 74 | + if (hash_equals($secret, $provided)) { |
| 75 | + return Principal::role($role, 'token'); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if ($backwardCompatible && $hasLegacyToken && hash_equals($token, $provided)) { |
| 80 | + return Principal::role( |
| 81 | + self::ROLE_ADMIN, |
| 82 | + 'token', |
| 83 | + legacyFullAccess: ! $hasRoleTokens, |
| 84 | + subject: 'legacy-token', |
| 85 | + claims: [ |
| 86 | + 'legacy_credential' => true, |
| 87 | + ], |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + throw AuthException::unauthenticated('Invalid or missing authentication token.'); |
| 92 | + } |
| 93 | + |
| 94 | + private function authenticateSignature(Request $request): Principal |
| 95 | + { |
| 96 | + $key = config('server.auth.signature_key'); |
| 97 | + $roleKeys = $this->configuredRoleSecrets('server.auth.role_signature_keys'); |
| 98 | + $hasRoleKeys = $roleKeys !== []; |
| 99 | + $hasLegacyKey = is_string($key) && $key !== ''; |
| 100 | + $backwardCompatible = (bool) config('server.auth.backward_compatible', true); |
| 101 | + |
| 102 | + if (! $hasRoleKeys && (! $backwardCompatible || ! $hasLegacyKey)) { |
| 103 | + throw AuthException::configuration('Auth driver is set to "signature" but DW_SIGNATURE_KEY is not configured.'); |
| 104 | + } |
| 105 | + |
| 106 | + $signature = $request->header('X-Signature'); |
| 107 | + |
| 108 | + if (! $signature) { |
| 109 | + throw AuthException::unauthenticated('Missing request signature.'); |
| 110 | + } |
| 111 | + |
| 112 | + $body = $request->getContent(); |
| 113 | + |
| 114 | + foreach ($roleKeys as $role => $secret) { |
| 115 | + $expected = hash_hmac('sha256', $body, $secret); |
| 116 | + |
| 117 | + if (hash_equals($expected, $signature)) { |
| 118 | + return Principal::role($role, 'signature'); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + if ($backwardCompatible && $hasLegacyKey) { |
| 123 | + $expected = hash_hmac('sha256', $body, $key); |
| 124 | + |
| 125 | + if (hash_equals($expected, $signature)) { |
| 126 | + return Principal::role( |
| 127 | + self::ROLE_ADMIN, |
| 128 | + 'signature', |
| 129 | + legacyFullAccess: ! $hasRoleKeys, |
| 130 | + subject: 'legacy-signature', |
| 131 | + claims: [ |
| 132 | + 'legacy_credential' => true, |
| 133 | + ], |
| 134 | + ); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + throw AuthException::unauthenticated('Invalid request signature.'); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * @return array<string, string> |
| 143 | + */ |
| 144 | + private function configuredRoleSecrets(string $configKey): array |
| 145 | + { |
| 146 | + $configured = config($configKey, []); |
| 147 | + |
| 148 | + if (! is_array($configured)) { |
| 149 | + return []; |
| 150 | + } |
| 151 | + |
| 152 | + $secrets = []; |
| 153 | + |
| 154 | + foreach (self::ROLES as $role) { |
| 155 | + $secret = $configured[$role] ?? null; |
| 156 | + |
| 157 | + if (is_string($secret) && $secret !== '') { |
| 158 | + $secrets[$role] = $secret; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + return $secrets; |
| 163 | + } |
| 164 | +} |
0 commit comments