-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #330 from AzureAD/shoatman-command
Added command objects to wrap invocation of controllers
- Loading branch information
Showing
21 changed files
with
576 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 30 additions & 7 deletions
37
msal/src/main/java/com/microsoft/identity/client/controllers/BrokerMSALController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,45 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// All rights reserved. | ||
// | ||
// This code is licensed under the MIT License. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files(the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions : | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
package com.microsoft.identity.client.controllers; | ||
|
||
import android.content.Intent; | ||
|
||
import com.microsoft.identity.client.AuthenticationResult; | ||
|
||
public class BrokerMSALController extends MSALController { | ||
|
||
@Override | ||
public void AcquireToken(MSALAcquireTokenRequest request) { | ||
|
||
public AuthenticationResult acquireToken(MSALAcquireTokenOperationParameters request) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void CompleteAcquireToken(int requestCode, int resultCode, Intent data) { | ||
|
||
public void completeAcquireToken(int requestCode, int resultCode, Intent data) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void AcquireTokenSilent(MSALAcquireTokenSilentRequest request) { | ||
|
||
public AuthenticationResult acquireTokenSilent(MSALAcquireTokenSilentOperationParameters request) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
} |
121 changes: 101 additions & 20 deletions
121
msal/src/main/java/com/microsoft/identity/client/controllers/LocalMSALController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,144 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// All rights reserved. | ||
// | ||
// This code is licensed under the MIT License. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files(the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions : | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
package com.microsoft.identity.client.controllers; | ||
|
||
import android.content.Intent; | ||
|
||
import com.microsoft.identity.client.AuthenticationResult; | ||
import com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsAuthorizationRequest; | ||
import com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsOAuth2Configuration; | ||
import com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsOAuth2Strategy; | ||
import com.microsoft.identity.common.internal.providers.oauth2.AuthorizationConfiguration; | ||
import com.microsoft.identity.common.internal.providers.oauth2.AuthorizationRequest; | ||
import com.microsoft.identity.common.internal.providers.oauth2.AuthorizationResponse; | ||
import com.microsoft.identity.common.internal.providers.oauth2.AuthorizationResult; | ||
import com.microsoft.identity.common.internal.providers.oauth2.AuthorizationStatus; | ||
import com.microsoft.identity.common.internal.providers.oauth2.AuthorizationStrategy; | ||
import com.microsoft.identity.common.internal.providers.oauth2.OAuth2Strategy; | ||
import com.microsoft.identity.common.internal.providers.oauth2.TokenRequest; | ||
import com.microsoft.identity.common.internal.providers.oauth2.TokenResult; | ||
import com.microsoft.identity.common.internal.ui.AuthorizationStrategyFactory; | ||
import com.microsoft.identity.common.internal.ui.browser.BrowserAuthorizationStrategy; | ||
import com.microsoft.identity.common.internal.util.StringUtil; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.Future; | ||
|
||
|
||
public class LocalMSALController extends MSALController{ | ||
public class LocalMSALController extends MSALController { | ||
|
||
private OAuth2Strategy mOAuthStrategy = null; | ||
private AuthorizationStrategy mAuthorizationStrategy = null; | ||
|
||
@Override | ||
public void AcquireToken(MSALAcquireTokenRequest request) throws ExecutionException, InterruptedException { | ||
public AuthenticationResult acquireToken(MSALAcquireTokenOperationParameters parameters) throws ExecutionException, InterruptedException { | ||
|
||
//TODO: Use factory to get applicable oAuth and Authorization strategies | ||
mOAuthStrategy = new MicrosoftStsOAuth2Strategy(new MicrosoftStsOAuth2Configuration()); | ||
|
||
//TODO: Map MSAL Acquire Token Request to Authorization Request | ||
AuthorizationRequest authRequest = new MicrosoftStsAuthorizationRequest(); | ||
//1) TODO: Use factory to get applicable oAuth and Authorization strategies | ||
OAuth2Strategy oAuth2Strategy = new MicrosoftStsOAuth2Strategy(new MicrosoftStsOAuth2Configuration()); | ||
|
||
authRequest.setClientId(request.getClientId()); | ||
authRequest.setRedirectUri(request.getRedirectUri()); | ||
authRequest.setScope(StringUtil.join(' ', request.getScopes())); | ||
//2) Gather authorization interactively | ||
AuthorizationResult result = performAuthorizationRequest(oAuth2Strategy, parameters); | ||
|
||
if (result.getAuthorizationStatus().equals(AuthorizationStatus.SUCCESS)) { | ||
//3) Exchange authorization code for token | ||
TokenResult tokenResult = performTokenRequest(oAuth2Strategy, result.getAuthorizationResponse(), parameters); | ||
if (tokenResult != null && tokenResult.getSuccess()) { | ||
//4) Save tokens in token cache | ||
//saveTokens(oAuth2Strategy, getAuthorizationRequest(oAuth2Strategy, parameters), tokenResult.getTokenResponse(), parameters.getTokenCache()); | ||
} | ||
} | ||
|
||
//TODO: Replace with factory to create the correct Authorization Strategy based on device capabilities and configuration | ||
mAuthorizationStrategy = AuthorizationStrategyFactory.getInstance().getAuthorizationStrategy(request.getActivity(), AuthorizationConfiguration.getInstance()); | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
Future<AuthorizationResult> future = mOAuthStrategy.requestAuthorization(authRequest, mAuthorizationStrategy); | ||
private AuthorizationResult performAuthorizationRequest(OAuth2Strategy strategy, MSALAcquireTokenOperationParameters parameters) throws ExecutionException, InterruptedException { | ||
|
||
future.get(); | ||
//TODO: Replace with factory to create the correct Authorization Strategy based on device capabilities and configuration | ||
mAuthorizationStrategy = AuthorizationStrategyFactory.getInstance().getAuthorizationStrategy(parameters.getActivity(), AuthorizationConfiguration.getInstance()); | ||
|
||
Future<AuthorizationResult> future = strategy.requestAuthorization(getAuthorizationRequest(parameters), mAuthorizationStrategy); | ||
|
||
//We could implement Timeout Here if we wish instead of looping forever | ||
//We could implement Timeout Here if we wish instead of blocking indefinitely | ||
//future.get(10, TimeUnit.MINUTES); // Need to handle timeout exception in the scenario it doesn't return within a reasonable amount of time | ||
//AuthorizationResult authorizationResult = future.get(); | ||
AuthorizationResult result = future.get(); | ||
|
||
return result; | ||
|
||
} | ||
|
||
private AuthorizationRequest getAuthorizationRequest(MSALAcquireTokenOperationParameters parameters) { | ||
AuthorizationRequest authRequest = new MicrosoftStsAuthorizationRequest(); | ||
|
||
String scopes = StringUtil.join(' ', parameters.getScopes()); | ||
|
||
authRequest.setClientId(parameters.getClientId()); | ||
authRequest.setRedirectUri(parameters.getRedirectUri()); | ||
authRequest.setScope(scopes); | ||
authRequest.setResponseType(AuthorizationRequest.ResponseTypes.CODE); | ||
|
||
return authRequest; | ||
} | ||
|
||
private TokenResult performTokenRequest(OAuth2Strategy strategy, AuthorizationResponse response, MSALAcquireTokenOperationParameters parameters) { | ||
|
||
TokenRequest tokenRequest = new TokenRequest(); | ||
|
||
tokenRequest.setCode(response.getCode()); | ||
tokenRequest.setClientId(parameters.getClientId()); | ||
tokenRequest.setRedirectUri(parameters.getRedirectUri()); | ||
tokenRequest.setScope(StringUtil.join(' ', parameters.getScopes())); | ||
tokenRequest.setGrantType(TokenRequest.GrantTypes.AUTHORIZATION_CODE); | ||
|
||
TokenResult tokenResult = null; | ||
|
||
try { | ||
tokenResult = strategy.requestToken(tokenRequest); | ||
} catch (IOException e) { | ||
//TODO: Figure out exception handling | ||
} | ||
|
||
return tokenResult; | ||
|
||
} | ||
|
||
/* | ||
private void saveTokens(OAuth2Strategy strategy, AuthorizationRequest request, TokenResponse tokenResponse, MsalOAuth2TokenCache tokenCache){ | ||
try { | ||
tokencCache.saveTokens(mOAuthStrategy, authRequest, tokenResult.getTokenResponse()); | ||
} catch (ClientException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
*/ | ||
|
||
|
||
@Override | ||
public void CompleteAcquireToken(int requestCode, int resultCode, final Intent data) { | ||
public void completeAcquireToken(int requestCode, int resultCode, final Intent data) { | ||
mAuthorizationStrategy.completeAuthorization(requestCode, resultCode, data); | ||
} | ||
|
||
@Override | ||
public void AcquireTokenSilent(MSALAcquireTokenSilentRequest request) { | ||
|
||
public AuthenticationResult acquireTokenSilent(MSALAcquireTokenSilentOperationParameters request) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |
Oops, something went wrong.