vscode-restclient
vscode-restclient copied to clipboard
[Feature Request] JWT creation
Firstly this is an awesome extension!
It'd be great if it was possible to generate JWTs when making requests.
At my work we use asymmetric JWT tokens for service to service authentication. Due to their short expiry they are a bit of a pain to work with locally - as you effectively need to generate a new token every time you want to work with rest requests.
Are you accepting pull requests? If so I'd be keen to take a look at this - as otherwise this extension is a perfect fit for my use.
Thanks.
@Myrannas PR is warmly welcomed
Great! I will take a look at adding it.
Thanks.
The solution is:
- run login rest api
- you should know your token name, e.g. my-app-token
- and run next rest request like below.
- Example:
# @name login
POST {{hostUrl}}{{apiVersion}}/local_login
Content-Type: application/json
{
"username": "{{username}}",
"password": "{{password}}"
}
###
@jwtoken = {{login.response.body.*}}
# @name get-my-report
GET {{hostUrl}}{{apiVersion}}/reports/my-report
my-app-token: {{jwtoken}}
@snyang the full path of request variable should be {{login.response.body.$}}
@Huachao {{login.response.body.$}}
not working for me. In that case, if I use {{jwtoken}}
, I got an error. Is there a way to print out a field value?
{
"error": "jwt malformed"
}
@snyang can you show me your response of the login
request, including the response headers and body? It will help me to figure out the root cause.
@Huachao, here it is
HTTP/1.1 200 OK
Accept-Ranges: bytes
content-type: text/plain; charset=utf-8
content-length: 196
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
x-download-options: noopen
set-cookie: EGG_SESS=LifKZl9Tv-AmPF3DfcE2L6JyoJy8FSXRjWTKlwgLp8Jzcgnv2c_rXIusXeKFQNtMe_izhXts9Oh4AWiyS5yWyb1lHi1KQTsXF82cnXwhG4H8L6eRcGhpj4MZkRVRzYRpIOXkWHHkIGKZMaWYE2q8HOuhwzvrYIXG5V-i2gfdJjPMXJffDT87HDoc3Cd4GhFTXr-VYurFybRrFUziX6NRXOhduRiFy4vOpGecRi6C9MQ=; path=/; expires=Sat, 23 Nov 2019 15:38:59 GMT; httponly
x-readtime: 138
Date: Fri, 22 Nov 2019 15:38:59 GMT
Connection: close
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVkZDIwNGE3MmU3NGU0ZTIzZGJkN2VjYyIsInVzZXJuYW1lIjoiYWRtaW4iLCJpYXQiOjE1NzQ0MzcxMzksImV4cCI6MTU3NDUyMzUzOX0.UGVu7WIs8Y_iq14KuWs0mRaWv67JcitPY0HFELhP2kM
@snyang since your mime type of your response is text/plain
, you can try to use {{login.response.body.*}}
@Huachao thanks, it works. BTW, the RestClient is an amazing tool.
The solution is:
- run login rest api
- you should know your token name, e.g. my-app-token
- and run next rest request like below.
- Example:
# @name login
POST {{hostUrl}}{{apiVersion}}/local_login
Content-Type: application/json
{
"username": "{{username}}",
"password": "{{password}}"
}
###
@jwtoken = {{login.response.body.*}}
# @name get-my-report
GET {{hostUrl}}{{apiVersion}}/reports/my-report
my-app-token: {{jwtoken}}
This is something I also need, and maybe a simple solution to import one script from another would be nice to create test suites that combine share the common logic of obtaining the token .
So whats happened, was the PR provided? Also looking for that functionality
I use JWT Bearer token.
My login response is this:
`
{ "status": "success", "user": { "id": 73, "name": "ABRAHAM", "created_at": "2023-03-08T03:40:01.000000Z", "updated_at": "2023-03-08T03:40:01.000000Z", }, "authorisation": { "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOi8vbG9jYWxob3N0OjgwMDAvYXBpL2xvZ2luIiwiaWF0IjoxNjkyNTg2ODIyLCJleHAiOjE2OTI1OTQwMjIsIm5iZiI6MTY5MjU4NjgyMiwianRpIjoiQmdqdHM5T05kdFVWT0pvQiIsInN1YiI6IjczIiwicHJ2IjoiMjNiZDVjODk0OWY2MDBhZGIzOWU3MDFjNDAwODcyZGI3YTU5NzZmNyIsIm5hbWUiOiJBQlJBQU8iLCJ1c2VybmFtZSI6IjAzMDE5OTQxNDU4IiwiaWQiOjczfQ.xM-LF4V11uxBBfXKuDRX1KaHnpFM9N0G_x8r91YETOQ", "type": "bearer" }, }
` What worked for me was this:
`POST http://localhost:8000/api/login Content-Type: application/json
{ "username": "myusername", "password": "mypassword" }
@jwtoken = {{login.response.body.*.token}}
test sending the token GET http://localhost:8000/api/vehicles Authorization: Bearer {{jwtoken}}`