9
9
using System . Text . RegularExpressions ;
10
10
using System . Threading . Tasks ;
11
11
using Microsoft . Git . CredentialManager ;
12
+ using Microsoft . IdentityModel . JsonWebTokens ;
12
13
13
14
namespace Microsoft . AzureRepos
14
15
{
15
16
public interface IAzureDevOpsRestApi : IDisposable
16
17
{
17
18
Task < string > GetAuthorityAsync ( Uri organizationUri ) ;
18
- Task < string > CreatePersonalAccessTokenAsync ( Uri organizationUri , string accessToken , IEnumerable < string > scopes ) ;
19
+ Task < string > CreatePersonalAccessTokenAsync ( Uri organizationUri , JsonWebToken accessToken , IEnumerable < string > scopes ) ;
19
20
}
20
21
21
22
public class AzureDevOpsRestApi : IAzureDevOpsRestApi
@@ -84,7 +85,7 @@ public async Task<string> GetAuthorityAsync(Uri organizationUri)
84
85
return commonAuthority ;
85
86
}
86
87
87
- public async Task < string > CreatePersonalAccessTokenAsync ( Uri organizationUri , string accessToken , IEnumerable < string > scopes )
88
+ public async Task < string > CreatePersonalAccessTokenAsync ( Uri organizationUri , JsonWebToken accessToken , IEnumerable < string > scopes )
88
89
{
89
90
const string sessionTokenUrl = "_apis/token/sessiontokens?api-version=1.0&tokentype=compact" ;
90
91
@@ -93,7 +94,7 @@ public async Task<string> CreatePersonalAccessTokenAsync(Uri organizationUri, st
93
94
{
94
95
throw new ArgumentException ( $ "Provided URI '{ organizationUri } ' is not a valid Azure DevOps hostname", nameof ( organizationUri ) ) ;
95
96
}
96
- EnsureArgument . NotNullOrWhiteSpace ( accessToken , nameof ( accessToken ) ) ;
97
+ EnsureArgument . NotNull ( accessToken , nameof ( accessToken ) ) ;
97
98
98
99
_context . Trace . WriteLine ( "Getting Azure DevOps Identity Service endpoint..." ) ;
99
100
Uri identityServiceUri = await GetIdentityServiceUriAsync ( organizationUri , accessToken ) ;
@@ -134,7 +135,7 @@ public async Task<string> CreatePersonalAccessTokenAsync(Uri organizationUri, st
134
135
135
136
#region Private Methods
136
137
137
- private async Task < Uri > GetIdentityServiceUriAsync ( Uri organizationUri , string accessToken )
138
+ private async Task < Uri > GetIdentityServiceUriAsync ( Uri organizationUri , JsonWebToken accessToken )
138
139
{
139
140
const string locationServicePath = "_apis/ServiceDefinitions/LocationService2/951917AC-A960-4999-8464-E3F0AA25B381" ;
140
141
const string locationServiceQuery = "api-version=1.0" ;
@@ -273,7 +274,7 @@ private static StringContent CreateAccessTokenRequestJson(Uri organizationUri, I
273
274
/// <param name="content">Optional request content.</param>
274
275
/// <param name="bearerToken">Optional bearer token for authorization.</param>
275
276
/// <returns>HTTP request message.</returns>
276
- private static HttpRequestMessage CreateRequestMessage ( HttpMethod method , Uri uri , HttpContent content = null , string bearerToken = null )
277
+ private static HttpRequestMessage CreateRequestMessage ( HttpMethod method , Uri uri , HttpContent content = null , JsonWebToken bearerToken = null )
277
278
{
278
279
var request = new HttpRequestMessage ( method , uri ) ;
279
280
@@ -282,9 +283,9 @@ private static HttpRequestMessage CreateRequestMessage(HttpMethod method, Uri ur
282
283
request . Content = content ;
283
284
}
284
285
285
- if ( ! string . IsNullOrWhiteSpace ( bearerToken ) )
286
+ if ( bearerToken != null )
286
287
{
287
- request . Headers . Authorization = new AuthenticationHeaderValue ( Constants . Http . WwwAuthenticateBearerScheme , bearerToken ) ;
288
+ request . Headers . Authorization = new AuthenticationHeaderValue ( Constants . Http . WwwAuthenticateBearerScheme , bearerToken . EncodedToken ) ;
288
289
}
289
290
290
291
return request ;
0 commit comments