Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Smaller) PR to add MicrosoftGraphClientService to the new API. #434

Merged
merged 34 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
2cf1c9a
Initial commit (does not build)
jmprieur Aug 3, 2020
b6dcf59
Updating Web API.
jmprieur Aug 4, 2020
fdc7004
Improving the API
jmprieur Aug 4, 2020
4d3e535
Improving the API.
jmprieur Aug 4, 2020
96d643e
Updating unit tests so that they build
jmprieur Aug 4, 2020
d81a3cb
Merge branch 'master' into jmprieur/WIPNewApi
jmprieur Aug 4, 2020
aa71f45
Renamings an clean-up discussed with DevDiv
jmprieur Aug 5, 2020
f84bc4b
More renaming
jmprieur Aug 5, 2020
2f7ad84
Test fix.
pmaytak Aug 5, 2020
e2efa7c
Renaming of more overrides
jmprieur Aug 8, 2020
2c8c092
Updating the templates
jmprieur Aug 8, 2020
e7a3c10
initial commit api w/microsoftIdentity
jennyf19 Aug 9, 2020
77de4a0
merge conflict
jennyf19 Aug 9, 2020
6bc76ba
merge conflicts + update templates
jennyf19 Aug 9, 2020
7d56096
Merge from master
jmprieur Aug 9, 2020
83a2921
Making the templates work with 0.3.*-*
jmprieur Aug 9, 2020
d5c42d9
Merge branch 'jmprieur/WIPNewApi' into jennyf/newAPI
jennyf19 Aug 10, 2020
242e5c4
few more updates
jennyf19 Aug 10, 2020
e493c5a
Make GetTokenForAppAsync less confusing and allow to pass tenantId #4…
jmprieur Aug 10, 2020
eb198b2
fix tests
jennyf19 Aug 10, 2020
843d416
add xml comments
jennyf19 Aug 10, 2020
d71597a
Merge branch 'jmprieur/WIPNewApi' into jennyf/newAPI
jennyf19 Aug 10, 2020
01fd179
few spelling changes
jennyf19 Aug 11, 2020
e6f346b
renaming of CallsWebApi to EnableTokenAcquisitionToCallDownstreamApi
jennyf19 Aug 11, 2020
5283a23
merge conflict
jennyf19 Aug 11, 2020
fbffb71
- Adding a missing renaming for AddMicrosoftWebApp => AddMicrosoftIde…
jmprieur Aug 12, 2020
3c71db7
Fixing the TodoListService controller in WebAppCallsWebApiCallsGraph
jmprieur Aug 12, 2020
ee38779
Smaller PR for MicrosoftGraphClientService
jmprieur Aug 12, 2020
af2f9fa
Merge branch 'jennyf/newAPI' into jennyf/newApiPlusGraphService
jmprieur Aug 12, 2020
14e1f35
Merge from master
jmprieur Aug 12, 2020
789fd51
fix PR feedback for xml comments and constants (#442)
jennyf19 Aug 13, 2020
0aed39a
Addressing PR feedback
jmprieur Aug 13, 2020
ba40cf3
Merge branch 'jennyf/newApiPlusGraphService' of https://github.com/Az…
jmprieur Aug 13, 2020
7194065
Merge branch 'master' into jennyf/newApiPlusGraphService
jmprieur Aug 13, 2020
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
7 changes: 6 additions & 1 deletion src/Microsoft.Identity.Web/Constants/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,13 @@ internal static class Constants
public const string Bearer = "Bearer";
public const string LoginHint = "loginHint";
public const string DomainHint = "domainHint";
public const string Authorization = "Authorization";

// Blazor challenge uri
// Blazor challenge URI
public const string BlazorChallengeUri = "MicrosoftIdentity/Account/Challenge?redirectUri=";

// Microsoft Graph
public const string UserReadScope = "user.read";
public const string GraphBaseUrlV1 = "https://graph.microsoft.com/v1.0";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ internal static class IDWebErrorMessage
public const string UnauthenticatedUser = "IDW10204:The user is unauthenticated. The HttpContext does not contain any claims. ";
public const string BlazorServerBaseUriNotSet = "IDW10205: Using Blazor server but the base URI was not properly set. ";
public const string BlazorServerUserNotSet = "IDW10206: Using Blazor server but the user was not properly set. ";
public const string CalledApiScopesAreNull = "IDW10207: The CalledApiScopes cannot be null. ";

// Token Validation IDW10300 = "IDW10300:"
public const string IssuerMetadataUrlIsRequired = "IDW10301: Azure AD Issuer metadata address URL is required. ";
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.Identity.Web/Microsoft.Identity.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Graph" Version="3.9.0" />
<PackageReference Include="Microsoft.Identity.Client" Version="4.17.1" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.164">
<PrivateAssets>all</PrivateAssets>
Expand Down
61 changes: 61 additions & 0 deletions src/Microsoft.Identity.Web/Microsoft.Identity.Web.xml

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

namespace Microsoft.Identity.Web
{
/// <summary>
/// Options passed-in to call Microsoft Graph.
/// </summary>
public class MicrosoftGraphOptions
{
/// <summary>
/// Base URL for the Microsoft Graph API. By default: <c>"https://graph.microsoft.com/v1.0/"</c>
/// but it can be changed to use the Microsoft Graph Beta endpoint or national cloud versions
/// of MicrosoftGraph.
/// </summary>
public string BaseUrl { get; set; } = "https://graph.microsoft.com/v1.0/";

/// <summary>
/// Space separated scopes used to call Microsoft Graph,
/// for instance <c>user.read mail.read</c>.
/// </summary>
public string? Scopes { get; set; } = "user.read";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.Graph;

namespace Microsoft.Identity.Web
{
/// <summary>
/// Extensions methods on a MicrosoftIdentityAppCallingWebApiAuthenticationBuilder builder
/// to add support to call Microsoft Graph.
/// </summary>
public static class MicrosoftGraphServiceExtensions
{
/// <summary>
/// Add support to call Microsoft Graph. From a named option and a configuration section.
/// </summary>
/// <param name="builder">Builder.</param>
/// <param name="configurationSection">Configuration section.</param>
/// <returns>The builder to chain.</returns>
public static MicrosoftIdentityAppCallsWebApiAuthenticationBuilder AddMicrosoftGraphServiceClient(
this MicrosoftIdentityAppCallsWebApiAuthenticationBuilder builder,
IConfigurationSection configurationSection)
{
return builder.AddMicrosoftGraphServiceClient(
options => configurationSection.Bind(options));
}

/// <summary>
/// Add support to call Microsoft Graph. From a base Graph URL and a default scope.
/// </summary>
/// <param name="builder">Builder.</param>
/// <param name="graphBaseUrl">Named instance of option.</param>
/// <param name="defaultScopes">Configuration section.</param>
/// <returns>The builder to chain.</returns>
public static MicrosoftIdentityAppCallsWebApiAuthenticationBuilder AddMicrosoftGraphServiceClient(
this MicrosoftIdentityAppCallsWebApiAuthenticationBuilder builder,
string graphBaseUrl = Constants.GraphBaseUrlV1,
string defaultScopes = Constants.UserReadScope)
{
return builder.AddMicrosoftGraphServiceClient(
options =>
{
options.BaseUrl = graphBaseUrl;
options.Scopes = defaultScopes;
});
}

/// <summary>
/// Add support to call Microsoft Graph. From a named options and a configuration method.
/// </summary>
/// <param name="builder">Builder.</param>
/// <param name="configureMicrosoftGraphOptions">Method to configure the options.</param>
/// <returns>The builder to chain.</returns>
public static MicrosoftIdentityAppCallsWebApiAuthenticationBuilder AddMicrosoftGraphServiceClient(
this MicrosoftIdentityAppCallsWebApiAuthenticationBuilder builder,
Action<MicrosoftGraphOptions> configureMicrosoftGraphOptions)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

// https://docs.microsoft.com/en-us/dotnet/standard/microservices-architecture/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests
builder.Services.AddOptions<MicrosoftGraphOptions>().Configure(configureMicrosoftGraphOptions);
builder.Services.AddTokenAcquisition(true);

builder.Services.AddSingleton<GraphServiceClient, GraphServiceClient>(serviceProvider =>
{
var tokenAquisitionService = serviceProvider.GetRequiredService<ITokenAcquisition>();
var options = serviceProvider.GetRequiredService<IOptions<MicrosoftGraphOptions>>();

var microsoftGraphOptions = options.Value;
if (microsoftGraphOptions.Scopes == null)
{
throw new ArgumentException(IDWebErrorMessage.CalledApiScopesAreNull);
}
jmprieur marked this conversation as resolved.
Show resolved Hide resolved

string graphBaseUrl = microsoftGraphOptions.BaseUrl;
string[] initialScopes = microsoftGraphOptions.Scopes.Split(' ');

GraphServiceClient client = string.IsNullOrWhiteSpace(graphBaseUrl) ?
new GraphServiceClient(new TokenAcquisitionCredentialProvider(tokenAquisitionService, initialScopes)) :
new GraphServiceClient(graphBaseUrl, new TokenAcquisitionCredentialProvider(tokenAquisitionService, initialScopes));
return client;
});
return builder;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Collections.Generic;
using System.Globalization;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Graph;

namespace Microsoft.Identity.Web
{
/// <summary>
/// Authentication provider based on ITokenAcquisition.
/// </summary>
jmprieur marked this conversation as resolved.
Show resolved Hide resolved
internal class TokenAcquisitionCredentialProvider : IAuthenticationProvider
{
public TokenAcquisitionCredentialProvider(ITokenAcquisition tokenAcquisition, IEnumerable<string> initialScopes)
{
_tokenAcquisition = tokenAcquisition;
_initialScopes = initialScopes;
}

private ITokenAcquisition _tokenAcquisition;
private IEnumerable<string> _initialScopes;

/// <summary>
/// Adds a bearer header to an HttpRequestMessage.
/// </summary>
/// <param name="request">HttpRequest message to authenticate.</param>
/// <returns>A Task (as this is an async method).</returns>
public async Task AuthenticateRequestAsync(HttpRequestMessage request)
{
request.Headers.Add(
Constants.Authorization,
string.Format(
CultureInfo.InvariantCulture,
"{0}{1}",
Constants.Bearer,
await _tokenAcquisition.GetAccessTokenForUserAsync(_initialScopes).ConfigureAwait(false)));
}
}
}
1 change: 0 additions & 1 deletion tests/B2CWebAppCallsWebApi/Client/TodoListClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.DataProtection.Abstractions" Version="3.1.1" />
<PackageReference Include="Microsoft.Graph" Version="1.16.0" />
<PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
</ItemGroup>

Expand Down

This file was deleted.

This file was deleted.

6 changes: 2 additions & 4 deletions tests/BlazorServerCallsGraph/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@ public Startup(IConfiguration configuration)
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
string[] scopes = Configuration.GetValue<string>("CalledApi:CalledApiScopes")?.Split(' ');
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(Configuration, "AzureAd")
.EnableTokenAcquisitionToCallDownstreamApi(scopes)
.EnableTokenAcquisitionToCallDownstreamApi()
.AddMicrosoftGraphServiceClient(Configuration.GetSection("GraphBeta"))
.AddInMemoryTokenCaches();

services.AddDownstreamWebApiService(Configuration);
services.AddMicrosoftGraph(scopes,
Configuration.GetValue<string>("CalledApi:CalledApiUrl"));
services.AddControllersWithViews()
.AddMicrosoftIdentityUI();

Expand Down
4 changes: 4 additions & 0 deletions tests/BlazorServerCallsGraph/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
// To call an API
"ClientSecret": "secret-goes-here"
},
"GraphBeta": {
"BaseUrl": "https://graph.microsoft.com/beta",
"Scopes": "user.read"
},
"CalledApi": {
/*
'CalledApiScopes' contains space separated scopes of the Web API you want to call. This can be:
Expand Down
1 change: 0 additions & 1 deletion tests/BlazorServerCallsGraph/blazor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<UserSecretsId>66e5c3c7-f757-4032-bfcf-68bd81948618</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Graph" Version="3.8.0" />
<ProjectReference Include="..\..\src\Microsoft.Identity.Web.UI\Microsoft.Identity.Web.UI.csproj" />
<ProjectReference Include="..\..\src\Microsoft.Identity.Web\Microsoft.Identity.Web.csproj" />
</ItemGroup>
Expand Down
Loading