Skip to content
This repository has been archived by the owner on Oct 4, 2021. It is now read-only.

Added sample for token administration #152

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,14 @@
<Compile Include="Graph\MembershipStatesSample.cs" />
<Compile Include="Graph\StorageKeySample.cs" />
<Compile Include="Graph\SubjectLookupSample.cs" />
<Compile Include="Graph\GroupsSample.cs" />
<Compile Include="Graph\UsersSample.cs" />
<Compile Include="Graph\MembershipSample.cs" />
<Compile Include="ProjectsAndTeams\ProcessesSample.cs" />
<Compile Include="ProjectsAndTeams\ProjectCollectionsSample.cs" />
<Compile Include="ProjectsAndTeams\ProjectsSample.cs" />
<Compile Include="ProjectsAndTeams\TeamsSample.cs" />
<Compile Include="Git\*.cs" />
<Compile Include="Graph\GroupsSample.cs" />
<Compile Include="Graph\UsersSample.cs" />
<Compile Include="Notification\*.cs" />
<Compile Include="Release\ReleasesSample.cs" />
<Compile Include="Security\AccessControlListsSample.cs" />
Expand All @@ -171,6 +172,9 @@
<Compile Include="Tfvc\ChangesetChangesSample.cs" />
<Compile Include="Tfvc\BranchesSample.cs" />
<Compile Include="Tfvc\ChangesetsSample.cs" />
<Compile Include="TokenAdmin\FromFutureWebApi\TokenAdminContracts.cs" />
<Compile Include="TokenAdmin\FromFutureWebApi\TokenAdminHttpClient.cs" />
<Compile Include="TokenAdmin\TokenAdminSample.cs" />
<Compile Include="WorkItemTracking\AttachmentsSample.cs" />
<Compile Include="WorkItemTracking\BatchSample.cs" />
<Compile Include="WorkItemTracking\ClassificationNodesSample.cs" />
Expand All @@ -190,7 +194,6 @@
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<Compile Include="Graph\MembershipSample.cs" />
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using Microsoft.VisualStudio.Services.DelegatedAuthorization;

namespace Microsoft.VisualStudio.Services.TokenAdmin.Client
{
public static class TokenAdminResourceIds
{
public const string AreaName = "TokenAdmin";
public const string AreaId = "af68438b-ed04-4407-9eb6-f1dbae3f922e";

public const string PersonalAccessTokensResource = "PersonalAccessTokens";
public static readonly Guid PersonalAccessTokensLocationId = new Guid("{af68438b-ed04-4407-9eb6-f1dbae3f922e}");

public const string RevocationsResource = "Revocations";
public static readonly Guid RevocationsLocationId = new Guid("{a9c08b2c-5466-4e22-8626-1ff304ffdf0f}");

public const string RevocationRulesResource = "RevocationRules";
public static readonly Guid RevocationRulesLocationId = new Guid("{ee4afb16-e7ab-4ed8-9d4b-4ef3e78f97e4}");
}

/// <summary>
/// A paginatated list of session tokens.
/// Session tokens correspond to OAuth credentials such as personal access tokens (PATs)
/// and other OAuth authorizations.
/// </summary>
[DataContract]
public class TokenAdminPagedSessionTokens
{
/// <summary>
/// The list of all session tokens in the current page.
/// </summary>
[DataMember(Name = "Value")]
public IEnumerable<SessionToken> SessionTokens { get; set; }

/// <summary>
/// The continuation token that can be used to retrieve the next page of session tokens,
/// or <code>null</code> if there is no next page.
/// </summary>
[DataMember]
public Guid? ContinuationToken { get; set; }
}

/// <summary>
/// A rule which is applied to disable any incoming delegated authorization
/// which matches the given properties.
/// </summary>
public class TokenAdminRevocationRule
{
/// <summary>
/// A string containing a space-delimited list of OAuth scopes.
/// A token matching any one of the scopes will be rejected.
/// For a list of all OAuth scopes supported by VSTS, see:
/// https://docs.microsoft.com/en-us/vsts/integrate/get-started/authentication/oauth?view=vsts#scopes.
/// This is a mandatory parameter.
/// </summary>
public string Scopes { get; set; }

/// <summary>
/// A datetime cutoff. Tokens created before this time will be rejected.
/// This is an optional paramter. If omitted, defaults to the time at which the rule was created.
/// </summary>
public DateTime? CreatedBefore { get; set; }
}

/// <summary>
/// A request to revoke a particular delegated authorization.
/// </summary>
public class TokenAdminRevocation
{
/// <summary>
/// The authorization ID of the OAuth authorization to revoke.
/// </summary>
public Guid AuthorizationId { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* ---------------------------------------------------------
* Copyright(C) Microsoft Corporation. All rights reserved.
* ---------------------------------------------------------
*
* ---------------------------------------------------------
* Generated file, DO NOT EDIT
* ---------------------------------------------------------
*
* See following wiki page for instructions on how to regenerate:
* https://vsowiki.com/index.php?title=Rest_Client_Generation
*
* Configuration file:
* vssf\client\webapi\httpclients\clientgeneratorconfigs\tokenadmin.genclient.json
*/

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;

namespace Microsoft.VisualStudio.Services.TokenAdmin.Client
{
[ResourceArea(TokenAdminResourceIds.AreaId)]
public class TokenAdminHttpClient : VssHttpClientBase
{
public TokenAdminHttpClient(Uri baseUrl, VssCredentials credentials)
: base(baseUrl, credentials)
{
}

public TokenAdminHttpClient(Uri baseUrl, VssCredentials credentials, VssHttpRequestSettings settings)
: base(baseUrl, credentials, settings)
{
}

public TokenAdminHttpClient(Uri baseUrl, VssCredentials credentials, params DelegatingHandler[] handlers)
: base(baseUrl, credentials, handlers)
{
}

public TokenAdminHttpClient(Uri baseUrl, VssCredentials credentials, VssHttpRequestSettings settings, params DelegatingHandler[] handlers)
: base(baseUrl, credentials, settings, handlers)
{
}

public TokenAdminHttpClient(Uri baseUrl, HttpMessageHandler pipeline, bool disposeHandler)
: base(baseUrl, pipeline, disposeHandler)
{
}

/// <summary>
/// [Preview API] Lists of all the session token details of the personal access tokens (PATs) for a particular user.
/// </summary>
/// <param name="subjectDescriptor">The descriptor of the target user.</param>
/// <param name="pageSize">The maximum number of results to return on each page.</param>
/// <param name="continuationToken">An opaque data blob that allows the next page of data to resume immediately after where the previous page ended. The only reliable way to know if there is more data left is the presence of a continuation token.</param>
/// <param name="userState"></param>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
public Task<TokenAdminPagedSessionTokens> ListPersonalAccessTokensAsync(
SubjectDescriptor subjectDescriptor,
int? pageSize = null,
string continuationToken = null,
object userState = null,
CancellationToken cancellationToken = default(CancellationToken))
{
HttpMethod httpMethod = new HttpMethod("GET");
Guid locationId = new Guid("af68438b-ed04-4407-9eb6-f1dbae3f922e");
object routeValues = new { subjectDescriptor = subjectDescriptor };

List<KeyValuePair<string, string>> queryParams = new List<KeyValuePair<string, string>>();
if (pageSize != null)
{
queryParams.Add("pageSize", pageSize.Value.ToString(CultureInfo.InvariantCulture));
}
if (!string.IsNullOrEmpty(continuationToken))
{
queryParams.Add("continuationToken", continuationToken);
}

return SendAsync<TokenAdminPagedSessionTokens>(
httpMethod,
locationId,
routeValues: routeValues,
version: new ApiResourceVersion("5.0-preview.1"),
queryParameters: queryParams,
userState: userState,
cancellationToken: cancellationToken);
}

/// <summary>
/// [Preview API] Creates a revocation rule to prevent the further usage of any OAuth authorizations that were created before the current point in time and which match the conditions in the rule.
/// </summary>
/// <param name="revocationRule">The revocation rule to create. The rule must specify a scope, after which preexisting OAuth authorizations that match that scope will be rejected. For a list of all OAuth scopes supported by VSTS, see: https://docs.microsoft.com/en-us/vsts/integrate/get-started/authentication/oauth?view=vsts#scopes</param>
/// <param name="userState"></param>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
public async Task CreateRevocationRuleAsync(
TokenAdminRevocationRule revocationRule,
object userState = null,
CancellationToken cancellationToken = default(CancellationToken))
{
HttpMethod httpMethod = new HttpMethod("POST");
Guid locationId = new Guid("ee4afb16-e7ab-4ed8-9d4b-4ef3e78f97e4");
HttpContent content = new ObjectContent<TokenAdminRevocationRule>(revocationRule, new VssJsonMediaTypeFormatter(true));

using (HttpResponseMessage response = await SendAsync(
httpMethod,
locationId,
version: new ApiResourceVersion("5.0-preview.1"),
userState: userState,
cancellationToken: cancellationToken,
content: content).ConfigureAwait(false))
{
return;
}
}

/// <summary>
/// [Preview API] Revokes the listed OAuth authorizations.
/// </summary>
/// <param name="revocations">The list of objects containing the authorization IDs of the OAuth authorizations, such as session tokens retrieved by listed a users PATs, that should be revoked.</param>
/// <param name="userState"></param>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
public async Task RevokeAuthorizationsAsync(
IEnumerable<TokenAdminRevocation> revocations,
object userState = null,
CancellationToken cancellationToken = default(CancellationToken))
{
HttpMethod httpMethod = new HttpMethod("POST");
Guid locationId = new Guid("a9c08b2c-5466-4e22-8626-1ff304ffdf0f");
HttpContent content = new ObjectContent<IEnumerable<TokenAdminRevocation>>(revocations, new VssJsonMediaTypeFormatter(true));

using (HttpResponseMessage response = await SendAsync(
httpMethod,
locationId,
version: new ApiResourceVersion("5.0-preview.1"),
userState: userState,
cancellationToken: cancellationToken,
content: content).ConfigureAwait(false))
{
return;
}
}
}
}
Loading