|
| 1 | +# MSAL Java - Copilot Instructions |
| 2 | + |
| 3 | +## Quick Reference |
| 4 | + |
| 5 | +- **Main Module**: `msal4j-sdk/` (focus here for most work) |
| 6 | +- **Main Package**: `com.microsoft.aad.msal4j` |
| 7 | +- **Three Application Types**: `PublicClientApplication`, `ConfidentialClientApplication`, `ManagedIdentityApplication` |
| 8 | +- **Pattern**: Each auth flow has `*Parameters` (public API), `*Request` (internal), `*Supplier` (executor) |
| 9 | +- **Current Version**: 1.23.1 |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## Project Overview |
| 14 | + |
| 15 | +**Microsoft Authentication Library for Java (MSAL4J)** is a library that enables applications to integrate with the Microsoft identity platform. It provides APIs for acquiring security tokens from Azure Active Directory (Azure AD), Microsoft Accounts, and Azure AD B2C. |
| 16 | + |
| 17 | +- **Language**: Java 8+ |
| 18 | +- **Build Tool**: Maven |
| 19 | +- **Current Version**: 1.23.1 |
| 20 | +- **Artifact**: `com.microsoft.azure:msal4j` |
| 21 | +- **Key Protocols**: OAuth2, OpenID Connect |
| 22 | + |
| 23 | +### Core Capabilities |
| 24 | +- Sign in users with Microsoft identities |
| 25 | +- Acquire access tokens for protected APIs (Microsoft Graph, custom APIs) |
| 26 | +- Token caching and automatic refresh |
| 27 | +- Support for various authentication flows (interactive, silent, client credentials, on-behalf-of, device code, managed identity) |
| 28 | +- Multi-cloud and B2C support |
| 29 | + |
| 30 | +### Repository Structure |
| 31 | +This repository contains three Maven modules: |
| 32 | +- **`msal4j-sdk/`** - The main MSAL Java library (focus of development) |
| 33 | +- **`msal4j-brokers/`** - Broker integration for native authentication (Windows WAM) |
| 34 | +- **`msal4j-persistence-extension/`** - Cross-platform token cache persistence helpers |
| 35 | + |
| 36 | +For most work, focus on **`msal4j-sdk/`**. |
| 37 | + |
| 38 | +--- |
| 39 | + |
| 40 | +## Architecture & Structure |
| 41 | + |
| 42 | +### Application Hierarchy |
| 43 | + |
| 44 | +MSAL4J provides three main application types, all extending from a common base: |
| 45 | + |
| 46 | +``` |
| 47 | +AbstractApplicationBase (base for all applications) |
| 48 | +├── AbstractClientApplicationBase (base for client applications) |
| 49 | +│ ├── PublicClientApplication (desktop, mobile apps) |
| 50 | +│ └── ConfidentialClientApplication (web apps, web APIs, daemons) |
| 51 | +└── ManagedIdentityApplication (Azure managed identity) |
| 52 | +``` |
| 53 | + |
| 54 | +**Key Classes:** |
| 55 | +- `PublicClientApplication` - For apps that cannot securely store secrets (desktop/mobile). Supports interactive, device code, integrated Windows auth flows. |
| 56 | +- `ConfidentialClientApplication` - For apps that can securely store secrets (web apps, APIs, daemons). Supports client credentials and on-behalf-of flows. |
| 57 | +- `ManagedIdentityApplication` - For Azure resources using managed identities (VMs, App Service, Functions, etc.) |
| 58 | + |
| 59 | +### Important Directories |
| 60 | + |
| 61 | +**Main source code:** |
| 62 | +- `msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/` - All library code |
| 63 | + - Core application classes: `PublicClientApplication`, `ConfidentialClientApplication`, `ManagedIdentityApplication` |
| 64 | + - Token cache: `TokenCache`, `TokenCacheAccessContext`, cache entity classes |
| 65 | + - Authentication flows: `*Request.java`, `*Parameters.java`, `*Supplier.java` classes |
| 66 | + - Authority handling: `Authority`, `AADAuthority`, `B2CAuthority`, `ADFSAuthority` |
| 67 | + - Token request execution: `TokenRequestExecutor`, `OAuthHttpRequest` |
| 68 | + - HTTP layer: `HttpHelper`, `DefaultHttpClient`, `IHttpClient` |
| 69 | + - Exceptions: `MsalException` and subclasses |
| 70 | + |
| 71 | +**Test code:** |
| 72 | +- `msal4j-sdk/src/test/java/` - Unit tests |
| 73 | +- `msal4j-sdk/src/integrationtest/java/` - Integration tests (require test infrastructure) |
| 74 | + |
| 75 | +**Samples:** |
| 76 | +- `msal4j-sdk/src/samples/` - Example applications demonstrating various scenarios |
| 77 | + |
| 78 | +### Key Architectural Patterns |
| 79 | + |
| 80 | +**Builder Pattern**: All application types use fluent builders (e.g., `PublicClientApplication.builder(clientId).authority(...).build()`) |
| 81 | + |
| 82 | +**Request/Parameters Pattern**: Each token acquisition flow has: |
| 83 | +- A `*Parameters` class (e.g., `InteractiveRequestParameters`) - User-facing API |
| 84 | +- A `*Request` class (e.g., `InteractiveRequest`) - Internal request representation |
| 85 | +- A `*Supplier` class (e.g., `AcquireTokenByInteractiveFlowSupplier`) - Executes the flow |
| 86 | + |
| 87 | +**Token Flow Execution**: |
| 88 | +1. Application receives `*Parameters` from developer |
| 89 | +2. Converts to internal `MsalRequest` (via `*Request` classes) |
| 90 | +3. Routes to appropriate `AuthenticationResultSupplier` implementation |
| 91 | +4. Supplier checks cache, executes HTTP requests via `TokenRequestExecutor` |
| 92 | +5. Returns `AuthenticationResult` with tokens and account info |
| 93 | + |
| 94 | +**Token Cache**: |
| 95 | +- In-memory cache with five entity types: `AccessTokenCacheEntity`, `RefreshTokenCacheEntity`, `IdTokenCacheEntity`, `AccountCacheEntity`, `AppMetadataCacheEntity` |
| 96 | +- Implements `ITokenCache` with serialization hooks via `ITokenCacheAccessAspect` |
| 97 | +- Thread-safe with read-write locks |
| 98 | + |
| 99 | +**Service Bundle**: Internal `ServiceBundle` class provides shared services (HTTP client, telemetry, executor service) to all requests |
| 100 | + |
| 101 | +--- |
| 102 | + |
| 103 | +## Authentication Flows & Public APIs |
| 104 | + |
| 105 | +MSAL4J supports multiple authentication flows, each with a public `*Parameters` class (used by developers) and an internal `*Supplier` class (executes the flow logic). Each flow is available only on specific application types. |
| 106 | + |
| 107 | +### Public Client Flows (PublicClientApplication) |
| 108 | + |
| 109 | +**Interactive Flow** |
| 110 | +- **Public API**: `acquireToken(InteractiveRequestParameters)` |
| 111 | +- **Parameters**: `InteractiveRequestParameters` - Opens system browser for user authentication |
| 112 | +- **Internal**: `InteractiveRequest` → `AcquireTokenByInteractiveFlowSupplier` |
| 113 | +- **Key Classes**: `HttpListener`, `AuthorizationResponseHandler`, `SystemBrowserOptions` |
| 114 | + |
| 115 | +**Device Code Flow** |
| 116 | +- **Public API**: `acquireToken(DeviceCodeFlowParameters)` |
| 117 | +- **Parameters**: `DeviceCodeFlowParameters` - For devices without a browser (IoT, CLI tools) |
| 118 | +- **Internal**: `DeviceCodeFlowRequest` → `AcquireTokenByDeviceCodeFlowSupplier` |
| 119 | +- **Key Classes**: `DeviceCode`, device code consumer callback |
| 120 | + |
| 121 | +**Username/Password (Deprecated)** |
| 122 | +- **Public API**: `acquireToken(UserNamePasswordParameters)` |
| 123 | +- **Parameters**: `UserNamePasswordParameters` - Resource Owner Password Credentials (ROPC) flow |
| 124 | +- **Internal**: `UserNamePasswordRequest` → `AcquireTokenByAuthorizationGrantSupplier` |
| 125 | +- **Note**: Deprecated and insecure, will be removed in future versions |
| 126 | + |
| 127 | +**Integrated Windows Authentication** |
| 128 | +- **Public API**: `acquireToken(IntegratedWindowsAuthenticationParameters)` |
| 129 | +- **Parameters**: `IntegratedWindowsAuthenticationParameters` - Uses Windows authentication |
| 130 | +- **Internal**: `IntegratedWindowsAuthenticationRequest` → `AcquireTokenByAuthorizationGrantSupplier` |
| 131 | +- **Key Classes**: `WSTrustRequest`, `WSTrustResponse` |
| 132 | + |
| 133 | +### Confidential Client Flows (ConfidentialClientApplication) |
| 134 | + |
| 135 | +**Client Credentials** |
| 136 | +- **Public API**: `acquireToken(ClientCredentialParameters)` |
| 137 | +- **Parameters**: `ClientCredentialParameters` - App-only authentication (daemon apps) |
| 138 | +- **Internal**: `ClientCredentialRequest` → `AcquireTokenByClientCredentialSupplier` |
| 139 | +- **Key Classes**: `IClientCredential`, `ClientSecret`, `ClientCertificate`, `ClientAssertion` |
| 140 | + |
| 141 | +**On-Behalf-Of (OBO)** |
| 142 | +- **Public API**: `acquireToken(OnBehalfOfParameters)` |
| 143 | +- **Parameters**: `OnBehalfOfParameters` - Middle-tier service calls downstream API on behalf of user |
| 144 | +- **Internal**: `OnBehalfOfRequest` → `AcquireTokenByOnBehalfOfSupplier` |
| 145 | +- **Key Classes**: `UserAssertion` (incoming access token from user) |
| 146 | + |
| 147 | +### Managed Identity Flow (ManagedIdentityApplication) |
| 148 | + |
| 149 | +**Managed Identity** |
| 150 | +- **Public API**: `acquireTokenForManagedIdentity(ManagedIdentityParameters)` |
| 151 | +- **Parameters**: `ManagedIdentityParameters` - For Azure resources (VMs, App Service, Functions) |
| 152 | +- **Internal**: `ManagedIdentityRequest` → `AcquireTokenByManagedIdentitySupplier` |
| 153 | +- **Key Classes**: `ManagedIdentitySource` implementations (`IMDSManagedIdentitySource`, `AppServiceManagedIdentitySource`, etc.) |
| 154 | + |
| 155 | +### Common Flows (All Application Types) |
| 156 | + |
| 157 | +**Authorization Code** |
| 158 | +- **Public API**: `acquireToken(AuthorizationCodeParameters)` |
| 159 | +- **Parameters**: `AuthorizationCodeParameters` - Exchanges authorization code for tokens |
| 160 | +- **Internal**: `AuthorizationCodeRequest` → `AcquireTokenByAuthorizationGrantSupplier` |
| 161 | +- **Use Case**: Second step after authorization endpoint redirect |
| 162 | + |
| 163 | +**Silent (Token Cache)** |
| 164 | +- **Public API**: `acquireTokenSilently(SilentParameters)` |
| 165 | +- **Parameters**: `SilentParameters` - Gets tokens from cache or refresh token |
| 166 | +- **Internal**: `SilentRequest` → `AcquireTokenSilentSupplier` |
| 167 | +- **Key Classes**: `TokenCache`, cache entity classes, `SilentRequestHelper` |
| 168 | + |
| 169 | +**Refresh Token (Migration)** |
| 170 | +- **Public API**: `acquireToken(RefreshTokenParameters)` |
| 171 | +- **Parameters**: `RefreshTokenParameters` - Direct use of refresh token (for ADAL migration) |
| 172 | +- **Internal**: `RefreshTokenRequest` → `AcquireTokenByAuthorizationGrantSupplier` |
| 173 | +- **Use Case**: Migration scenarios from ADAL to MSAL |
| 174 | + |
| 175 | +### Flow Execution Pattern |
| 176 | + |
| 177 | +All flows follow this pattern: |
| 178 | +1. Developer creates `*Parameters` object using builder |
| 179 | +2. Calls `application.acquireToken(*Parameters)` or `application.acquireTokenSilently(*Parameters)` |
| 180 | +3. Application creates `*Request` from parameters (contains `MsalRequest`) |
| 181 | +4. Routes to appropriate `*Supplier` via `getAuthenticationResultSupplier()` in `AbstractApplicationBase` |
| 182 | +5. Supplier checks cache, executes HTTP requests via `TokenRequestExecutor` |
| 183 | +6. Returns `CompletableFuture<IAuthenticationResult>` with tokens |
| 184 | + |
| 185 | +--- |
| 186 | + |
| 187 | +## Maintaining These Instructions |
| 188 | + |
| 189 | +**IMPORTANT**: When you implement changes to the MSAL Java codebase, you must review and update this `copilot-instructions.md` file to keep it accurate and useful for future agents. |
| 190 | + |
| 191 | +### When to Update This File |
| 192 | + |
| 193 | +Update this file whenever you make changes that affect: |
| 194 | + |
| 195 | +1. **Architecture & Structure** |
| 196 | + - Adding/removing application types or major classes |
| 197 | + - Changing the class hierarchy or inheritance structure |
| 198 | + - Modifying key architectural patterns |
| 199 | + - Reorganizing directory structure |
| 200 | + |
| 201 | +2. **Authentication Flows & Public APIs** |
| 202 | + - Adding a new authentication flow (new `*Parameters` and `*Supplier` classes) |
| 203 | + - Deprecating or removing an existing flow |
| 204 | + - Changing the flow execution pattern |
| 205 | + - Modifying which application types support which flows |
| 206 | + |
| 207 | +3. **Project Overview** |
| 208 | + - Updating version numbers (in pom.xml) |
| 209 | + - Adding/removing core capabilities |
| 210 | + - Changing supported Java versions or dependencies |
| 211 | + - Adding/removing modules from the repository |
| 212 | + |
| 213 | +4. **Important Implementation Details** |
| 214 | + - Adding new key classes that are central to how the library works |
| 215 | + - Changing token cache architecture or entity types |
| 216 | + - Modifying HTTP layer or retry behavior |
| 217 | + - Updating exception handling patterns |
| 218 | + |
| 219 | +### How to Update |
| 220 | + |
| 221 | +1. **After making your changes**, review each section of this file |
| 222 | +2. **Check for accuracy**: Do class names, file paths, and descriptions still match your changes? |
| 223 | +3. **Add new information**: If you added a new flow or major component, document it following the existing format |
| 224 | +4. **Mark deprecations**: If you deprecated a feature, note it clearly with a deprecation warning |
| 225 | +5. **Keep it concise**: This file is for Copilot agents - prioritize clarity and navigation over comprehensive detail |
| 226 | +6. **Test your updates**: Ensure the instructions would help another agent understand your changes |
| 227 | + |
| 228 | +### Example Scenarios |
| 229 | + |
| 230 | +- **Added a new auth flow?** → Add it to the "Authentication Flows & Public APIs" section with its Parameters class, Supplier class, and key classes |
| 231 | +- **Changed version to 1.24.0?** → Update the version in "Project Overview" |
| 232 | +- **Refactored cache entities?** → Update the "Token Cache" description in "Key Architectural Patterns" |
| 233 | +- **Added a new application type?** → Update "Application Hierarchy" and relevant flow sections |
| 234 | +- **Moved files to new directories?** → Update the "Important Directories" section |
| 235 | + |
| 236 | +By keeping these instructions current, you help ensure that future Copilot agents (and developers) can quickly understand and work with the MSAL Java library effectively. |
| 237 | + |
| 238 | +--- |
| 239 | + |
0 commit comments