Overview
VS Code’s Authentication API supports:- Multiple authentication providers (GitHub, Microsoft, custom providers)
- OAuth and OpenID Connect flows
- Secure token management
- Account switching and multiple sessions
- Silent authentication flows
Authentication Namespace
Thevscode.authentication namespace provides methods for working with authentication:
Core Interfaces
AuthenticationSession
Represents an authentication session with an access token and associated account information:AuthenticationSession Interface
AuthenticationSession Interface
AuthenticationSessionAccountInformation
Information about the account associated with a session:Getting Authentication Sessions
Useauthentication.getSession() to request an authentication session from a provider.
Basic Usage
Authentication Options
createIfNone
createIfNone
Whether login should be performed if there is no matching session. If
true, a modal dialog will be shown. If false, a numbered badge will be shown on the accounts menu.forceNewSession
forceNewSession
Whether to reauthenticate even if there is already a session available. Useful for scenarios where the token needs to be re-minted.
silent
silent
Whether to show the indication to sign in. If
true, no UI will be shown.clearSessionPreference
clearSessionPreference
Whether to clear the existing session preference. Useful when you want the user to be able to choose a different account.
Using Authentication in Extensions
Example: GitHub API Access
Example: Microsoft Graph API
Handling Session Changes
Listen to session changes to update your extension’s state:Creating Custom Authentication Providers
Extensions can register custom authentication providers to support custom OAuth flows or enterprise authentication systems.AuthenticationProvider Interface
AuthenticationProvider Interface
AuthenticationProvider Interface
Registering a Provider
Authentication Scopes
Different providers support different scopes:GitHub Scopes
Common GitHub scopes:repo- Full control of private repositoriesuser- Read/write access to profile infouser:email- Access to email addressesread:user- Read access to profile infogist- Create gistsworkflow- Update GitHub Actions workflows
Microsoft Scopes
Common Microsoft Graph scopes:User.Read- Read user profileUser.ReadWrite- Read and write user profileMail.Read- Read user mailMail.Send- Send mail as the userFiles.Read- Read user files
Best Practices
Request Minimal Scopes
Request Minimal Scopes
Only request the scopes your extension actually needs. Users are more likely to grant narrower permissions.
Handle Authentication Failures Gracefully
Handle Authentication Failures Gracefully
Users may deny authentication or close the dialog. Always check for undefined sessions.
Use Silent Mode for Background Operations
Use Silent Mode for Background Operations
When checking for existing sessions in the background, use silent mode to avoid disturbing the user.
Cache Sessions
Cache Sessions
Cache the session in your extension to avoid repeated authentication calls.
Security Considerations
Related APIs
Commands API
Register commands for authentication actions
Workspace API
Store authentication state in workspace settings