Swagger-Net and Bearer Token Authorization - asp.net-web-api

We are using Swagger-Net with Bearer Token Authorization as follows:
c.ApiKey("Authorization", "header", "Bearer Token Authentication");
The request needs to be sent in the format "Bearer [bearertoken]", and I have verified this does work correctly when pasted in this format into the "API Key" authorization textbox. The Curl shows it being passed in the header as -H "Authorization: Bearer [token] and the API request succeeds.
Is there any way to automatically send a known token along with every API request? Or asked another way, is there any way to pre-fill this dynamically created API Key textbox with a known string to simplify the process for the end using testing our API?

Related

Insomnia configure OAuth 2.0 grant_type password access token for other API calls

I am trying to embed OAuth 2.0 grant_type password access token to my insomnia client API calls but getting "Failed to fetch token url= state us=0"
When we try below curl command its returning the access token. each time we have to copy access token and add it as header in insomnia rest call (Their is no client secret as it not confidential).
curl --request POST \
--url https://mydomain/auth/realms/my-app/protocol/openid-connect/token \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data client_id=myclientid \
--data username=app.admin \
--data 'password=password!' \
--data grant_type=password
Above curl if you create in insomnia client as POST request that is also working only thing is we need to copy and paste as header in other API calls
NOTE: the same is working in postman rest client through pre-request script but I want it to be achieved in insomnia client
After doing lot of research found out the solution by myself
follow this blog https://www.ankursheel.com/blog/automatically-set-access-token-authenticated-requests-insomnia
You need to create separate Generate token request, and set the bearer token in the API request where you want to embed Auth token automatically, click on the Bearer tab and enter Response ⇒ Body Attribute for the token and follow those instruction it worked for me :)

Inspecting bearer token

I have some challenges authentication to WEB API so I started a fiddler to see what going on. I got a bearer token but I am suspecting the framework is not attaching the token to the request.
I cant see any bearer token in the header, is it correct to assume that the bearer token should have been in the header of the request, or can bearer token be added otherwise?
is it correct to assume that the bearer token should have been in the header of the request
Yes, that's exactly what the response tells you:
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer
This means your client should send a Authorization: Bearer $token request header.

How to develop test-automation using Postman when OAuth 2.0 authorization is required

I have an ASP.NET Web API 2 which is using OAuth 2.0 for authorization. And let's imagine I have a simple Web API method, like:
GET: http://host/api/profiles/user123 (requires OAuth 2.0 token)
So, with Postman, it is easy to test this Web API. I get an OAuth token for user123 from the Web API OAuthAuthorization method and then I use that token in the header of the HTTP request:
GET /api/profiles/user123 HTTP/1.1
Host: {host}
Authorization: Bearer {Token}
Content-Type: application/json
Cache-Control: no-cache
However, if I save my test and run it later (either by Postman itself or by Newman), the token will be expired at that time and it won't work.
How can I make Newman to get a new token automatically for user123 and use it in the HTTP request?
Note: I know how to use Postman's Authentication helpers to ask for a new token. But this scenario doesn't fit the test-automation. In test-automation, I want to remove any human interaction.
It's simple, get your access token at run time and save it into environment variable. Then use it in your next Get request.
In Get Token request, do this in Tests sections:
var body = JSON.parse(responseBody);
pm.environment.set('AccessToken', body.access_token);
In your main Get request, you can use the environment variable in Authorization header:
Authorization: Bearer {{AccessToken}}
Hope this helps.

Is it possible to convert Token from request string to Header in jwt authentication?

I'm using Tymon\JWTAuth with laravel
and I'm sending the requests like this
www.example.com/test?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjUzNywiaXNzIjoiaHR0cDpcL1wvbG9jYWxob3N0Ojg4ODhcL3YyXC9zZWxmXC9hdXRoZW50aWNhdGUiLCJpYXQiOjE0NzE0MzI0ODMsImV4cCI6MTQ3MTQ3NTY4MywibmJmIjoxNDcxNDMyNDgzLCJqdGkiOiI4ZDVlZGE4MmE2MTZlMzM5NjgwMmFmZTk5NWI3N2Q1MCJ9.-t0El5nJj_pgNzpgtLy8EVLUsf9dp8RTLhWA3cK_Vmw
I want to move this token to header is that possible ?
Yes. You can send the token with header.
From the documentation:
To make authenticated requests via http using the built in methods, you will need to set an authorization header as follows:
Authorization: Bearer {yourtokenhere}

Google Documents List API - problem retrieving a list of documents

I'd like to get a list of my documents from google docs api. First I get a authValue using ClientLogin, then I send a request to that url https://docs.google.com/feeds/documents/private/full with header set to "Authorization: GoogleLogin auth=authValue", but I always get a response "Authorization required". What should I do?
Make sure you request the token with the "writely" service when performing ClientLogin. Also, make sure that you're sending the token provided after Auth= in the ClientLogin response.
Authorization: GoogleLogin auth=DQAAA...

Resources