vscode-restclient icon indicating copy to clipboard operation
vscode-restclient copied to clipboard

[Feature Request] JWT creation

Open Myrannas opened this issue 6 years ago • 12 comments

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 avatar Dec 12 '18 05:12 Myrannas

@Myrannas PR is warmly welcomed

Huachao avatar Dec 12 '18 05:12 Huachao

Great! I will take a look at adding it.

Thanks.

Myrannas avatar Dec 12 '18 05:12 Myrannas

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 avatar Nov 17 '19 15:11 snyang

@snyang the full path of request variable should be {{login.response.body.$}}

Huachao avatar Nov 18 '19 03:11 Huachao

@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 avatar Nov 21 '19 05:11 snyang

@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 avatar Nov 21 '19 07:11 Huachao

@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 avatar Nov 22 '19 15:11 snyang

@snyang since your mime type of your response is text/plain, you can try to use {{login.response.body.*}}

Huachao avatar Nov 25 '19 02:11 Huachao

@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}}

snyang avatar Nov 30 '19 02:11 snyang

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 .

luistrigueiros avatar Apr 25 '20 08:04 luistrigueiros

So whats happened, was the PR provided? Also looking for that functionality

kirhgoff avatar May 30 '21 07:05 kirhgoff

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}}`

abrahampe avatar Aug 21 '23 03:08 abrahampe