AnWeber / vscode-httpyac

Quickly and easily send REST, Soap, GraphQL, GRPC, MQTT and WebSocket requests directly within Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=anweber.vscode-httpyac
MIT License
222 stars 20 forks source link

Support Intellij env file configuration for Oauth2 #275

Closed EiKeHe closed 2 months ago

EiKeHe commented 2 months ago

Hi @AnWeber, first of all thanks a lot for providing the plugin with all its features!

Relatively recently IntelliJ has introduced Oauth2 support for the http-client in IntelliJ Ultimate. The public env file can now be configured according to this documentation: https://www.jetbrains.com/help/idea/oauth-2-0-authorization.html#create-authentication-configuration

I've been trying to get this new configuration to be correctly recognized by httpyac and authorize my requests accordingly but so far have not been successful. My public env.json file is structured like this:

{
  "local": {
    "Security": {
      "Auth": {
        "auth-id": {
          "type": "OAuth2",
          "Grant Type": "Password",
          "Client Credentials": "basic",
          "Client ID": "clientId",
          "Client Secret": "{{clientSecret}}",
          "Username": "{{username}}",
          "Password": "{{password}}",
          "Token URL": "{{tokenUrl}}"
        }
      }
    }
  },
  "tst": {
    "Security": {
      "Auth": {
        "auth-id": {
          "type": "OAuth2",
          "Grant Type": "Password",
          "Client Credentials": "basic",
          "Client ID": "clientId",
          "Client Secret": "{{clientSecret}}",
          "Username": "{{username}}",
          "Password": "{{password}}",
          "Token URL": "{{tokenUrl}}"
        }
      }
    }
  }
}

My private env.json file is structured like this:

{
  "local": {
    "username": "",
    "password": "",
    "baseUrl": "",
    "tokenUrl": "",
    "authUrl": "",
    "clientSecret": ""
  },
  "tst": {
    ...
  }
}

The authorization of my requests is according to the IntelliJ documentation and looks like this:

GET {{baseUrl}}/PATH/API
Authorization: Bearer {{$auth.token("auth-id")}}

When I run my requests like this, I get the following error message: $auth is not defined

Am I missing something in the documentation, that points out how to configure auth with this kind of structure for my env.json files?

If I understand the docs here: https://httpyac.github.io/guide/variables.html#oauth2-openid-connect correctly I would have to change my authorization for each request and change the structure of my private env.json file when switching from IntelliJ to using VS Code with httpyac?

AnWeber commented 2 months ago

@EiKeHe Yes, agree that you should implement the interface. I would have tried it before, but there was no documentation online at the time. But there is now. I'll have to see about installing an Intellij IDE again to extract the current interfaces from the JAR. https://www.jetbrains.com/help/idea/oauth-2-0-authorization.html

EiKeHe commented 2 months ago

Thanks for looking into it, @AnWeber! If there is anything I can help with or more info needed, please let me know!

AnWeber commented 2 months ago

Maybe you can post the Api (.d.ts) of the $auth Object. You can view the Interface by navigating with mouseclick on the object (go to definition). In Intellij Idea some Resource of a http-client-plugin.jar was opened the last time I used this trick.

EiKeHe commented 2 months ago

Unfortunately that trick did not work for me so far but I will try again in the coming days.

I had a look at the jar for the plugin and found the full json schemas for the envs and figured that they may be helpful, so I´m attaching them here. http-client.env-schema.json http-client.private.env-schema.json

AnWeber commented 2 months ago

Almost. I checked my intellij idea installation on my work machine. In jar idea-IU-231.9161.38/plugins/restClient/lib/rest-client.jar is a folder com/intellij/ws/rest/client/stubs/. My installation contains no API for OAuth2. Maybe I need to update my installation.

Found:-) stubs.zip

Location: plugins/restClient/lib/rest-client.jar!com\intellij\ws\rest\client\stubs

But not was I expected

declare const $auth: AuthVariables

interface AuthVariables {
    idToken(authId: string): string

    token(authId: string): string
}
EiKeHe commented 2 months ago

I have been looking at the following version of the rest-client plugin: restClient-241.15989.49, which is the latest AFAIK. It contains the same interface from your last comment. Don´t know if you already updated on your side but this does not seem to change with version number, at least what I found so far. I´m attaching my version of the stubs: stubs_restClient-241.15989.49.zip

AnWeber commented 2 months ago
image

I forgot that Intellij has two different interfaces for HttpClientRequest (request vs response). I don't think I support that properly either

EiKeHe commented 2 months ago

Turns out IntelliJ does not even support it yet in their own CLI tool 😁 https://youtrack.jetbrains.com/issue/IJPL-69639/HTTP-Client-CLI-support-OAuth2-password-and-client-credentials-grant-types

AnWeber commented 2 months ago

Most flows should work, but only tested Password flow, but need to test all flows. I need to understand all properties of the Intellij Auth Config and try to support most of it. And support for ID Token is missing at the moment. So there are still a few areas that need to be improved.

not supported props:

AnWeber commented 2 months ago

I release support for Intellij OAuth2 with v6.13

EiKeHe commented 2 months ago

Thanks a lot for the implementation! I'll be sure to test it ASAP and see if I run into any issues apart from the list of unsupported features!