Skip to content

Commit

Permalink
Add Readme
Browse files Browse the repository at this point in the history
  • Loading branch information
pelmered committed Sep 5, 2024
1 parent fc936b0 commit e082030
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 3 deletions.
108 changes: 107 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,107 @@
# laravel-http-oauth-helper
# Laravel HTTP Client OAuth2 helper

An easy-to-use helper for Laravel HTTP Client to make OAuth2 requests.\
Refreshes and caches access tokens automatically with minimal boilerplate code right in the [Laravel HTTP Client](https://laravel.com/docs/11.x/http-client).

[![Latest Stable Version](https://poser.pugx.org/pelmered/laravel-http-oauth-helper/v/stable)](https://packagist.org/packages/pelmered/laravel-http-oauth-helper)
[![Total Downloads](https://poser.pugx.org/pelmered/laravel-http-oauth-helper/d/total)](//packagist.org/packages/pelmered/laravel-http-oauth-helper)
[![Monthly Downloads](https://poser.pugx.org/pelmered/laravel-http-oauth-helper/d/monthly)](//packagist.org/packages/pelmered/laravel-http-oauth-helper)
[![License](https://poser.pugx.org/pelmered/laravel-http-oauth-helper/license)](https://packagist.org/packages/pelmered/laravel-http-oauth-helper)

[![Tests](https://github.com/pelmered/laravel-http-oauth-helper/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/pelmered/laravel-http-oauth-helper/actions/workflows/tests.yml)
[![Build Status](https://scrutinizer-ci.com/g/pelmered/laravel-http-oauth-helper/badges/build.png?b=main)](https://scrutinizer-ci.com/g/pelmered/laravel-http-oauth-helper/build-status/main)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/pelmered/laravel-http-oauth-helper/badges/quality-score.png?b=main)](https://scrutinizer-ci.com/g/pelmered/laravel-http-oauth-helper/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/pelmered/laravel-http-oauth-helper/badges/coverage.png?b=main)](https://scrutinizer-ci.com/g/pelmered/laravel-http-oauth-helper/?branch=main)

[![Tested on PHP 8.2 to 8.3](https://img.shields.io/badge/Tested%20on%20PHP-8.2%20|%208.3-brightgreen.svg?maxAge=2419200)](https://github.com/pelmered/filament-money-field/actions/workflows/tests.yml)
[![Tested on OS:es Linux, MacOS, Windows](https://img.shields.io/badge/Tested%20on%20lastest%20versions%20of-%20Ubuntu%20|%20MacOS%20|%20Windows-brightgreen.svg?maxAge=2419200)](https://github.com/pelmered/laravel-http-oauth-helper/actions/workflows/tests.yml)

## Requirements

- PHP 8.1 or higher
- Laravel 10

## Installation

```bash
composer require pelmered/laravel-http-oauth-helper
```

## Usage

It's really simple to use. Just add the `withOAuthToken` method to your HTTP request and provide the necessary parameters.

Minimal example:
```php
$response = Http::withOAuthToken(
'token_name',
'https://example.com/token.oauth2',
'client_id',
'client_secret',
)->get(
'https://example.com/api',
);
```

All parameters with default values:
```php
$response = Http::withOAuthToken(
'token_name',
'https://example.com/token.oauth2',
'client_id',
'client_secret',
[
'scopes' => [],
'expires' => 'expires_in', // When token should be considered expired. A string key in the response JSON for the expiration. We try to parse different formats and then remove 1 minute to be on the safe side.
'auth_type' => 'body', // 'body' or 'header'
'access_token' => 'access_token', // Key for the access token in the response JSON
],
'Bearer'
)->get(
'https://example.com/api',
);
```

You can also provide callbacks for `expires`, `auth_type`, and `access_token` to customize the behavior.
```php
$response = Http::withOAuthToken(
'token_name',
'https://example.com/token.oauth2',
'client_id',
'client_secret',
[
"expires" => fn($response) => $response->json()["expires_in"] - 300, // Should return the ttl in seconds that has been parsed from the response and can be manipulated as you want.
"access_token_key" => fn($response) => $response->access_token, // Should return the access token that has been parsed from the response.
],
'Bearer'
)->get(
'https://example.com/api',
);
```

## Tip

If you use the same token on multiple places you can create the client only once and save it. For example:
```php
$this->client = Http::withOAuthToken(
'token_name',
'https://example.com/token.oauth2',
'client_id',
'client_secret',
[
'scopes' => ['read:posts', 'write:posts', 'read:comments'],
]
)->baseUrl('https://example.com/api');
```

to use it later like:
```php
$this->client->get('posts');

$this->client->get('comments');

$this->client->post('posts', [
'title' => 'My post',
'content' => 'My content',
]);
```
4 changes: 2 additions & 2 deletions src/RefreshToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __invoke(
): string {
$options = array_merge([
'scopes' => [],
'auth_type' => 'basic',
'auth_type' => 'body',
'expires' => 3600,
'access_token' => 'access_token',
], $options);
Expand All @@ -37,7 +37,7 @@ public function __invoke(

match ($options['auth_type']) {
'basic' => $httpClient->withBasicAuth($clientId, $clientSecret),
'bearer' => $httpClient->withToken($tokenName),
'header' => $httpClient->withToken($tokenName),
'body' => $requestBody = $requestBody + ['client_id' => $clientId, 'client_secret' => $clientSecret],
default => throw new Exception('Invalid auth type')
};
Expand Down

0 comments on commit e082030

Please sign in to comment.