-
-
Notifications
You must be signed in to change notification settings - Fork 396
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
oauth2 + passport = Bearer <token> #57
Comments
It looks like Swagger UI not supporting this: |
An way around this issue is specifying the auth as bellow:
On the controller, add this:
Then you create a
And then declare it on your
Now lets wait for Swagger v3 that is said to have oAuth2 support... |
Thanks @joaoBeno! |
Yes these variables are defined in config and passed into the view here:
https://github.com/DarkaOnLine/L5-Swagger/blob/master/src/Http/Controllers/
SwaggerController.php#L63-L71
…On 23 March 2017 at 14:29, ratanakpek ***@***.***> wrote:
Hi! Everyone! I need help!
I am trying to install L5-swagger, so i follow the command from this
L5-swagger <https://github.com/DarkaOnLine/L5-Swagger> but I got errored
everytime that I try to open this view that show the swagger. It said that
undefined variable: $urlToDocs, $highlightThreshold,... and another
variable. I commented the variable error already...
for my question is that "How can I give the value to these variable?" in
config/l5-swagger file?
`<?php
if (app()->environment() != 'testing') {
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With");
}
?>
<title>{{config('l5-swagger.api.title')}}</title> <script
src='{{config('l5-swagger.paths.assets_public')}}/lib/object-assign-pollyfill.js'
type='text/javascript'></script> <script src='{{config('l5-swagger.path
s.assets_public')}}/lib/jquery-1.8.0.min.js'
type='text/javascript'></script> <script src='{{config('l5-swagger.path
s.assets_public')}}/lib/jquery.slideto.min.js'
type='text/javascript'></script> <script src='{{config('l5-swagger.path
s.assets_public')}}/lib/jquery.wiggle.min.js'
type='text/javascript'></script> <script src='{{config('l5-swagger.path
s.assets_public')}}/lib/jquery.ba-bbq.min.js'
type='text/javascript'></script> <script src='{{config('l5-swagger.path
s.assets_public')}}/lib/handlebars-2.0.0.js'
type='text/javascript'></script> <script src='{{config('l5-swagger.path
s.assets_public')}}/lib/lodash.min.js' type='text/javascript'></script>
<script src='{{config('l5-swagger.paths.assets_public')}}/lib/backbone-min.js'
type='text/javascript'></script> <script src='{{config('l5-swagger.path
s.assets_public')}}/swagger-ui.min.js' type='text/javascript'></script>
<script src='{{config('l5-swagger.paths.assets_public')}}/lib/highlight.9.1.0.pack.js'
type='text/javascript'></script> <script src='{{config('l5-swagger.path
s.assets_public')}}/lib/highlight.9.1.0.pack_extended.js'
type='text/javascript'></script> <script src='{{config('l5-swagger.path
s.assets_public')}}/lib/jsoneditor.min.js' type='text/javascript'></script>
<script src='{{config('l5-swagger.paths.assets_public')}}/lib/marked.js'
type='text/javascript'></script> <script src='{{config('l5-swagger.path
s.assets_public')}}/lib/swagger-oauth.js' type='text/javascript'></script>
<!-- Some basic translations -->
<!-- <script src='lang/translator.js' type='text/javascript'></script> -->
<!-- <script src='lang/ru.js' type='text/javascript'></script> -->
<!-- <script src='lang/en.js' type='text/javascript'></script> -->
<script type="text/javascript">
$(function () {
var url = window.location.search.match(/url=([^&]+)/);
if (url && url.length > 1) {
url = decodeURIComponent(url[1]);
} else {
{{--url = "{!! $urlToDocs !!}";--}}
url ="http://petstore.swagger.io/v2/swagger.json";
}
**{{--hljs.configure({--}}
{{--highlightSizeThreshold: {{ $highlightThreshold }}--}}
{{--});--}}**
// Pre load translate...
if(window.SwaggerTranslator) {
window.SwaggerTranslator.translate();
}
window.swaggerUi = new SwaggerUi({
url: url,
dom_id: "swagger-ui-container",
@if(array_key_exists('validatorUrl', get_defined_vars()))
// This differentiates between a null value and an undefined variable
validatorUrl: {!! isset($validatorUrl) ? '"' . $validatorUrl . '"' : 'null' !!},
@endif
supportedSubmitMethods: ['get', 'post', 'put', 'delete', 'patch'],
onComplete: function(swaggerApi, swaggerUi){
@if(isset($requestHeaders))
@foreach($requestHeaders as $requestKey => $requestValue)
window.swaggerUi.api.clientAuthorizations.add("{{$requestKey}}", new SwaggerClient.ApiKeyAuthorization("{{$requestKey}}", "{{$requestValue}}", "header"));
@Endforeach
@endif
if(typeof initOAuth == "function") {
initOAuth({
clientId: "your-client-id",
clientSecret: "your-client-secret-if-required",
realm: "your-realms",
appName: "your-app-name",
scopeSeparator: ",",
additionalQueryStringParams: {}
});
}
if(window.SwaggerTranslator) {
window.SwaggerTranslator.translate();
}
},
onFailure: function(data) {
console.log("Unable to Load SwaggerUI");
},
docExpansion: {!! isset($docExpansion) ? '"' . $docExpansion . '"' : '"none"' !!},
jsonEditor: false,
apisSorter: "alpha",
defaultModelRendering: 'schema',
showRequestHeaders: false
});
function addApiKeyAuthorization(){
var key = $('#input_apiKey')[0].value;
{{--if ("{{$apiKeyInject}}" === "query") {--}}
{{--key = encodeURIComponent(key);--}}
{{--}--}}
{{--if(key && key.trim() != "") {--}}
{{--var apiKeyAuth = new SwaggerClient.ApiKeyAuthorization("{{$apiKeyVar}}", key, "{{$apiKeyInject}}");--}}
{{--window.swaggerUi.api.clientAuthorizations.add("{{$securityDefinition}}", apiKeyAuth);--}}
{{--}--}}
}
$('#input_apiKey').change(function() {
addApiKeyAuthorization();
});
window.swaggerUi.load();
// if you have an apiKey you would like to pre-populate on the page for demonstration purposes
// just put it in the .env file, API_AUTH_TOKEN variable
***@***.***($apiKey)--}}
{{--$('#input_apiKey').val("{{$apiKey}}");--}}
{{--addApiKeyAuthorization();--}}
***@***.***}}
});
</script>
swagger <http://swagger.io>
Explore <#m_-324956319607622765_m_1126979693123509463_>
`
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#57 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABHg8mFYOzSjtVn0OT3DAk7PCDUvZjBtks5romWpgaJpZM4MJGgj>
.
|
Thanks Bro! |
where does the middleware should be called? |
@pamaleona-navagis if you place this on your
It will run on all requests... If they don't have the bearer header, it will get it from the DB and append it to the request, before Passport handle the request... Ps.: you need to past just the "swfix" line as the first item of the |
I tried putting it on the forst line on the route middleware but
unfortunately it doesn't work.
…On Nov 9, 2017 7:40 AM, "joaoBeno" ***@***.***> wrote:
@pamaleona-navagis <https://github.com/pamaleona-navagis> if you place
this on your Kernel.php:
// I placed it first so it runs before passport's middleware...
protected $routeMiddleware = [
'swfix' => \App\Http\Middleware\SwaggerFix::class,
]
It will run on all requests... If they don't have the bearer header, it
will get it from the DB and append it to the request, before Passport
handle the request...
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#57 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AdjxKfYXGFNdod8AJ6W6meT7xE6lfk61ks5s0jwCgaJpZM4MJGgj>
.
|
@pamaleona-navagis, please post your kernel.php on a gist, and post the link here, so I can give you more support without notifying other people... 👍 |
Here's my Kernel.php
https://gist.github.com/pamaleona-navagis/5010be66f1594fb98cea17d3098605af
…On Thu, Nov 9, 2017 at 5:52 PM, joaoBeno ***@***.***> wrote:
@pamaleona-navagis <https://github.com/pamaleona-navagis>, please post
your kernel.php on a gist, and post the link here, so I can give you more
support without notifying other people... 👍
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#57 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AdjxKRGa0jd9Pkd_FwJnwVlgObdR10Ljks5s0stkgaJpZM4MJGgj>
.
|
Sharing my solution in case it might help. This is I how made it to work with Passport using password grant. No need for middleware since latest version uses Swagger UI v3.
'passport' => [ // Unique name of security
'type' => 'oauth2', // The type of the security scheme. Valid values are "basic", "apiKey" or "oauth2".
'description' => 'Laravel passport oauth2 security.',
'flow' => 'password', // The flow used by the OAuth2 security scheme. Valid values are "implicit", "password", "application" or "accessCode".
'tokenUrl' => config('app.url') . '/oauth/token', // The authorization URL to be used for (password/application/accessCode)
'scopes' => []
],
* @SWG\SecurityScheme(
* securityDefinition="passport",
* type="oauth2",
* tokenUrl="/oauth/token",
* flow="password",
* scopes={}
* )
* @SWG\Get(
* path="/api/user",
* tags={"user"},
* security={
* {"passport": {}},
* },
* summary="Get user",
* @SWG\Response(
* response=200,
* description="Logged in user info"
* )
* )
Request |
@yajra thanks for sharing. But I think steps 1 and 2 duplicate each other. You need to use only one of them. Because security definitions in the config file will be generated and appended to the final swagger documentation json file: https://github.com/DarkaOnLine/L5-Swagger/blob/master/src/Generator.php#L46 |
@DarkaOnLine thanks for pointing that out. Will update my answer and my code. 👍 |
Is there a way to keep the user login even if I reload the Swagger UI? It's loosing the authentication |
@yajra is possible to set default values for |
@DarkaOnLine joining to last questions |
Please see @joseph-montanez suggestions here: #120 |
@yajra But when we add security to any Api then should not send response without authorization |
this is what I did after reading the API doc and it worked for me * @oas\SecurityScheme(
////////////////////////////////////// security={
|
Passport annotation is good but when you have your own middleware then how about setting it to the interceptor and adding respective middlewares to the l5-swagger config file? my-project/resources/views/vendor/l5-swagger/index.blade.php
|
With latest version of l5-swagger: Follow the Passport Instalation/Configuration l5-swagger.php
In your secured controller:
And done... Should be work. |
I followed @oyepez003 and used default passport setting from l5-swagger.php file but don't see password in popup? How can I fix my problem? |
Hi,
This is my route:
My RouteServiceProvider
In Controller.php add
And ShippingController
kernel.php
Where are the error? Some idea @oyepez003 , @yajra @kranthi610, etc? |
@ssheduardo Check the passport option in the config/l5-swagger.php
|
@oyepez003 |
@OA\SecurityScheme(
this should help
|
After added the changes, this is the response And this
Why not set the Bearer???
|
Check the security scheme...I'm using bearer scheme.this is how my security definiton looks
*security={
*) Lemme know if this helps |
Where put this code?
Please attached a screenshot. |
Top of your controller ..Take a look on this pet controller example |
Perfect, I got it!
Thanks you @kranthi610 |
|
Why not anyone create a tutorial this? |
Is there a way to keep the user logged in, after refreshing Swagger UI page? |
I reply to myself: yes, there is. Found this: #120 (comment) |
@Doublefree9
You can use this
The apiKey is the Bearer token. You build a login api, copy the token into "apiKey" with "Bearer THE TOKEN U COPY" |
Adding a full example for a get request: In top of your main controller add this: /**
On top of your get request add this :
Here is a sample of User schema, add it on top of your model. /**
|
If you notice your requests just time out with Laravel passport, then make sure you have this in your controller methods (requests) tags after setting up Laravel Passport as a security type in your
Initially I was using this (without the asterisk):
|
I cant get this to work... I have this in my Controller.php
Now I have this on my AddressesController.php:
This is the cURL:
It works without a problem in Postman... Everytime I hit that endpoint I get a 401 Unauthorised. What am I doing wrong? How can I pass the "Accept" header? How can I take the "X-CSRF-TOKEN:" out? Thanks in advance |
Change your security definition to this and try
|
@kranthi610 Still doesnt work... |
share me the swagger UI for API |
I was because of the "Accept" header. Please I need to know how to change the "Accept" header? It is everytime And I also need to know how to take out the "X-CSRF-TOKEN" header?? It is a shame, that I cant find any documentation for this 2 things. @DarkaOnLine Could you please help here?? Thank you in advance |
Mine looks like this.. in my Adress controller... *@OA\Response(
@OA\MediaType(
*),
*),
*),
*) this is how you do in base controller /**
|
I already said that I got it to work, but I had to change a Middleware where I only accept the accept header of application/json. It seems there is a problem with the request headers. It sends |
Hi dude, i was the same problem. Just deleting the interceptor function it works to me. Good luck!.
|
Simple solution guys. go to index.blade.php file and find method requestInterceptor(); and replace with below code.
no need to create any middleware. securityScheme Passport in Your l5-swagger.php file should be like this
|
"Authorization" : "Bearer token213315454Sample"
Is it possible to handle this kind o header in "documentation-swagger"?
If yes, how?
The text was updated successfully, but these errors were encountered: