-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Update] Remove SmallsOnline.MsGraphClient dependency (#54)
* Add classes for configuring the GraphClient * Remove references to SmallsOnline.MsGraphClient GraphClientService no longers uses this. * Update XML doc text * Remove SmallsOnline.MsGraphClient package * Add methods for authenticating the service * Add method for sending API calls to MS Graph * Restructure files / update API call implementation * Update GraphClientService config in AdminConsole * Add logic for when "Show users" is null * Update GraphClientService config in FunctionApp * Make ConnectAsync private * Make GetAuthTokenAsync private * Add XML docs to new methods
- Loading branch information
1 parent
d2353f2
commit b513861
Showing
17 changed files
with
317 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace SmallsOnline.PasswordExpirationNotifier.Lib.Models.Graph; | ||
|
||
/// <summary> | ||
/// Holds the configuration for the Microsoft Graph API client used in <see cref="SmallsOnline.PasswordExpirationNotifier.Lib.Services.GraphClientService" />. | ||
/// </summary> | ||
public class GraphClientConfig : IGraphClientConfig | ||
{ | ||
/// <inheritdoc /> | ||
public string ClientId { get; set; } = null!; | ||
|
||
/// <inheritdoc /> | ||
public string TenantId { get; set; } = null!; | ||
|
||
/// <inheritdoc /> | ||
public string[] ApiScopes { get; set; } = null!; | ||
|
||
/// <inheritdoc /> | ||
public IGraphClientCredential Credential { get; set; } = null!; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System.Security.Cryptography.X509Certificates; | ||
|
||
namespace SmallsOnline.PasswordExpirationNotifier.Lib.Models.Graph; | ||
|
||
/// <summary> | ||
/// Holds the credentials for authenticating with the Microsoft Graph API. | ||
/// </summary> | ||
public class GraphClientCredential : IGraphClientCredential | ||
{ | ||
public GraphClientCredential(GraphClientCredentialType credentialType, string clientSecret) | ||
{ | ||
CredentialType = credentialType; | ||
ClientSecret = clientSecret; | ||
} | ||
|
||
public GraphClientCredential(GraphClientCredentialType credentialType, X509Certificate2 clientCertificate) | ||
{ | ||
CredentialType = credentialType; | ||
ClientCertificate = clientCertificate; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public GraphClientCredentialType CredentialType { get; } | ||
|
||
/// <inheritdoc /> | ||
public string? ClientSecret { get; } | ||
|
||
/// <inheritdoc /> | ||
public X509Certificate2? ClientCertificate { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace SmallsOnline.PasswordExpirationNotifier.Lib.Models.Graph; | ||
|
||
/// <summary> | ||
/// The type of credential to use for authenticating an Azure AD app with the Microsoft Graph API. | ||
/// </summary> | ||
public enum GraphClientCredentialType | ||
{ | ||
/// <summary> | ||
/// The app uses a client secret for authentication. | ||
/// </summary> | ||
ClientSecret, | ||
|
||
/// <summary> | ||
/// The app uses a certificate for authentication. | ||
/// </summary> | ||
ClientCertificate | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
namespace SmallsOnline.PasswordExpirationNotifier.Lib.Models.Graph; | ||
|
||
/// <summary> | ||
/// Interface for configuring the Microsoft Graph client in <see cref="SmallsOnline.PasswordExpirationNotifier.Lib.Services.GraphClientService"/>. | ||
/// </summary> | ||
public interface IGraphClientConfig | ||
{ | ||
/// <summary> | ||
/// The client ID of the Azure AD app. | ||
/// </summary> | ||
string ClientId { get; set; } | ||
|
||
/// <summary> | ||
/// The tenant ID of the Azure AD app. | ||
/// </summary> | ||
string TenantId { get; set; } | ||
|
||
/// <summary> | ||
/// The API scopes to request. | ||
/// </summary> | ||
string[] ApiScopes { get; set; } | ||
|
||
/// <summary> | ||
/// The credential to use for authenticating with the Microsoft Graph API. | ||
/// </summary> | ||
IGraphClientCredential Credential { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System.Security.Cryptography.X509Certificates; | ||
|
||
namespace SmallsOnline.PasswordExpirationNotifier.Lib.Models.Graph; | ||
|
||
/// <summary> | ||
/// Interface for holding credentials for authenticating with the Microsoft Graph API. | ||
/// </summary> | ||
public interface IGraphClientCredential | ||
{ | ||
/// <summary> | ||
/// The type of the credential. | ||
/// </summary> | ||
GraphClientCredentialType CredentialType { get; } | ||
|
||
/// <summary> | ||
/// The client secret for the app. | ||
/// </summary> | ||
string? ClientSecret { get; } | ||
|
||
/// <summary> | ||
/// The certificate for the app. | ||
/// </summary> | ||
X509Certificate2? ClientCertificate { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/Lib/Services/GraphClientService/authentication/ConnectAsync.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
namespace SmallsOnline.PasswordExpirationNotifier.Lib.Services; | ||
|
||
public partial class GraphClientService | ||
{ | ||
/// <summary> | ||
/// Connects to the Graph API and/or refreshes the authentication token if necessary. | ||
/// </summary> | ||
private async Task ConnectAsync() | ||
{ | ||
// Invert the current value of _isConnected to determine if we need to connect. | ||
bool needsToConnect = !_isConnected; | ||
|
||
// If we already have an authentication token, check if it's expired. | ||
// If it is, we need to set the value for 'needsToConnect' to true to get a new token. | ||
if (_authToken is not null) | ||
{ | ||
if (DateTimeOffset.Now >= _authToken.ExpiresOn) | ||
{ | ||
needsToConnect = true; | ||
} | ||
} | ||
|
||
// If needed, get a new authentication token to connect | ||
// to the Graph API. | ||
if (needsToConnect) | ||
{ | ||
_authToken = await GetAuthTokenAsync(); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/Lib/Services/GraphClientService/authentication/GetAuthTokenAsync.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Microsoft.Identity.Client; | ||
using SmallsOnline.PasswordExpirationNotifier.Lib.Models.Graph; | ||
|
||
namespace SmallsOnline.PasswordExpirationNotifier.Lib.Services; | ||
|
||
public partial class GraphClientService | ||
{ | ||
/// <summary> | ||
/// Get an authentication token to connect to the Graph API. | ||
/// </summary> | ||
/// <returns><see cref="AuthenticationResult" /></returns> | ||
private async Task<AuthenticationResult> GetAuthTokenAsync() | ||
{ | ||
AuthenticationResult? authToken = await _confidentialClientApplication | ||
.AcquireTokenForClient(_apiScopes) | ||
.ExecuteAsync(); | ||
|
||
return authToken; | ||
} | ||
} |
Oops, something went wrong.