{"id":51045339,"url":"https://github.com/whilesmartphp/eloquent-client-credentials","last_synced_at":"2026-06-22T13:02:31.851Z","repository":{"id":364264115,"uuid":"1113380707","full_name":"whilesmartphp/eloquent-client-credentials","owner":"whilesmartphp","description":"Simple OAuth2 client credentials authentication to any Eloquent model.","archived":false,"fork":false,"pushed_at":"2026-06-12T10:24:59.000Z","size":41,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-06-12T11:10:48.051Z","etag":null,"topics":["api-keys","apps","m2m","oauth","oauth2"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/whilesmartphp.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-12-09T22:40:42.000Z","updated_at":"2026-06-12T09:44:36.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/whilesmartphp/eloquent-client-credentials","commit_stats":null,"previous_names":["whilesmartphp/eloquent-client-credentials"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/whilesmartphp/eloquent-client-credentials","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whilesmartphp%2Feloquent-client-credentials","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whilesmartphp%2Feloquent-client-credentials/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whilesmartphp%2Feloquent-client-credentials/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whilesmartphp%2Feloquent-client-credentials/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/whilesmartphp","download_url":"https://codeload.github.com/whilesmartphp/eloquent-client-credentials/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whilesmartphp%2Feloquent-client-credentials/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34649822,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-22T02:00:06.391Z","response_time":106,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api-keys","apps","m2m","oauth","oauth2"],"created_at":"2026-06-22T13:02:31.167Z","updated_at":"2026-06-22T13:02:31.846Z","avatar_url":"https://github.com/whilesmartphp.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Eloquent Client Credentials\n\nAdd OAuth2 client credentials authentication to any Eloquent model.\n\n## Installation\n\n```bash\ncomposer require whilesmart/eloquent-client-credentials\n```\n\nPublish the config file:\n\n```bash\nphp artisan vendor:publish --tag=client-credentials-config\n```\n\nRun migrations:\n\n```bash\nphp artisan migrate\n```\n\n## Configuration\n\n```php\n// config/client-credentials.php\nreturn [\n    'default_model' =\u003e \\Whilesmart\\EloquentClientCredentials\\Models\\Client::class,\n\n    'owner_resolver' =\u003e \\Whilesmart\\EloquentClientCredentials\\Resolvers\\DefaultOwnerResolver::class,\n\n    'middleware_hooks' =\u003e [],\n\n    'routes' =\u003e [\n        'enabled' =\u003e false,\n        'prefix' =\u003e 'api',\n        'middleware' =\u003e [],\n        'client_routes' =\u003e false,\n    ],\n\n    'oauth' =\u003e [\n        'enabled' =\u003e true,\n        'token_lifetime' =\u003e 3600,\n        'refresh_token_lifetime' =\u003e 86400 * 30,\n        'refresh_tokens_enabled' =\u003e false,\n    ],\n];\n```\n\n## Usage\n\n### Using the Default Client Model\n\nEnable routes in your config:\n\n```php\n'routes' =\u003e [\n    'enabled' =\u003e true,\n    'prefix' =\u003e 'api',\n    'middleware' =\u003e ['auth:sanctum'],\n    'client_routes' =\u003e true,\n],\n```\n\nThis registers the following routes:\n\n| Method | URI | Description |\n|--------|-----|-------------|\n| POST | `/api/oauth/token` | Issue access token |\n| POST | `/api/oauth/revoke` | Revoke access token |\n| GET | `/api/clients` | List clients |\n| POST | `/api/clients` | Create client |\n| GET | `/api/clients/{slug}` | Get client |\n| PUT | `/api/clients/{slug}` | Update client |\n| DELETE | `/api/clients/{slug}` | Delete client |\n| POST | `/api/clients/{slug}/regenerate-secret` | Regenerate secret |\n\n### Adding Client Credentials to Your Own Model\n\nUse the `HasClientCredentials` trait:\n\n```php\nuse Whilesmart\\EloquentClientCredentials\\Traits\\HasClientCredentials;\n\nclass ApiApp extends Model\n{\n    use HasClientCredentials;\n\n    protected $fillable = ['name', 'secret', /* ... */];\n}\n```\n\nThe trait provides:\n\n- `setSecret(string $plainSecret)` - Hash and store a secret\n- `verifySecret(string $secret)` - Verify a plain secret against stored hash\n- `regenerateSecret()` - Generate and store a new random secret\n- `plainSecret` - Access the plain secret (only available immediately after creation/regeneration)\n\n### Owner Resolver\n\nConfigure how the owner is resolved for client operations. Create a custom resolver:\n\n```php\nuse Whilesmart\\EloquentClientCredentials\\Contracts\\OwnerResolverInterface;\n\nclass CustomOwnerResolver implements OwnerResolverInterface\n{\n    public function resolve(Request $request): ?Model\n    {\n        return $request-\u003euser()-\u003ecurrentTeam;\n    }\n}\n```\n\nRegister in config:\n\n```php\n'owner_resolver' =\u003e \\App\\Resolvers\\CustomOwnerResolver::class,\n```\n\nYou can also pass an owner directly when using the controller programmatically:\n\n```php\n$controller = app(ClientController::class);\n$controller-\u003estore($request, $customOwner);\n```\n\n## OAuth2 Token Flow\n\n### Issuing Tokens\n\n**Client Credentials Grant:**\n\n```bash\nPOST /api/oauth/token\nContent-Type: application/json\n\n{\n    \"grant_type\": \"client_credentials\",\n    \"client_id\": \"your-client-id\",\n    \"client_secret\": \"your-client-secret\",\n    \"scope\": \"read write\"\n}\n```\n\nResponse:\n\n```json\n{\n    \"access_token\": \"...\",\n    \"token_type\": \"Bearer\",\n    \"expires_in\": 3600,\n    \"scope\": \"read write\",\n    \"refresh_token\": \"...\"\n}\n```\n\n**Refresh Token Grant** (when enabled):\n\n```bash\nPOST /api/oauth/token\nContent-Type: application/json\n\n{\n    \"grant_type\": \"refresh_token\",\n    \"refresh_token\": \"your-refresh-token\"\n}\n```\n\n### Revoking Tokens\n\n```bash\nPOST /api/oauth/revoke\nAuthorization: Bearer your-access-token\n```\n\n## Middleware\n\n### Bearer Token Authentication\n\nAuthenticate requests using OAuth2 bearer tokens:\n\n```php\nRoute::middleware('client.bearer')-\u003egroup(function () {\n    Route::get('/resource', fn () =\u003e 'Protected');\n});\n\n// With scope requirement\nRoute::middleware('client.bearer:admin')-\u003eget('/admin', fn () =\u003e 'Admin only');\n```\n\n### Basic Auth\n\nAuthenticate using HTTP Basic Authentication:\n\n```php\nRoute::middleware('client.basic')-\u003egroup(function () {\n    Route::get('/resource', fn () =\u003e 'Protected');\n});\n```\n\nCredentials: `client_id:client_secret` base64 encoded.\n\n### Header-Based Authentication\n\nAuthenticate using custom headers:\n\n```php\nRoute::middleware('client.auth')-\u003egroup(function () {\n    Route::get('/resource', fn () =\u003e 'Protected');\n});\n```\n\nHeaders required:\n- `X-Client-ID: your-client-id`\n- `X-Client-Secret: your-client-secret`\n\n### Registering Middleware\n\nIn your `bootstrap/app.php` or service provider:\n\n```php\nuse Whilesmart\\EloquentClientCredentials\\Http\\Middleware\\AuthenticateBearerToken;\nuse Whilesmart\\EloquentClientCredentials\\Http\\Middleware\\AuthenticateBasicAuth;\nuse Whilesmart\\EloquentClientCredentials\\Http\\Middleware\\AuthenticateClient;\n\n-\u003ewithMiddleware(function (Middleware $middleware) {\n    $middleware-\u003ealias([\n        'client.bearer' =\u003e AuthenticateBearerToken::class,\n        'client.basic' =\u003e AuthenticateBasicAuth::class,\n        'client.auth' =\u003e AuthenticateClient::class,\n    ]);\n})\n```\n\n## Hook System\n\nAdd custom logic before/after controller actions:\n\n```php\n// config/client-credentials.php\n'middleware_hooks' =\u003e [\n    \\App\\Hooks\\MyClientHook::class,\n],\n```\n\nCreate a hook class:\n\n```php\nuse Whilesmart\\EloquentClientCredentials\\Interfaces\\MiddlewareHookInterface;\n\nclass MyClientHook implements MiddlewareHookInterface\n{\n    public function before(Request $request, string $action): ?Request\n    {\n        // Modify request or return null to continue\n        return $request;\n    }\n\n    public function after(Request $request, JsonResponse $response, string $action): JsonResponse\n    {\n        // Modify response\n        return $response;\n    }\n}\n```\n\nAvailable hook actions (from `HookAction` enum):\n- `CLIENT_STORE`\n- `CLIENT_UPDATE`\n- `CLIENT_DELETE`\n- `CLIENT_REGENERATE_SECRET`\n- `TOKEN_ISSUE`\n- `TOKEN_REVOKE`\n\n## Models\n\n### Client\n\nDefault client model with:\n- UUID primary key\n- Sluggable name\n- Polymorphic owner relationship\n- Revocation support\n\n### AccessToken\n\nOAuth2 access tokens with:\n- UUID primary key\n- Polymorphic client relationship\n- Scopes support\n- Expiration and revocation\n\n### RefreshToken\n\nRefresh tokens with:\n- UUID primary key\n- Linked to access token (cascades on delete)\n- Expiration and revocation\n\n## Publishing Assets\n\n```bash\n# Config\nphp artisan vendor:publish --tag=client-credentials-config\n\n# Migrations\nphp artisan vendor:publish --tag=client-credentials-migrations\n\n# Language files\nphp artisan vendor:publish --tag=client-credentials-lang\n\n# Routes\nphp artisan vendor:publish --tag=client-credentials-routes\n```\n\n## Testing\n\n```bash\ncomposer test\n```\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwhilesmartphp%2Feloquent-client-credentials","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwhilesmartphp%2Feloquent-client-credentials","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwhilesmartphp%2Feloquent-client-credentials/lists"}