-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGraphApiService.cs
100 lines (85 loc) · 3.74 KB
/
GraphApiService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Graph;
using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
namespace Garaio.AzureRecipes
{
/// <inheritdoc cref="IGraphApiService"/>
public class GraphApiService
{
private readonly ILogger<GraphApiService> _logger;
private readonly IConfiguration _configuration;
private readonly BuiltInAuthConfig _authConfig;
private readonly Lazy<GraphServiceClient> _graphServiceClient;
public GraphApiService(ILoggerFactory loggerFactory, IConfiguration configuration)
{
_logger = loggerFactory.CreateLogger<GraphApiService>();
_configuration = configuration;
_authConfig = new BuiltInAuthConfig(configuration);
_graphServiceClient = new Lazy<GraphServiceClient>(() =>
{
// Create a client credentials auth provider
var authProvider = new BuiltInAuthProvider(
new[] { Constants.AuthScopes.Graph },
_logger,
_authConfig);
return new GraphServiceClient(authProvider);
});
}
private static bool IsValidEmail(string email)
{
return Regex.IsMatch(email, @"^[^@\s]+@[^@\s]+\.[^@\s]+$", RegexOptions.IgnoreCase);
}
public bool CheckIsEnabled()
{
return _authConfig.IsEnabled && !string.IsNullOrEmpty(_authConfig.ClientId) && !string.IsNullOrEmpty(_authConfig.ClientSecret) && !string.IsNullOrEmpty(_authConfig.TenantId);
}
/// <summary>
/// Requires permission `User.Read.All`
/// </summary>
public async Task<bool> CheckUserExistsInActiveDirectoryAsync(string userId, CancellationToken cancellationToken = default)
{
if (!IsValidEmail(userId))
{
_logger.LogWarning($"Invalid user-id '{userId}' detected");
return false;
}
var resultPage = await _graphServiceClient.Value.Users.Request().Filter($"mail eq '{userId}'").GetAsync(cancellationToken);
var user = resultPage.CurrentPage.FirstOrDefault();
return user != null;
}
/// <summary>
/// Requires permission `User.Invite.All`
/// </summary>
public async Task<string> CreateUserInvitationAsync(string userId, CancellationToken cancellationToken = default)
{
if (!IsValidEmail(userId))
{
_logger.LogWarning($"Invalid user-id '{userId}' detected");
return null;
}
var invitation = new Invitation
{
InvitedUserEmailAddress = userId,
InviteRedirectUrl = _configuration[Public.Constants.Configurations.FrontendUrl],
SendInvitationMessage = false
};
try
{
Invitation result = await _graphServiceClient.Value.Invitations.Request().AddAsync(invitation, cancellationToken);
return result.InviteRedeemUrl;
}
// One typical case for this exception is, when you try to invite users with an email that belongs to the AAD's domain (i.e. existing non-guest AAD users)
catch (ServiceException e)
{
var exMsg = $"Failed to invite user '{userId}' to Active Directory: {e.Message}";
_logger.LogWarning(e, exMsg);
throw new InvalidOperationException(exMsg, e); // Throw neutral exception so that service users don't need to reference Graph library
}
}
}
}