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

Enable authentication to the service using Microsoft account #163

Merged
merged 1 commit into from
Jan 23, 2024
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
@@ -0,0 +1,54 @@
using Microsoft.Graph;
using ServiceAssessmentService.WebApp.Pages.Me;

namespace ServiceAssessmentService.WebApp.Models;

public class GraphUserClient
{

private readonly GraphServiceClient _graphServiceClient;
private readonly ILogger<GraphUserClient> _logger;

private Microsoft.Graph.User? _graphUser = null;

public GraphUserClient(GraphServiceClient graphServiceClient, ILogger<GraphUserClient> logger)
{
_graphServiceClient = graphServiceClient;
_logger = logger;
}

public async Task<Microsoft.Graph.User> GetGraphUser()
{
if (_graphUser == null)
{
_graphUser = await _graphServiceClient.Me.Request().GetAsync();
}

return _graphUser;
}

public async Task<string> GetGraphUserDisplayName()
{
var graphUser = await GetGraphUser();
return graphUser.DisplayName;
}

public async Task<string> GetGraphUserGivenName()
{
var graphUser = await GetGraphUser();
return graphUser.GivenName;
}

public async Task<string> GetGraphUserSurname()
{
var graphUser = await GetGraphUser();
return graphUser.Surname;
}

public async Task<string> GetGraphUserMail()
{
var graphUser = await GetGraphUser();
return graphUser.Mail;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
@page
@model ServiceAssessmentService.WebApp.Pages.Me.IndexModel
@{
ViewData["Title"] = "Home page";

var graphUser = ViewData["GraphUser"] as Microsoft.Graph.User;
}

<div class="main--content">

<div class="main--inside-container">

<div class="govuk-grid-row">
<div class="govuk-grid-column">
<h1 class="govuk-heading-l ">Graph API result</h1>

<ul>
<li>@ViewData["GraphApiResult"]</li>
<li>@graphUser.GivenName</li>

Check warning on line 19 in src/ServiceAssessmentService/ServiceAssessmentService.WebApp/Pages/Me/Index.cshtml

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
<li>@graphUser.Surname</li>
<li>@graphUser.Mail</li>
</ul>
</div>
</div>

<div class="govuk-grid-row">
<div class="govuk-grid-column">
<h3 class="govuk-heading-m">User Properties</h3>

<dl class="govuk-summary-list">
@foreach (var property in Model.User.GetType().GetProperties())
{
<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">
@property.Name
</dt>
<dd class="govuk-summary-list__value">
@property.GetValue(Model.User)
</dd>
</div>
}
</dl>
</div>
</div>

<div class="govuk-grid-row">
<div class="govuk-grid-column">
<h3 class="govuk-heading-m">Claims</h3>

<dl class="govuk-summary-list">
@foreach (var claim in Model.User.Claims)
{
<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">
@claim.Type
</dt>
<dd class="govuk-summary-list__value">
@claim.Value
</dd>
</div>
}
</dl>
</div>
</div>


<div class="govuk-grid-row">
<div class="govuk-grid-column">
<h3 class="govuk-heading-m">User Properties</h3>

<dl class="govuk-summary-list">
@foreach (var property in graphUser.GetType().GetProperties())
{
var value = property.GetValue(graphUser);
if (value is null)
{
continue;
}

<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">
@property.Name
</dt>
<dd class="govuk-summary-list__value">
@value
</dd>
</div>
}
</dl>
</div>
</div>

</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Identity.Web;
using System.Net;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Graph;
using ServiceAssessmentService.WebApp.Models;

namespace ServiceAssessmentService.WebApp.Pages.Me;

[AuthorizeForScopes(ScopeKeySection = "MicrosoftGraph:Scopes")]
public class IndexModel : PageModel
{
private readonly GraphServiceClient _graphServiceClient;
private readonly GraphUserClient _graphUserClient;
private readonly ILogger<IndexModel> _logger;

public IndexModel(ILogger<IndexModel> logger, GraphServiceClient graphServiceClient, GraphUserClient graphUserClient)
{
_logger = logger;
_graphServiceClient = graphServiceClient;
_graphUserClient = graphUserClient;
}

public async Task OnGet()
{
var user = await _graphUserClient.GetGraphUser();
ViewData["GraphUser"] = user;
ViewData["GraphApiResult"] = user.DisplayName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
@using ServiceAssessmentService.Data.Entities
@using GovUk.Frontend.AspNetCore.TagHelpers

@inject SignInManager<ServiceAssessmentServiceWebAppUser> SignInManager
@inject UserManager<ServiceAssessmentServiceWebAppUser> UserManager

@{
// ReSharper disable once Razor.LayoutNotResolved
Expand All @@ -12,10 +10,10 @@

// Use the page title, suffixed by the service name
ViewData["Title"] += " - Service Assessment Service";

// get name of current area
var area = ViewContext.RouteData.Values["area"] as string;

// get name of current controller
var controller = ViewContext.RouteData.Values["controller"] as string;

Expand All @@ -40,14 +38,14 @@
// Helpers, used to determine which navigation item is currently active
var isAreaDefault = (area is null) || ("Home".Equals(area, StringComparison.OrdinalIgnoreCase));
var isAreaBook = ("Book".Equals(area, StringComparison.OrdinalIgnoreCase));


}


@* ReSharper disable once Razor.SectionNotResolved *@
@section Head {
<link rel="stylesheet" href="~/dist/css/site.min.css"/>
<link rel="stylesheet" href="~/dist/css/site.min.css" />
}


Expand All @@ -73,7 +71,7 @@
@* <a href="/" class="govuk-link govuk-link--inverse">Sign out</a> *@
@* </li> *@
@* </ul> *@

<partial name="_LoginPartial" />

<div class="dfe-header__menu">
Expand All @@ -93,28 +91,27 @@
<button class="dfe-header__navigation-close" id="close-menu">
<svg class="dfe-icon dfe-icon__close" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"
aria-hidden="true" focusable="false" width="27" height="27">
<path
d="M13.41 12l5.3-5.29a1 1 0 1 0-1.42-1.42L12 10.59l-5.29-5.3a1 1 0 0 0-1.42 1.42l5.3 5.29-5.3 5.29a1 1 0 0 0 0 1.42 1 1 0 0 0 1.42 0l5.29-5.3 5.29 5.3a1 1 0 0 0 1.42 0 1 1 0 0 0 0-1.42z">
<path d="M13.41 12l5.3-5.29a1 1 0 1 0-1.42-1.42L12 10.59l-5.29-5.3a1 1 0 0 0-1.42 1.42l5.3 5.29-5.3 5.29a1 1 0 0 0 0 1.42 1 1 0 0 0 1.42 0l5.29-5.3 5.29 5.3a1 1 0 0 0 1.42 0 1 1 0 0 0 0-1.42z">
</path>
</svg>
<span class="govuk-visually-hidden">Close menu</span>
</button>
</p>
<ul class="dfe-header__navigation-list">
@if (SignInManager.IsSignedIn(User))
@if (User.Identity.IsAuthenticated)
{
<li class="dfe-header__navigation-item @(isAreaDefault ? "dfe-header__navigation-item--current" : "")">
<a class="dfe-header__navigation-link" asp-page="/Dashboard">
Dashboard
<svg class="dfe-icon dfe-icon__chevron-right" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"
aria-hidden="true" width="34" height="34">
aria-hidden="true" width="34" height="34">
<path d="M15.5 12a1 1 0 0 1-.29.71l-5 5a1 1 0 0 1-1.42-1.42l4.3-4.29-4.3-4.29a1 1 0 0 1 1.42-1.42l5 5a1 1 0 0 1 .29.71z">
</path>
</svg>
</a>
</li>
}
@if (SignInManager.IsSignedIn(User))
@if (User.Identity.IsAuthenticated)
{
@* <li class="dfe-header__navigation-item @(isAreaDefault ? "dfe-header__navigation-item--current" : "")"> *@
@* <a class="dfe-header__navigation-link" asp-page="/Book/Index"> *@
Expand All @@ -127,13 +124,26 @@
@* </a> *@
@* </li> *@
}
@if (SignInManager.IsSignedIn(User))
@if (User.Identity.IsAuthenticated)
{
<li class="dfe-header__navigation-item @(isAreaDefault ? "dfe-header__navigation-item--current" : "")">
<a class="dfe-header__navigation-link" asp-page="/Book/List">
Booking Requests
<svg class="dfe-icon dfe-icon__chevron-right" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"
aria-hidden="true" width="34" height="34">
aria-hidden="true" width="34" height="34">
<path d="M15.5 12a1 1 0 0 1-.29.71l-5 5a1 1 0 0 1-1.42-1.42l4.3-4.29-4.3-4.29a1 1 0 0 1 1.42-1.42l5 5a1 1 0 0 1 .29.71z">
</path>
</svg>
</a>
</li>
}
@if (User.Identity.IsAuthenticated)
{
<li class="dfe-header__navigation-item @(isAreaDefault ? "dfe-header__navigation-item--current" : "")">
<a class="dfe-header__navigation-link" asp-page="/Me/Index">
About Me
<svg class="dfe-icon dfe-icon__chevron-right" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"
aria-hidden="true" width="34" height="34">
<path d="M15.5 12a1 1 0 0 1-.29.71l-5 5a1 1 0 0 1-1.42-1.42l4.3-4.29-4.3-4.29a1 1 0 0 1 1.42-1.42l5 5a1 1 0 0 1 .29.71z">
</path>
</svg>
Expand All @@ -148,7 +158,7 @@


@* ReSharper disable once Razor.SectionNotResolved *@
@section BeforeContent{
@section BeforeContent {
<govuk-phase-banner>
<govuk-phase-banner-tag>Beta</govuk-phase-banner-tag>
This is a new service - your <a href="#" class="govuk-link">feedback</a> will help us to improve it.
Expand Down Expand Up @@ -220,7 +230,7 @@
height="17"
width="41">
<path fill="currentColor"
d="M421.5 142.8V.1l-50.7 32.3v161.1h112.4v-50.7zm-122.3-9.6A47.12 47.12 0 0 1 221 97.8c0-26 21.1-47.1 47.1-47.1 16.7 0 31.4 8.7 39.7 21.8l42.7-27.2A97.63 97.63 0 0 0 268.1 0c-36.5 0-68.3 20.1-85.1 49.7A98 98 0 0 0 97.8 0C43.9 0 0 43.9 0 97.8s43.9 97.8 97.8 97.8c36.5 0 68.3-20.1 85.1-49.7a97.76 97.76 0 0 0 149.6 25.4l19.4 22.2h3v-87.8h-80l24.3 27.5zM97.8 145c-26 0-47.1-21.1-47.1-47.1s21.1-47.1 47.1-47.1 47.2 21 47.2 47S123.8 145 97.8 145"/>
d="M421.5 142.8V.1l-50.7 32.3v161.1h112.4v-50.7zm-122.3-9.6A47.12 47.12 0 0 1 221 97.8c0-26 21.1-47.1 47.1-47.1 16.7 0 31.4 8.7 39.7 21.8l42.7-27.2A97.63 97.63 0 0 0 268.1 0c-36.5 0-68.3 20.1-85.1 49.7A98 98 0 0 0 97.8 0C43.9 0 0 43.9 0 97.8s43.9 97.8 97.8 97.8c36.5 0 68.3-20.1 85.1-49.7a97.76 97.76 0 0 0 149.6 25.4l19.4 22.2h3v-87.8h-80l24.3 27.5zM97.8 145c-26 0-47.1-21.1-47.1-47.1s21.1-47.1 47.1-47.1 47.2 21 47.2 47S123.8 145 97.8 145" />
</svg>
<span class="govuk-footer__licence-description">
All content is available under the
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
@using Microsoft.AspNetCore.Identity
@using ServiceAssessmentService.Data.Entities
@using GovUk.Frontend.AspNetCore.TagHelpers

@inject SignInManager<ServiceAssessmentServiceWebAppUser> SignInManager
@inject UserManager<ServiceAssessmentServiceWebAppUser> UserManager
@using GovUk.Frontend.AspNetCore.TagHelpers

<ul class="dfe-header__action-links">
@if (SignInManager.IsSignedIn(User))
@if (User?.Identity?.IsAuthenticated ?? false)
{
<li class="nav-item">
<a class="govuk-link govuk-link--inverse" asp-area="Identity" asp-page="/Account/Manage/Index" title="Manage">Hello @UserManager.GetUserName(User)!</a>
<span class="govuk-link govuk-link--inverse">Hello @User.Identity.Name!</span>
</li>
<li class="nav-item">
<form id="logoutForm" class="form-inline" asp-area="Identity" asp-page="/Account/Logout" asp-route-returnUrl="/">
<button id="logout" type="submit" class="nav-link btn btn-link text-dark border-0">Logout</button>
</form>
<a class="govuk-link govuk-link--inverse" asp-area="MicrosoftIdentity" asp-controller="Account" asp-action="SignOut">Sign out</a>
</li>
}
else
{
<li class="nav-item">
<a class="govuk-link govuk-link--inverse" asp-area="Identity" asp-page="/Account/Register">Register</a>
</li>
<li class="nav-item">
<a class="govuk-link govuk-link--inverse" asp-area="Identity" asp-page="/Account/Login">Login</a>
<a class="govuk-link govuk-link--inverse" asp-area="MicrosoftIdentity" asp-controller="Account" asp-action="SignIn">Sign in</a>
</li>
}
</ul>
Loading
Loading