Vscode-restclient: [Feature Request] JWT creation

Created on 12 Dec 2018  路  10Comments  路  Source: Huachao/vscode-restclient

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.

feature request

Most helpful comment

Great! I will take a look at adding it.

Thanks.

All 10 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

OzgurDogan picture OzgurDogan  路  13Comments

Odonno picture Odonno  路  22Comments

Fed29 picture Fed29  路  13Comments

johnbeynon picture johnbeynon  路  23Comments

ben-foster-cko picture ben-foster-cko  路  19Comments