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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public CommunicationIdentityClient(System.Uri endpoint, Azure.Core.TokenCredenti
public virtual System.Threading.Tasks.Task<Azure.Response> DeleteUserAsync(Azure.Communication.CommunicationUserIdentifier communicationUser, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual Azure.Response<Azure.Core.AccessToken> GetToken(Azure.Communication.CommunicationUserIdentifier communicationUser, System.Collections.Generic.IEnumerable<Azure.Communication.Identity.CommunicationTokenScope> scopes, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.Core.AccessToken>> GetTokenAsync(Azure.Communication.CommunicationUserIdentifier communicationUser, System.Collections.Generic.IEnumerable<Azure.Communication.Identity.CommunicationTokenScope> scopes, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual Azure.Response<Azure.Communication.Identity.Models.CommunicationTurnCredentialsResponse> GetTurnCredentials(Azure.Communication.CommunicationUserIdentifier communicationUser, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.Communication.Identity.Models.CommunicationTurnCredentialsResponse>> GetTurnCredentialsAsync(Azure.Communication.CommunicationUserIdentifier communicationUser, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual Azure.Response RevokeTokens(Azure.Communication.CommunicationUserIdentifier communicationUser, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response> RevokeTokensAsync(Azure.Communication.CommunicationUserIdentifier communicationUser, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
}
Expand Down Expand Up @@ -45,3 +47,19 @@ public enum ServiceVersion
public override string ToString() { throw null; }
}
}
namespace Azure.Communication.Identity.Models
{
public partial class CommunicationTurnCredentialsResponse
{
internal CommunicationTurnCredentialsResponse() { }
public System.DateTimeOffset ExpiresOn { get { throw null; } }
public System.Collections.Generic.IReadOnlyList<Azure.Communication.Identity.Models.CommunicationTurnServer> TurnServers { get { throw null; } }
}
public partial class CommunicationTurnServer
{
internal CommunicationTurnServer() { }
public string Credential { get { throw null; } }
public string Urls { get { throw null; } }
public string Username { get { throw null; } }
}
}
11 changes: 5 additions & 6 deletions sdk/communication/Azure.Communication.Identity/samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ description: Samples for the Azure.Communication.Identity client library
Azure Communication Identity is a client library that is used to do operations necessary for identities and tokens.
To get started you will need to have an Azure Subscription. Once you have this you can go into the Azure portal and create Azure Communication Services resource. The page will give you necessary information to be able to use the sample codes here such as connections string, shared access key, etc.

This client library allows to do following operations:
- Generate user tokens that allows the holders to access Azure Communication Services.
- Purchase, configure and release phone numbers.
This client library allows for the following operations:
- Generate user tokens that allow the bearers to access Azure Communication Services.
- Generate TURN server credentials that allow the bearers to access a TURN server for media relay.

#### You can find samples for each of these functions below.
- Generate user tokens [synchronously][sample_identity] or [asynchronously][sample_identity_async]
You can find samples for each of these functions below.
- Generate user tokens and TURN credentials [synchronously][sample_identity] or [asynchronously][sample_identity_async]

<!-- LINKS -->
[sample_identity]: https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/communication/Azure.Communication.Identity/samples/Sample1_CommunicationIdentityClient.md
[sample_identity_async]: https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/communication/Azure.Communication.Identity/samples/Sample1_CommunicationIdentityClientAsync.md
[
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ You will need to store the `identity` that is returned by Azure Communication Se
In the example code snippet below, it is assumed that the token is generated for a user on your application. During token generation you should also pass list of scopes to Azure Communication Services,
so the token generated by Azure Communication Services is authorized only to do things determined by `scopes`. <!-- You can see the full list of scopes [here][scopes]. -->

Every token generated has an expiry date stamped on it. Once the token is expired, you can renew the token by calling the same method.
Every token generated has an expiry date and time stamped on it. Once the token has expired, you can renew the token by calling the same method.

```C# Snippet:CreateCommunicationToken
Response<AccessToken> tokenResponse = client.GetToken(user, scopes: new[] { CommunicationTokenScope.Chat });
Expand All @@ -63,6 +63,26 @@ Console.WriteLine($"Token: {token}");
Console.WriteLine($"Expires On: {expiresOn}");
```

## Generate TURN credentials

The example code snippet below shows how to generate TURN credentials for an Azure Communication user that was created following the steps above.
A set of TURN credentials are returned for the user. Each TURN credential consists of a URL for a TURN server, its corresponding username and a credential.

Every set of TURN credentials has an expiry date and time stamped on it. Once the credentials have expired, you can renew the token by calling the same method.

```C# Snippet:CreateTURNTokenAsync
Response<CommunicationTurnCredentialsResponse> turnTokenResponse = await client.GetTurnCredentialsAsync(user);
DateTimeOffset turnTokenExpiresOn = turnTokenResponse.Value.ExpiresOn;
IReadOnlyList<CommunicationTurnServer> turnServers = turnTokenResponse.Value.TurnServers;
Console.WriteLine($"Expires On: {turnTokenExpiresOn}");
foreach (CommunicationTurnServer turnServer in turnServers)
{
Console.WriteLine($"TURN Url: {turnServer.Urls}");
Console.WriteLine($"TURN Username: {turnServer.Username}");
Console.WriteLine($"TURN Credential: {turnServer.Credential}");
}
```

<!--
To see the full example source files, see:
* [Generate user token][GenerateUserTokenCode]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ so the token generated by Azure Communication Services is authorized only to do

<!-- You can see the full list of scopes TODO: [here](https://github.com/mikben/azure-docs-pr/blob/release-project-spool/articles/project-spool/concepts/identity-model.md).-->

Every token generated has an expiry date stamped on it. Once the token is expired, you can renew the token by calling the same method.
Every token generated has an expiry date and time stamped on it. Once the token has expired, you can renew the token by calling the same method.

```C# Snippet:CreateCommunicationTokenAsync
Response<AccessToken> tokenResponse = await client.GetTokenAsync(user, scopes: new[] { CommunicationTokenScope.Chat });
Expand All @@ -65,6 +65,26 @@ Console.WriteLine($"Token: {token}");
Console.WriteLine($"Expires On: {expiresOn}");
```

## Generate TURN credentials

The example code snippet below shows how to generate TURN credentials for an Azure Communication user that was created following the steps above.
A set of TURN credentials are returned for the user. Each TURN credential consists of a URL for a TURN server, its corresponding username and a credential.

Every set of TURN credentials has an expiry date and time stamped on it. Once the credentials have expired, you can renew the token by calling the same method.

```C# Snippet:CreateTURNTokenAsync
Response<CommunicationTurnCredentialsResponse> turnTokenResponse = await client.GetTurnCredentialsAsync(user);
DateTimeOffset turnTokenExpiresOn = turnTokenResponse.Value.ExpiresOn;
IReadOnlyList<CommunicationTurnServer> turnServers = turnTokenResponse.Value.TurnServers;
Console.WriteLine($"Expires On: {turnTokenExpiresOn}");
foreach (CommunicationTurnServer turnServer in turnServers)
{
Console.WriteLine($"TURN Url: {turnServer.Urls}");
Console.WriteLine($"TURN Username: {turnServer.Username}");
Console.WriteLine($"TURN Credential: {turnServer.Credential}");
}
```

<!--
To see the full example source files, see:
* [Generate user token][GenerateUserTokenCode]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class CommunicationIdentityClient
{
private readonly ClientDiagnostics _clientDiagnostics;
internal CommunicationIdentityRestClient RestClient { get; }
internal CommunicationIdentityRestClient TurnRestClient { get; }

#region public constructors - all argument need null check

Expand Down Expand Up @@ -81,6 +82,7 @@ private CommunicationIdentityClient(string endpoint, HttpPipeline httpPipeline,
{
_clientDiagnostics = new ClientDiagnostics(options);
RestClient = new CommunicationIdentityRestClient(_clientDiagnostics, httpPipeline, endpoint, options.ApiVersion);
TurnRestClient = new CommunicationIdentityRestClient(_clientDiagnostics, httpPipeline, endpoint, "2021-12-12");
}

#endregion
Expand All @@ -90,6 +92,7 @@ protected CommunicationIdentityClient()
{
_clientDiagnostics = null;
RestClient = null;
TurnRestClient = null;
}

/// <summary>Creates a new <see cref="CommunicationUserIdentifier"/>.</summary>
Expand Down Expand Up @@ -289,6 +292,43 @@ public virtual async Task<Response> RevokeTokensAsync(CommunicationUserIdentifie
}
}

/// <summary>Gets a TURN credential for a <see cref="CommunicationUserIdentifier"/>.</summary>
/// <param name="communicationUser">The <see cref="CommunicationUserIdentifier"/> for whom to issue a token.</param>
/// <param name="cancellationToken">The cancellation token to use.</param>
/// <exception cref="RequestFailedException">The server returned an error.</exception>
public virtual Response<CommunicationTurnCredentialsResponse> GetTurnCredentials(CommunicationUserIdentifier communicationUser, CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(CommunicationIdentityClient)}.{nameof(GetTurnCredentials)}");
scope.Start();
try
{
return TurnRestClient.IssueTurnCredentials(communicationUser.Id, cancellationToken);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}

/// <summary>Asynchronously gets a TURN credential for a <see cref="CommunicationUserIdentifier"/>.</summary>
/// <param name="communicationUser">The <see cref="CommunicationUserIdentifier"/> for whom to issue a token.</param>
/// <param name="cancellationToken">The cancellation token to use.</param>
public virtual async Task<Response<CommunicationTurnCredentialsResponse>> GetTurnCredentialsAsync(CommunicationUserIdentifier communicationUser, CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(CommunicationIdentityClient)}.{nameof(GetTurnCredentials)}");
scope.Start();
try
{
return await TurnRestClient.IssueTurnCredentialsAsync(communicationUser.Id, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}

private static T AssertNotNull<T>(T argument, string argumentName)
where T : class
{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading