Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept null as a parameter for Sanctum@getAccessTokenFromRequestUsing() #477

Merged
merged 2 commits into from
Nov 3, 2023

Conversation

cosmastech
Copy link
Contributor

@cosmastech cosmastech commented Nov 2, 2023

Have a kind of an odd case where we're communicating between two applications and want to only allow for authorizing via Sanctum (on one endpoint) with a custom method of getting the access token. (The original app uses Passport, the new app uses Sanctum, so the HasApiTokens traits conflict on the User model).

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Laravel\Sanctum\Sanctum;

class SwapTokenController extends Controller
{
    public function __invoke(SwapTokenRequest $request)
    {
        $originalCallback = Sanctum::$accessTokenRetrievalCallback ?? null;

        try {
            Sanctum::getAccessTokenFromRequestUsing(function (Request $request) {
                if (! $sanctumTokenString = $request->get('sanctum_token')) {
                    return null;
                }

                return $sanctumTokenString;
            });

            /** @var User|null $user */
            $sanctumGuard = app('auth')->guard('sanctum');
            $user = $sanctumGuard->user();

            // some other fun stuff here
        } finally {
            Sanctum::$accessTokenRetrievalCallback = $originalCallback;
        }
    }
}

$originalCallback is currently null for our app. It seems kind of weird that we can't just call Sanctum::getAccessTokenFromRequestUsing($originalCallback) instead of having to set the static property on the Sanctum object.

src/Sanctum.php Outdated Show resolved Hide resolved
Co-authored-by: Dries Vints <[email protected]>
@taylorotwell taylorotwell merged commit e1a2728 into laravel:3.x Nov 3, 2023
8 checks passed
renovate bot referenced this pull request in RadioRoster/backend Nov 26, 2023
[![Mend Renovate logo
banner](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [laravel/sanctum](https://togithub.com/laravel/sanctum) | `3.3.1` ->
`3.3.2` |
[![age](https://developer.mend.io/api/mc/badges/age/packagist/laravel%2fsanctum/3.3.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/packagist/laravel%2fsanctum/3.3.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/packagist/laravel%2fsanctum/3.3.1/3.3.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/packagist/laravel%2fsanctum/3.3.1/3.3.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>laravel/sanctum (laravel/sanctum)</summary>

###
[`v3.3.2`](https://togithub.com/laravel/sanctum/blob/HEAD/CHANGELOG.md#v332---2023-11-03)

[Compare
Source](https://togithub.com/laravel/sanctum/compare/v3.3.1...v3.3.2)

- Fix typo in config by
[@&#8203;cosmastech](https://togithub.com/cosmastech) in
[https://github.com/laravel/sanctum/pull/476](https://togithub.com/laravel/sanctum/pull/476)
- Accept null as a parameter for
`Sanctum[@&#8203;getAccessTokenFromRequestUsing](https://togithub.com/getAccessTokenFromRequestUsing)()`
by [@&#8203;cosmastech](https://togithub.com/cosmastech) in
[https://github.com/laravel/sanctum/pull/477](https://togithub.com/laravel/sanctum/pull/477)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/Lapotor/RadioRoster-api).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy41OS44IiwidXBkYXRlZEluVmVyIjoiMzcuNTkuOCIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants