Laravel AT Protocol Client (alpha & unstable)
3
fork

Configure Feed

Select the types of activity you want to include in your feed.

Integrate scope checking into service provider and HasHttp trait

+51
+7
src/AtpClientServiceProvider.php
··· 8 8 use SocialDept\AtpClient\Auth\DPoPKeyManager; 9 9 use SocialDept\AtpClient\Auth\DPoPNonceManager; 10 10 use SocialDept\AtpClient\Auth\OAuthEngine; 11 + use SocialDept\AtpClient\Auth\ScopeChecker; 11 12 use SocialDept\AtpClient\Auth\TokenRefresher; 13 + use SocialDept\AtpClient\Enums\ScopeEnforcementLevel; 12 14 use SocialDept\AtpClient\Console\GenerateOAuthKeyCommand; 13 15 use SocialDept\AtpClient\Contracts\CredentialProvider; 14 16 use SocialDept\AtpClient\Contracts\KeyStore; ··· 56 58 ); 57 59 }); 58 60 $this->app->singleton(OAuthEngine::class); 61 + $this->app->singleton(ScopeChecker::class, function ($app) { 62 + return new ScopeChecker( 63 + config('atp-client.scope_enforcement', ScopeEnforcementLevel::Permissive) 64 + ); 65 + }); 59 66 60 67 // Register main client facade accessor 61 68 $this->app->bind('atp-client', function ($app) {
+44
src/Http/HasHttp.php
··· 5 5 use Illuminate\Http\Client\Response as LaravelResponse; 6 6 use Illuminate\Support\Facades\Http; 7 7 use InvalidArgumentException; 8 + use SocialDept\AtpClient\Auth\ScopeChecker; 9 + use SocialDept\AtpClient\Enums\Scope; 8 10 use SocialDept\AtpClient\Exceptions\ValidationException; 9 11 use SocialDept\AtpClient\Session\Session; 10 12 use SocialDept\AtpClient\Session\SessionManager; ··· 17 19 protected string $did; 18 20 19 21 protected DPoPClient $dpopClient; 22 + 23 + protected ?ScopeChecker $scopeChecker = null; 20 24 21 25 /** 22 26 * Make XRPC call ··· 126 130 ->post($url); 127 131 128 132 return new Response($response); 133 + } 134 + 135 + /** 136 + * Require specific scopes before making a request. 137 + * 138 + * Checks if the session has the required scopes. In strict mode, throws 139 + * MissingScopeException if scopes are missing. In permissive mode, logs 140 + * a warning but allows the request to proceed. 141 + * 142 + * @param string|Scope ...$scopes The required scopes 143 + * 144 + * @throws \SocialDept\AtpClient\Exceptions\MissingScopeException 145 + */ 146 + protected function requireScopes(string|Scope ...$scopes): void 147 + { 148 + $session = $this->sessions->session($this->did); 149 + 150 + $this->getScopeChecker()->checkOrFail($session, $scopes); 151 + } 152 + 153 + /** 154 + * Check if the session has a specific scope. 155 + */ 156 + protected function hasScope(string|Scope $scope): bool 157 + { 158 + $session = $this->sessions->session($this->did); 159 + 160 + return $this->getScopeChecker()->hasScope($session, $scope); 161 + } 162 + 163 + /** 164 + * Get the scope checker instance. 165 + */ 166 + protected function getScopeChecker(): ScopeChecker 167 + { 168 + if ($this->scopeChecker === null) { 169 + $this->scopeChecker = app(ScopeChecker::class); 170 + } 171 + 172 + return $this->scopeChecker; 129 173 } 130 174 }