|
| 1 | +# Microsoft Authentication Extensions for Java |
| 2 | + |
| 3 | +The Microsoft Authentication Extensions for Java offers secure mechanisms for client applications to perform cross-platform token cache serialization and persistence. It gives additional support to the [Microsoft Authentication Library for Java (MSAL)](https://github.com/AzureAD/microsoft-authentication-library-for-java). |
| 4 | + |
| 5 | +MSAL Java supports an in-memory cache by default and provides the `ITokenCacheAccessAspect` interface to perform cache serialization. You can read more about this in the MSAL Java [documentation](https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-java-token-cache-serialization). Developers are required to implement their own cache persistance across multiple platforms and Microsoft Authentication Extensions makes this simpler. |
| 6 | +The extensions library supports persistence of the cache to disk in encrypted form where the platform supports this. |
| 7 | + |
| 8 | +This is available for Windows, Mac and Linux. |
| 9 | +- Windows - [DPAPI](https://docs.microsoft.com/en-us/dotnet/standard/security/how-to-use-data-protection) is used for encryption. |
| 10 | +- MAC - The MAC KeyChain is used and encryption is built in. |
| 11 | +- Linux - [LibSecret](https://wiki.gnome.org/Projects/Libsecret) is used for encryption. Any place where libsecret is not available encryption is not supported. |
| 12 | + |
| 13 | +> Note: It is recommended to use this library for cache persistance support for Public client applications such as Desktop apps only. In web applications, this may lead to scale and performance issues. Web applications are recommended to persist the cache in session. Take a look at this [webapp sample](https://github.com/Azure-Samples/ms-identity-java-webapp). |
| 14 | +
|
| 15 | +## Installation |
| 16 | + |
| 17 | +You can find the latest pacakge in the [Maven repository](https://mvnrepository.com/artifact/com.microsoft.azure/msal4j-persistence-extension). |
| 18 | + |
| 19 | +Add the dependecy to the pom file. |
| 20 | + |
| 21 | +``` |
| 22 | +<dependency> |
| 23 | + <groupId>com.microsoft.azure</groupId> |
| 24 | + <artifactId>msal4j-persistence-extension</artifactId> |
| 25 | + <version>1.2.0</version> |
| 26 | +</dependency> |
| 27 | +``` |
| 28 | + |
| 29 | +## Versions |
| 30 | + |
| 31 | +This library follows [Semantic Versioning](http://semver.org/). |
| 32 | + |
| 33 | +## Usage |
| 34 | + |
| 35 | +[](https://javadoc.io/doc/com.microsoft.azure/msal4j-persistence-extension) |
| 36 | + |
| 37 | +The Microsoft Authentication Extensions library provides the `PersistenceTokenCacheAccessAspect` which is an implementation of the `ITokenCacheAccessAspect` interface defined in MSAL Java. After configuring this token cache, it can then be used to instantiate the client application in MSAL Java. |
| 38 | + |
| 39 | +The token cache includes a file lock, and auto-reload behavior under the hood. |
| 40 | + |
| 41 | +Here is the usage pattern for multiple platforms: |
| 42 | + |
| 43 | +1. Configure the `PersistenceSettings`. |
| 44 | + |
| 45 | + ```java |
| 46 | + private PersistenceSettings createPersistenceSettings() throws IOException { |
| 47 | + |
| 48 | + Path path = Paths.get(System.getProperty("user.home"), "MSAL", "testCache"); |
| 49 | + |
| 50 | + return PersistenceSettings.builder("testCacheFile", path) |
| 51 | + .setMacKeychain("MsalTestService", "MsalTestAccount") |
| 52 | + .setLinuxKeyring(null, |
| 53 | + "MsalTestSchema", |
| 54 | + "MsalTestSecretLabel", |
| 55 | + "MsalTestAttribute1Key", |
| 56 | + "MsalTestAttribute1Value", |
| 57 | + "MsalTestAttribute2Key", |
| 58 | + "MsalTestAttribute2Value") |
| 59 | + .setLockRetry(1000, 50) |
| 60 | + .build(); |
| 61 | + } |
| 62 | + ``` |
| 63 | + |
| 64 | +1. Create the `PersistenceTokenCacheAccessAspect`. |
| 65 | + |
| 66 | + ```java |
| 67 | + private ITokenCacheAccessAspect createPersistenceAspect() throws IOException { |
| 68 | + return new PersistenceTokenCacheAccessAspect(createPersistenceSettings()); |
| 69 | + } |
| 70 | + ``` |
| 71 | +1. Now you can use `PersistenceTokenCacheAccessAspect` to configure Msal client application: |
| 72 | + |
| 73 | + ```java |
| 74 | + return PublicClientApplication.builder(PUBLIC_CLIENT_ID) |
| 75 | + .authority(AUTHORITY) |
| 76 | + .setTokenCacheAccessAspect(createPersistenceAspect()) |
| 77 | + .build(); |
| 78 | + ``` |
| 79 | + |
| 80 | +## Community Help and Support |
| 81 | + |
| 82 | +We leverage Stack Overflow to work with the community on supporting Azure Active Directory and its SDKs, including this one! |
| 83 | +We highly recommend you ask your questions on Stack Overflow (we're all on there!). |
| 84 | +Also browse existing issues to see if someone has had your question before. |
| 85 | +
|
| 86 | +We recommend you use the "msal" tag so we can see it! |
| 87 | +Here is the latest Q&A on Stack Overflow for MSAL: |
| 88 | +[http://stackoverflow.com/questions/tagged/msal](http://stackoverflow.com/questions/tagged/msal) |
| 89 | +
|
| 90 | +
|
| 91 | +## Contributing |
| 92 | +
|
| 93 | +All code is licensed under the MIT license and we triage actively on GitHub. |
| 94 | +
|
| 95 | +This project welcomes contributions and suggestions. Most contributions require you to agree to a |
| 96 | +Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us |
| 97 | +the rights to use your contribution. For details, visit https://cla.microsoft.com. |
| 98 | +
|
| 99 | +When you submit a pull request, a CLA-bot will automatically determine whether you need to provide |
| 100 | +a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions |
| 101 | +provided by the bot. You will only need to do this once across all repos using our CLA. |
| 102 | +
|
| 103 | +
|
| 104 | +## We value and adhere to the Microsoft Open Source Code of Conduct |
| 105 | +
|
| 106 | +This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [[email protected]](mailto:[email protected]) with any additional questions or comments. |
0 commit comments