using Microsoft.Graph;
using Microsoft.Teams.Api.Activities;
using Microsoft.Teams.Apps;
namespace Microsoft.Teams.Extensions.Graph;
public static class ContextExtensions
{
///
/// Get user's graph client from the context.
///
/// The activity type
/// The context object
/// The graph client scoped to the user's token
/// If the user token doesn't exist on the context. That is, if the user is not signed in.
public static GraphServiceClient GetUserGraphClient(this IContext context) where TActivity : IActivity
{
var userToken = context.UserGraphToken;
if (userToken is null)
{
throw new InvalidOperationException("context.UserGraphToken is null. Ensure the user is signed in and the token is available in the context.");
}
if (context.Extra.TryGetValue("UserGraphClient", out var client) && client is GraphServiceClient graphClient)
{
return graphClient;
}
var userGraphTokenProvider = Azure.Core.DelegatedTokenCredential.Create((context, _) =>
{
return new Azure.Core.AccessToken(userToken.ToString(), userToken.Token.ValidTo);
});
graphClient = new GraphServiceClient(userGraphTokenProvider);
context.Extra["UserGraphClient"] = graphClient;
return graphClient;
}
}