Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 4 additions & 24 deletions msal4j-brokers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.microsoft.azure</groupId>
<artifactId>msal4j-brokers</artifactId>
<version>1.0.0-beta</version>
<version>0.0.1</version>
<packaging>jar</packaging>
<name>msal4j-brokers</name>
<description>
Expand All @@ -26,39 +26,18 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.microsoft.azure/msal4j -->
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>msal4j</artifactId>
<version>1.13.4</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>javamsalruntime</artifactId>
<version>0.13.4</version>
<version>1.13.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<scope>test</scope>
</dependency>
</dependencies>

<!-- force https -->
Expand All @@ -81,6 +60,7 @@
</pluginRepository>
</pluginRepositories>
<build>
<sourceDirectory>${project.build.directory}/delombok</sourceDirectory>
<plugins>
<plugin>
<groupId>org.projectlombok</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.microsoft.aad.msal4jbrokers;

import com.microsoft.aad.msal4j.*;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CompletableFuture;

@Slf4j
public class MSALRuntimeBroker implements IBroker {

@Override
public IAuthenticationResult acquireToken(PublicClientApplication application, SilentParameters requestParameters) {
log.debug("Should not call this API if msal runtime init failed");
throw new MsalClientException("Broker implementation missing", "missing_broker");
}

@Override
public IAuthenticationResult acquireToken(PublicClientApplication application, InteractiveRequestParameters requestParameters) {
throw new MsalClientException("Broker implementation missing", "missing_broker");
}

@Override
public IAuthenticationResult acquireToken(PublicClientApplication application, UserNamePasswordParameters requestParameters) {
throw new MsalClientException("Broker implementation missing", "missing_broker");
}

@Override
public CompletableFuture removeAccount(IAccount account) {
throw new MsalClientException("Broker implementation missing", "missing_broker");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -115,22 +115,9 @@ public class AuthenticationErrorCode {
* A JWT parsing failure, indicating the JWT provided to MSAL is of invalid format.
*/
public final static String INVALID_JWT = "invalid_jwt";

/**
* Indicates that a Broker implementation is missing from the device, such as when an app developer
* does not include one of our broker packages as a dependency in their project, or otherwise cannot
* be accessed by MSAL Java
*/
* be accessed by MSAL Java*/
public final static String MISSING_BROKER = "missing_broker";

/**
* Indicates an error from the MSAL Java/MSALRuntime interop layer used by the Java Brokers package,
* and will generally just be forwarding an error message from the interop layer or MSALRuntime itself
*/
public final static String MSALRUNTIME_INTEROP_ERROR = "interop_package_error";

/**
* Indicates an error in the MSAL Java Brokers package
*/
public final static String MSALJAVA_BROKERS_ERROR = "brokers_package_error";
}
68 changes: 23 additions & 45 deletions msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/IBroker.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,80 +3,58 @@

package com.microsoft.aad.msal4j;

import com.nimbusds.jwt.JWTParser;

import java.net.URL;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

/**
* Used to define the basic set of methods that all Brokers must implement
*
* All methods are marked as default so they can be referenced by MSAL Java without an implementation,
* and most will simply throw an exception if not overridden by an IBroker implementation
* All methods are so they can be referenced by MSAL Java without an implementation, and by default simply throw an
* exception saying that a broker implementation is missing
*/
public interface IBroker {

/**
* checks if a IBroker implementation exists
*/

default boolean isAvailable(){
return false;
}
/**
* Acquire a token silently, i.e. without direct user interaction
*
* This may be accomplished by returning tokens from a token cache, using cached refresh tokens to get new tokens,
* or via any authentication flow where a user is not prompted to enter credentials
*
* @param requestParameters MsalRequest object which contains everything needed for the broker implementation to make a request
* @return IBroker implementations will return an AuthenticationResult object
*/
default CompletableFuture<IAuthenticationResult> acquireToken(PublicClientApplication application, SilentParameters requestParameters) {
default IAuthenticationResult acquireToken(PublicClientApplication application, SilentParameters requestParameters) {
throw new MsalClientException("Broker implementation missing", AuthenticationErrorCode.MISSING_BROKER);
}

/**
* Acquire a token interactively, by prompting users to enter their credentials in some way
*
* @param requestParameters MsalRequest object which contains everything needed for the broker implementation to make a request
* @return IBroker implementations will return an AuthenticationResult object
*/
default CompletableFuture<IAuthenticationResult> acquireToken(PublicClientApplication application, InteractiveRequestParameters parameters) {
default IAuthenticationResult acquireToken(PublicClientApplication application, InteractiveRequestParameters requestParameters) {
throw new MsalClientException("Broker implementation missing", AuthenticationErrorCode.MISSING_BROKER);
}

/**
* Acquire a token silently, i.e. without direct user interaction, using username/password authentication
*
* @param requestParameters MsalRequest object which contains everything needed for the broker implementation to make a request
* @return IBroker implementations will return an AuthenticationResult object
*/
default CompletableFuture<IAuthenticationResult> acquireToken(PublicClientApplication application, UserNamePasswordParameters parameters) {
default IAuthenticationResult acquireToken(PublicClientApplication application, UserNamePasswordParameters requestParameters) {
throw new MsalClientException("Broker implementation missing", AuthenticationErrorCode.MISSING_BROKER);
}

default void removeAccount(PublicClientApplication application, IAccount account) throws MsalClientException {
default CompletableFuture removeAccount(IAccount account) {
throw new MsalClientException("Broker implementation missing", AuthenticationErrorCode.MISSING_BROKER);
}

default boolean isBrokerAvailable() {
throw new MsalClientException("Broker implementation missing", AuthenticationErrorCode.MISSING_BROKER);
}

/**
* MSAL Java's AuthenticationResult requires several package-private classes that a broker implementation can't access,
* so this helper method can be used to create AuthenticationResults from within the MSAL Java package
*/
default IAuthenticationResult parseBrokerAuthResult(String authority, String idToken, String accessToken,
String accountId, String clientInfo,
long accessTokenExpirationTime) {

AuthenticationResult.AuthenticationResultBuilder builder = AuthenticationResult.builder();

try {
if (idToken != null) {
builder.idToken(idToken);
if (accountId!= null) {
String idTokenJson =
JWTParser.parse(idToken).getParsedParts()[1].decodeToString();
//TODO: need to figure out if 'policy' field is relevant for brokers
builder.accountCacheEntity(AccountCacheEntity.create(clientInfo,
Authority.createAuthority(new URL(authority)), JsonHelper.convertJsonToObject(idTokenJson,
IdToken.class), null));
}
}
if (accessToken != null) {
builder.accessToken(accessToken);
builder.expiresOn(accessTokenExpirationTime);
}
} catch (Exception e) {
throw new MsalClientException(String.format("Exception when converting broker result to MSAL Java AuthenticationResult: %s", e.getMessage()), AuthenticationErrorCode.MSALJAVA_BROKERS_ERROR);
}
return builder.build();
}
}
Loading