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

[WIP] Add AuthenticationProvider implementing OAuth2 code flow #256

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

ghys
Copy link
Member

@ghys ghys commented Apr 2, 2021

This is an early implementation of the VS Code Authentication
Provider API which is able to open regular sessions in the
openHAB instance by authorizing itself with the OAuth2 flow,
similarly to the main UI.

When an API request fails, a "Sign in" button is offered to
perform the initial authorization flow (opening a browser to
allow the user to sign in and open a session), the refresh
token is then stored in the secrets storage (new secrets API),
and reused to get additional access tokens to access the API
on behalf of the user.

It doesn't support yet:

  • PKCE challenges
  • Normal access token management, for instance refreshing it
    automatically before it expires (after 1 hour)
  • Signing out (!)

Signed-off-by: Yannick Schaus github@schaus.net

ghys added 4 commits April 2, 2021 15:34
This is an early implementation of the VS Code Authentication
Provider API which is able to open regular sessions in the
openHAB instance by authorizing itself with the OAuth2 flow,
similarly to the main UI.

When an API request fails, a "Sign in" button is offered to
perform the initial authorization flow (opening a browser to
allow the user to sign in and open a session), the refresh
token is then stored in the keychain (secrets in the extension
context), and reused to get additional access tokens to access
the API on behalf of the user.

It doesn't support yet:

- PKCE challenges
- Normal access token management, for instance refreshing it
  automatically before it expires (after 1 hour)
- Signing out (!)

Signed-off-by: Yannick Schaus <github@schaus.net>
Signed-off-by: Yannick Schaus <github@schaus.net>
Signed-off-by: Yannick Schaus <github@schaus.net>
Signed-off-by: Yannick Schaus <github@schaus.net>
@ghys
Copy link
Member Author

ghys commented Apr 2, 2021

Example:
image

Native dialog from VS Code:

image

image

The sign in page is opened in a browser tab:

image

When clicking sign in the browser may ask if VS Code should open the link:

image

The extension catches the callback, gets the authorization code from it, and exchanges it for a refresh token and access token:

image

The protected API calls now work, no manual token creation necessary:

image

The client ID & refresh token are stored as secrets and they can be retrieved later, so no need to repeat this procedure afterwards.

The VS Code sessions appear in the user's profile page in main UI and can be revoked from there:

image

Signed-off-by: Yannick Schaus <github@schaus.net>
@Confectrician
Copy link
Collaborator

Ah nice.

This was on my todo list after the 1.0.0 release too. 🙂

@Confectrician
Copy link
Collaborator

The sign in page is opened in a browser tab

Maybe we could tweak this to be opened in a vscode WebView too.
It worked already pretty well for the docs in a test i made.

https://community.openhab.org/t/docs-in-vscode-anyone-interested/119774?u=confectrician

@ghys
Copy link
Member Author

ghys commented Apr 3, 2021

Why not, provided the webviews can catch vscode:// URLs too and send them back to VS Code for the UrlHandler to catch.

fyi this code has a lot of code borrowed from the built-in GitHub login. Maybe it would make sense to have an approach as similar as possible to the built-in authenticators.

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

Successfully merging this pull request may close these issues.

None yet

2 participants