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

Make TryIt work with Sanctum's cookie based authentication out of the box #336

Merged
merged 2 commits into from
Mar 24, 2024
Merged

Make TryIt work with Sanctum's cookie based authentication out of the box #336

merged 2 commits into from
Mar 24, 2024

Conversation

layerok
Copy link
Contributor

@layerok layerok commented Mar 13, 2024

Fixes #286

In this PR I added code that intercepts every TryIt request and adds an XSRF-TOKEN header, which is necessary for Sanctum's cookie based authentication to work correctly. Intercepting was done by monkey-patching fetch function

@layerok
Copy link
Contributor Author

layerok commented Mar 13, 2024

Previously I've been fixing this issue but I've only fixed one part of it. This PR fixes second and the last part. Now Sanctum cookie based authentication should work out of the box.

@romalytvynenko
Copy link
Member

@vovarudomanenko you are the best, thanks! Will test for API token based auth soon and merge. Thanks again, this was really painful issue.

@layerok
Copy link
Contributor Author

layerok commented Mar 18, 2024

@romalytvynenko First of all, you are the best. Secondly, I wanted to tell that you should only test for cookie-based authentication because my solution won't work for token-based one.

Token-based auth requires a different solution.
Specifically, it requires to pass a token in the Authorization header as a bearer token.

I currently don't know how to solve this problem universally for everyone. Because in order to automatically pass a token to a request I must to know how to create the token and that depends on the specific API implementation.

But I know one solution to this problem, which I don't really like, but it works. This solution requires adding the Authorization header to the OpenAPI definition and manually inserting the token into TryIt form before submitting a request.

Scramble::extendOpenApi(function (OpenApi $openApi) {
    $openApi->secure(SecurityScheme::http('bearer', 'JWT'));
});
image

@romalytvynenko romalytvynenko changed the title make TryIt work with Sanctum's cookie based authentication out of the box Make TryIt work with Sanctum's cookie based authentication out of the box Mar 24, 2024
@romalytvynenko
Copy link
Member

@vovarudomanenko hey!

I meant in general if the solution affects token based auth (due to fetch fn being patched). I've just tested the behavior and it looks good!

Speaking of inserting API token automatically – this is really a nice idea. If it'll resurface, I'll add it.

Thanks again for your time. Now Sactum's auth works like a charm!

@romalytvynenko romalytvynenko merged commit 6a4e32c into dedoc:main Mar 24, 2024
6 checks passed
@layerok layerok deleted the sanctum-cookies branch April 14, 2024 21:26
@erkinboy-botirov
Copy link

In resources/views/docs.blade.php at line 40:

Shouldn't decodeURIComponent be used instead of decodeURI?

It seems like the XSRF-TOKEN that comes as a cookie is base64 encoded. From what I found, an equals sign is added as padding at the end of a base64 encoded string to match a specific number of characters. When this token is passed as a cookie value, it is URL encoded, which turns the equals signs into %3D.

As you can see in the picture, the decodeURI function won't decode it back, but decodeURIComponent will.

Screenshot 2024-05-28 at 10 14 40

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.

auth:sanctum is not detected or documented?
3 participants