@@ -8,10 +8,12 @@ namespace Microsoft.Store.PartnerCenter.PowerShell.Commands
88{
99 using System ;
1010 using System . Management . Automation ;
11- using System . Security ;
1211 using System . Text . RegularExpressions ;
12+ using System . Web ;
1313 using Common ;
14- using IdentityModel . Clients . ActiveDirectory ;
14+ using Models . Authentication ;
15+ using Network ;
16+ using Platform ;
1517 using Profile ;
1618
1719 [ Cmdlet ( VerbsCommon . New , "PartnerAccessToken" , DefaultParameterSetName = "UserCredential" ) ]
@@ -23,13 +25,29 @@ public class NewPartnerAccessToken : PSCmdlet
2325 /// </summary>
2426 private const string CommonEndpoint = "common" ;
2527
28+ /// <summary>
29+ /// The value for the redirect URI.
30+ /// </summary>
31+ private const string redirectUriValue = "urn:ietf:wg:oauth:2.0:oob" ;
32+
33+ /// <summary>
34+ /// The redirect URI used when requesting an access token.
35+ /// </summary>
36+ private readonly Uri redirectUri = new Uri ( redirectUriValue ) ;
37+
2638 /// <summary>
2739 /// Gets or sets the application identifier.
2840 /// </summary>
2941 [ Parameter ( HelpMessage = "The application identifier used to access Partner Center." , Mandatory = true , ParameterSetName = "UserCredential" ) ]
3042 [ ValidatePattern ( @"^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$" , Options = RegexOptions . Compiled | RegexOptions . IgnoreCase ) ]
3143 public string ApplicationId { get ; set ; }
3244
45+ /// <summary>
46+ /// Gets or sets a flag indicating that the intention is to perform the partner consent process.
47+ /// </summary>
48+ [ Parameter ( HelpMessage = "A flag that indicates that the intention is to perform the partner consent process." , Mandatory = false ) ]
49+ public SwitchParameter Consent { get ; set ; }
50+
3351 /// <summary>
3452 /// Gets or sets the credentials.
3553 /// </summary>
@@ -45,6 +63,20 @@ public class NewPartnerAccessToken : PSCmdlet
4563 [ ValidateNotNullOrEmpty ]
4664 public EnvironmentName Environment { get ; set ; }
4765
66+ /// <summary>
67+ /// Gets or sets the refresh token to use in the refresh flow.
68+ /// </summary>
69+ [ Parameter ( HelpMessage = "The refresh token to use in the refresh flow." , Mandatory = false ) ]
70+ [ ValidateNotNullOrEmpty ]
71+ public string RefreshToken { get ; set ; }
72+
73+ /// <summary>
74+ /// Gets or sets the identifier of the target resource that is the recipient of the requested token.
75+ /// </summary>
76+ [ Parameter ( HelpMessage = "The identifier of the target resource that is the recipient of the requested token." , Mandatory = true ) ]
77+ [ ValidateNotNullOrEmpty ]
78+ public string Resource { get ; set ; }
79+
4880 /// <summary>
4981 /// Gets or sets a flag indicating that a service principal will be used to authenticate.
5082 /// </summary
@@ -54,26 +86,22 @@ public class NewPartnerAccessToken : PSCmdlet
5486 /// <summary>
5587 /// Gets or sets the tenant identifier.
5688 /// </summary>
57- [ Parameter ( HelpMessage = "The Azure AD domain or tenant identifier." , Mandatory = true , ParameterSetName = "ServicePrincipal" ) ]
89+ [ Parameter ( HelpMessage = "The Azure AD domain or tenant identifier." , Mandatory = false ) ]
5890 [ ValidateNotNullOrEmpty ]
5991 public string TenantId { get ; set ; }
6092
61- /// <summary>
62- /// Gets or sets the token cache.
63- /// </summary>
64- [ Parameter ( HelpMessage = "The token cache to be used when requesting an access token." , Mandatory = false ) ]
65- [ ValidateNotNull ]
66- public TokenCache TokenCache { get ; set ; }
67-
6893 /// <summary>
6994 /// Performs the execution of the command.
7095 /// </summary>
7196 protected override void ProcessRecord ( )
7297 {
7398 AuthenticationResult authResult ;
7499 AzureAccount account = new AzureAccount ( ) ;
75- SecureString password = null ;
76-
100+ IPartnerServiceClient client ;
101+ PartnerEnvironment environment ;
102+ AuthorizationResult authorizationResult ;
103+ string authority ;
104+ string clientId ;
77105
78106 if ( ParameterSetName . Equals ( "ServicePrincipal" , StringComparison . InvariantCultureIgnoreCase ) )
79107 {
@@ -85,20 +113,48 @@ protected override void ProcessRecord()
85113 account . Type = AccountType . User ;
86114 }
87115
88- if ( Credential != null )
116+ account . Properties [ AzureAccountPropertyType . Tenant ] = string . IsNullOrEmpty ( TenantId ) ? CommonEndpoint : TenantId ;
117+ environment = PartnerEnvironment . PublicEnvironments [ Environment ] ;
118+
119+ client = new PartnerServiceClient ( new Uri ( environment . PartnerCenterEndpoint ) ) ;
120+ authority = $ "{ environment . ActiveDirectoryAuthority } { account . Properties [ AzureAccountPropertyType . Tenant ] } /oauth2/token";
121+
122+ clientId = account . Type == AccountType . ServicePrincipal ? Credential . UserName : ApplicationId ;
123+
124+ if ( ! string . IsNullOrEmpty ( RefreshToken ) )
89125 {
90- account . Id = Credential . UserName ;
91- password = Credential . Password ;
126+ authResult = client . RefreshAccessTokenAsync (
127+ authority ,
128+ Resource ,
129+ RefreshToken ,
130+ clientId ,
131+ Credential ? . Password . ConvertToString ( ) ) . GetAwaiter ( ) . GetResult ( ) ;
92132 }
133+ else if ( account . Type == AccountType . ServicePrincipal && ! Consent . IsPresent || Consent . ToBool ( ) == false )
134+ {
135+ authResult = client . AcquireTokenAsync (
136+ authority ,
137+ Resource ,
138+ clientId ,
139+ Credential . Password . ConvertToString ( ) ) . GetAwaiter ( ) . GetResult ( ) ;
140+ }
141+ else
142+ {
143+ using ( WindowsFormsWebAuthenticationDialog dialog = new WindowsFormsWebAuthenticationDialog ( null ) )
144+ {
145+ authorizationResult = dialog . AuthenticateAAD (
146+ new Uri ( $ "{ environment . ActiveDirectoryAuthority } { account . Properties [ AzureAccountPropertyType . Tenant ] } /oauth2/authorize?resource={ HttpUtility . UrlEncode ( Resource ) } &client_id={ clientId } &response_type=code&haschrome=1&redirect_uri={ HttpUtility . UrlEncode ( redirectUriValue ) } &response_mode=form_post&prompt=login") ,
147+ redirectUri ) ;
148+ }
93149
94- account . Properties [ AzureAccountPropertyType . Tenant ] = string . IsNullOrEmpty ( TenantId ) ? CommonEndpoint : TenantId ;
95-
96- authResult = PartnerSession . Instance . AuthenticationFactory . Authenticate (
97- ApplicationId ,
98- account ,
99- password ,
100- Environment ,
101- TokenCache ?? TokenCache . DefaultShared ) ;
150+ authResult = client . AcquireTokenByAuthorizationCodeAsync (
151+ authority ,
152+ Resource ,
153+ redirectUri ,
154+ authorizationResult . Code ,
155+ clientId ,
156+ Credential ? . Password . ConvertToString ( ) ) . GetAwaiter ( ) . GetResult ( ) ;
157+ }
102158
103159 WriteObject ( authResult ) ;
104160 }
0 commit comments