Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Intellij env file configuration for Oauth2 #275

Closed
EiKeHe opened this issue Apr 17, 2024 · 13 comments
Closed

Support Intellij env file configuration for Oauth2 #275

EiKeHe opened this issue Apr 17, 2024 · 13 comments

Comments

@EiKeHe
Copy link

EiKeHe commented Apr 17, 2024

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
Copy link
Owner

AnWeber commented Apr 19, 2024

@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
Copy link
Author

EiKeHe commented Apr 19, 2024

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

@AnWeber
Copy link
Owner

AnWeber commented Apr 19, 2024

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
Copy link
Author

EiKeHe commented Apr 19, 2024

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
Copy link
Owner

AnWeber commented Apr 20, 2024

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
Copy link
Author

EiKeHe commented Apr 21, 2024

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
Copy link
Owner

AnWeber commented Apr 21, 2024

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

@EiKeHe
Copy link
Author

EiKeHe commented Apr 24, 2024

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
Copy link
Owner

AnWeber commented Apr 27, 2024

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:

  • 'Acquire Automatically'
  • State (always generate random string)
  • PKCE plain (always use SHA-256)
  • Open Complete URI
  • Start Polling After Browser (polling start immediatly)

AnWeber added a commit to AnWeber/httpyac that referenced this issue Apr 27, 2024
@AnWeber
Copy link
Owner

AnWeber commented Apr 28, 2024

I release support for Intellij OAuth2 with v6.13

@AnWeber AnWeber closed this as completed Apr 28, 2024
@EiKeHe
Copy link
Author

EiKeHe commented Apr 29, 2024

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!

@Laurent-PRP
Copy link

@EiKeHe, did you test it with success ? I'm using httpYac 6.14.0 and seems to be have exactly the same error as you initially : $auth is not defined with the same http-client.env.json and .http file as you.

The weird thing is that it seems to detect the authentification file : when I run the query, the authentification process does run. But after I login, it redirect back to my editor and then the error appear.

I also noticed that the Redirect URL that I provide is ignored, and http://localhost:3000 is always used instead.

@EiKeHe
Copy link
Author

EiKeHe commented Jul 12, 2024

@Laurent-PRP, yes, I was able to test it successfully and for me at least working via the httpyac VS code plugin I am still able to successfully send the requests we specified using OAUTH2 and the Intellji HTTP Client env files. Admittedly I am not using any redirect and therefor never tested that case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants