-
Notifications
You must be signed in to change notification settings - Fork 145
/
InvitationController.cs
197 lines (174 loc) · 7.91 KB
/
InvitationController.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http.Authentication;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using WingTipCommon.Services;
using WingTipGamesWebApplication.Configuration;
using WingTipGamesWebApplication.Repositories;
using WingTipGamesWebApplication.ViewModels.Invitation;
namespace WingTipGamesWebApplication.Controllers
{
public class InvitationController : Controller
{
private static readonly TimeSpan InvitationTokenLifetime = new TimeSpan(1, 0, 0, 0);
private readonly IGameRepository _gameRepository;
private readonly InvitationControllerOptions _options;
private readonly ISmtpService _smtpService;
public InvitationController(IGameRepository gameRepository, IOptions<InvitationControllerOptions> optionsAccessor, ISmtpService smtpService)
{
_gameRepository = gameRepository ?? throw new ArgumentNullException(nameof(gameRepository));
if (optionsAccessor == null)
{
throw new ArgumentNullException(nameof(optionsAccessor));
}
_options = optionsAccessor.Value;
_smtpService = smtpService ?? throw new ArgumentNullException(nameof(smtpService));
}
[HttpGet]
public IActionResult Create()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Create(CreateViewModel viewModel)
{
if (viewModel == null)
{
throw new ArgumentNullException(nameof(viewModel));
}
if (ModelState.IsValid)
{
// Send an invitation message containing a redemption link, which includes a HMAC-based signature, to the "Redeem" action.
// This action validates this redemption link and if it is valid, then it redirects the end user to the "b2c_1a_invitation"
// policy.
if (string.Equals(viewModel.RedemptionMethod, "ApplicationLink"))
{
var redeemUrl = GenerateSignedRedeemUrl(viewModel.EmailAddress);
_smtpService.SendInvitationEmail(viewModel.EmailAddress, redeemUrl);
return View("Created");
}
// Send an invitation message containing a redemption link, which includes a signed JWT with the email address of the
// invited user, to the "b2c_1a_invitation" policy.
if (string.Equals(viewModel.RedemptionMethod, "PolicyLink"))
{
var authenticationProperties = new AuthenticationProperties();
authenticationProperties.Items[Constants.AuthenticationPropertiesKeys.PlayerProfileRegistrationMode] = "Basic";
// Set the invitation lifetime to 1 day.
authenticationProperties.Items[Constants.AuthenticationPropertiesKeys.PolicyTokenLifetime] = InvitationTokenLifetime.ToString();
// The end user might open the redemption link in a different browser or session so disable the cross-site request
// forgery (XSRF) logic in the OpenID Connect authentication middleware.
authenticationProperties.Items[Constants.AuthenticationPropertiesKeys.SkipCorrelation] = true.ToString();
// Set the email address of the invited user.
authenticationProperties.Items[Constants.AuthenticationPropertiesKeys.VerifiedEmail] = viewModel.EmailAddress;
authenticationProperties.RedirectUri = Url.Action("Redeemed", "Invitation");
await HttpContext.Authentication.ChallengeAsync(Constants.PolicyIds.InvitationLink, authenticationProperties);
return View("Created");
}
throw new InvalidOperationException();
}
return View(viewModel);
}
[HttpGet]
public async Task<IActionResult> Redeem(
[Bind(Prefix = "email")] string emailAddress,
[Bind(Prefix = "nbf")] long notBefore,
[Bind(Prefix = "exp")] long expires,
[Bind(Prefix = "sig")] string signature)
{
if (ValidateSignedRedeemUrl(
emailAddress,
notBefore,
expires,
signature))
{
var authenticationProperties = new AuthenticationProperties();
authenticationProperties.Items[Constants.AuthenticationPropertiesKeys.PlayerProfileRegistrationMode] = "Basic";
// Set the email address of the invited user.
authenticationProperties.Items[Constants.AuthenticationPropertiesKeys.VerifiedEmail] = emailAddress;
authenticationProperties.RedirectUri = Url.Action("Redeemed", "Invitation");
await HttpContext.Authentication.ChallengeAsync(Constants.PolicyIds.InvitationRedirect, authenticationProperties);
return new EmptyResult();
}
return View("RedeemError");
}
[HttpGet]
public async Task<IActionResult> Redeemed()
{
var newReleaseGames = await _gameRepository.GetNewReleaseGamesAsync();
var viewModel = new RedeemedViewModel
{
Game = newReleaseGames.First()
};
return View(viewModel);
}
private string GenerateRedeemUrl(
string emailAddress,
long notBefore,
long expires,
string signature)
{
var actionContext = new UrlActionContext
{
Protocol = "https",
Controller = "Invitation",
Action = "Redeem",
Values = new
{
email = emailAddress,
nbf = notBefore,
exp = expires,
sig = signature
}
};
return Url.Action(actionContext);
}
private string GenerateSignedRedeemUrl(string emailAddress)
{
var now = DateTime.UtcNow;
var notBefore = EpochTime.GetIntDate(now);
var expires = EpochTime.GetIntDate(now.Add(InvitationTokenLifetime));
using (var hashAlgorithm = new HMACSHA256(Encoding.UTF8.GetBytes(_options.Key)))
{
var signature = Convert.ToBase64String(
hashAlgorithm.ComputeHash(
Encoding.UTF8.GetBytes(
GenerateRedeemUrl(
emailAddress,
notBefore,
expires,
string.Empty))));
return GenerateRedeemUrl(
emailAddress,
notBefore,
expires,
signature);
}
}
private bool ValidateSignedRedeemUrl(
string emailAddress,
long notBefore,
long expires,
string thisSignature)
{
using (var hashAlgorithm = new HMACSHA256(Encoding.UTF8.GetBytes(_options.Key)))
{
var thatSignature = Convert.ToBase64String(
hashAlgorithm.ComputeHash(
Encoding.UTF8.GetBytes(
GenerateRedeemUrl(
emailAddress,
notBefore,
expires,
string.Empty))));
var now = EpochTime.GetIntDate(DateTime.UtcNow);
return string.Equals(thatSignature, thisSignature) && now >= notBefore && now < expires;
}
}
}
}