Redirect to IDE from browser

Hi,

I'm building an extension which utilizes the PKCE OAuth flow. This means I need to launch out of Intellij and into a browser for sign-on activities, and then redirect back into the IDE to complete the sign-on.

I am able to complete the first step without any issue.

For the second step I need to redirect back from the browser to the IDE - currently I have created a RestService and registered this in the plugin.xml. When I install the plugin manually, I can hit the endpoint. The intension was to call this endpoint as part of the PKCE redirect URL.

However, I would like to redirect back to and regain focus with the IDE (specifically the window that originally launched the browser).

In VSCode this is possible by prefixing the redirect URL with vscode://urlhandleruri and registering a URI handler with a specific authority (the authority is used to determine which instance of VSCode to redirect into). 

The browser uses the vscode:// prefix to launch the application, and then the application uses the provided authority to determine which IDE window to focus on.

Can this be recreated in Intellij somehow? Can I launch Intellij from the URL bar within the browser using something like jetbrains://idea/...?

Thanks,

David

2 comments
Comment actions Permalink

Hi,

I want to provide an update on this as I continue with my investigations on this flow.

I was able to trigger a redirect from Chrome back to Intellij using the following as the redirect URL (I got some ideas from here -  https://youtrack.jetbrains.com/issue/IDEA-65879), but the endpoint isn't being executed as I'd hoped.

idea://open?url=https://127.0.0.1:63340/api/intellij/pkce/redirect

I can hit my endpoint from postman by issuing a GET request directly to:

https://127.0.0.1:63340/api/intellij/pkce/redirect

However I was hoping that by providing the URL in the following format that Intellij would call my endpoint, but it does not unfortunately.

idea://open?url=https://127.0.0.1:63340/api/intellij/pkce/redirect

Could you help me understand if it is possible to hit the endpoint in this way? My RestService implementation is as follows:

public class BrowserRedirectListener extends RestService {

@NotNull
@Override
protected String getServiceName() {
return "intellij/pkce/redirect";
}

@Override
protected boolean isMethodSupported(@NotNull HttpMethod httpMethod) {
return httpMethod == HttpMethod.GET;
}

@Nullable
@Override
public String execute(@NotNull QueryStringDecoder queryStringDecoder, @NotNull FullHttpRequest fullHttpRequest, @NotNull ChannelHandlerContext channelHandlerContext) {
notifyEvent("Redirect Successful.");
sendOk(fullHttpRequest, channelHandlerContext);
return "Redirect Completed Successfully!";
}

@Override
protected boolean isHostTrusted(@NotNull FullHttpRequest request) {
return true;
}

@Override
protected boolean isHostTrusted(@NotNull FullHttpRequest request, @NotNull QueryStringDecoder queryStringDecoder) {
return true;
}

private void notifyEvent(String message) {
Notifications.Bus.notify(new Notification("redirect", "Redirect notification", message, NotificationType.INFORMATION));
}
}

If this is not possible, would it be possible to focus on the Intellij application from within the RestService implementation? So when we call the endpoint at /api/intellij/pkce/redirect we can focus the correct Intellij window to the user (emulating the redirect behavior I'm trying to achieve using idea:// syntax?

Appreciate your help,

David

0
Comment actions Permalink

I found a way to get this to work using Jetbrains Toolbox and by providing an implementation of JBProtocolCommand and registering it in plugin.xml. Using toolbox, I can call my protocol handler using the following and retrieve the query parameters from the browser redirect:

jetbrains://idea/pkce/redirect

However this is a little clunky because as an Enterprise, we have tight control over what versions of Jetbrains are installed and available to us. Therefore the functionality provided by toolbox are mostly not required, and it would be preferred that we didn't have to install it to use the protocol handler.

Can a protocol handler implementation be registered and invoked without flowing through Toolbox? I would need control over what version of Intellij is opened (Intellij Ultimate vs Intellij CE) as different developers use different versions of Intellij in my company, so ideally there would be different protocol handlers within the same plugin that could handle requests depending on which version of Intellij they have installed.

Thanks, David

 

 

0

Please sign in to leave a comment.