Skip to content

Commit

Permalink
added few more test pages
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahmoud Swehli committed Jul 20, 2023
1 parent 92e7c12 commit 7210578
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public record AzureAdB2cOptions

public string MagicLinkFlowUrl { get; init; } = string.Empty;

public string RedirectUrl { get; init; } = string.Empty;

public string TenantId { get; init; } = string.Empty;

public string TokenIssuer { get; init; } = string.Empty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,9 @@
<Folder Include="Services\" />
<Folder Include="Models\" />
<Folder Include="Mappers\" />
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<Content Remove="wwwroot\" />
</ItemGroup>
</Project>
12 changes: 6 additions & 6 deletions example/Muljin.B2CMagicLink.Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseSwagger();
app.UseSwaggerUI();


app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.UseDefaultFiles();
app.UseStaticFiles();

app.Run();

11 changes: 8 additions & 3 deletions example/Muljin.B2CMagicLink.Example/Services/EmailService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Web;
using Microsoft.Extensions.Options;
using Muljin.B2CMagicLink.Example.Models;
using SendGrid;
Expand All @@ -8,12 +9,14 @@ namespace Muljin.B2CMagicLink.Example.Services
{
public class EmailService
{
private static string magicLinkFlowUrl = "https://Muljin.b2clogin.com/Muljin.onmicrosoft.com/oauth2/v2.0/authorize?p=B2C_1A_SIGNIN_WITH_EMAIL&client_id=64257a7a-3240-4021-8860-45af0bbd6734&nonce=defaultNonce&redirect_uri=https%3A%2F%2Fjwt.ms&scope=openid&response_type=code&prompt=login";
private static string magicLinkFlowUrl = "https://Muljin.b2clogin.com/Muljin.onmicrosoft.com/oauth2/v2.0/authorize?p=B2C_1A_SIGNIN_WITH_EMAIL&client_id=64257a7a-3240-4021-8860-45af0bbd6734&nonce=defaultNonce&redirect_uri={0}&scope=openid&response_type=code";
private readonly SendGridOptions _sendGridOptions;

public EmailService(IOptions<SendGridOptions> sendGridOptions)
public EmailService(IOptions<SendGridOptions> sendGridOptions,
IOptions<AzureAdB2cOptions> azureAdB2cOptions)
{
_sendGridOptions = sendGridOptions.Value ?? throw new ArgumentNullException(nameof(sendGridOptions));
magicLinkFlowUrl = magicLinkFlowUrl.Replace("{0}", HttpUtility.HtmlEncode(azureAdB2cOptions.Value.RedirectUrl));
}

public async Task SendMagicLinkAsync(string email, string token)
Expand All @@ -27,7 +30,9 @@ public async Task SendMagicLinkAsync(string email, string token)

var content = $"To login, goto {magicLinkFlowUrl}&id_token_hint={token}";

var msg = MailHelper.CreateSingleEmail(from, to, "Your Muljin Magic Link Example login link", content, content);
var htmlContent = $"To login, <a href=\"{magicLinkFlowUrl}&id_token_hint={token}\"> click here </a>";

var msg = MailHelper.CreateSingleEmail(from, to, "Your Muljin Magic Link Example login link", content, htmlContent);


var client = new SendGridClient(new SendGridClientOptions()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Microsoft.Extensions.Options;
using Muljin.B2CMagicLink.Example.Models;

namespace Muljin.B2CMagicLink.Example.Services
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AzureAdB2c": {
"RedirectUrl": "https://localhost:7004/authresult.html"
}
}

3 changes: 2 additions & 1 deletion example/Muljin.B2CMagicLink.Example/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"ClientId": "64257a7a-3240-4021-8860-45af0bbd6734",
"DomainOrig": "muljin.onmicrosoft.com",
"TenantId": "14182de3-6b9b-4138-a0fa-e4107db293e5",
"TokenIssuer": "https://muljin.onmicrosoft.com/14182de3-6b9b-4138-a0fa-e4107db293e5/v2.0/"
"TokenIssuer": "https://muljin.onmicrosoft.com/14182de3-6b9b-4138-a0fa-e4107db293e5/v2.0/",
"RedirectUrl": "https://muljinmagiclinkexample.azurewebsites.net/authresult.html"
},
"MagicLink": {
"KeyVault": {
Expand Down
56 changes: 56 additions & 0 deletions example/Muljin.B2CMagicLink.Example/wwwroot/authresult.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Muljin Magic Link Tester</title>
<script type="text/javascript" src="https://alcdn.msauth.net/browser/2.35.0/js/msal-browser.min.js"></script>
</head>
<body>
<h1>Auth results:</h1>
<div id="output">...</div>

<script>
var muljin = muljin || {};
const msalConfig = {
auth: {
clientId: '64257a7a-3240-4021-8860-45af0bbd6734',
authority: 'https://muljin.b2clogin.com/tfp/14182de3-6b9b-4138-a0fa-e4107db293e5/B2C_1A_SIGNIN_WITH_EMAIL',
knownAuthorities: ['https://muljin.b2clogin.com/tfp/14182de3-6b9b-4138-a0fa-e4107db293e5/B2C_1A_SIGNIN_WITH_EMAIL']
}
};

function loadMsal(){
muljin.output = document.querySelector('#output');
muljin.msalInstance = new msal.PublicClientApplication(msalConfig);
muljin.msalInstance.initialize().then(checkForCode);
}

function checkForCode(){
muljin.output.innerHTML = "Checking for code...";
var code = window.location.search.split('code=')[1];
if(code !=null && code != undefined){
console.log(code);
muljin.output.innerHTML = "Code found. Exchanging for token...";
exchangeCodeForToken(code);
}
else{
muljin.output.innerHTML = "No code found";
}
}

function exchangeCodeForToken(code){
muljin.msalInstance.acquireTokenByCode({code: code, scopes: ['openid'], redirectUri: "https://jwt.ms"}).then(function(response){
console.log(response);
muljin.output.innerHTML = "Token received: " + response.accessToken;
})
.catch(function(error){
console.log(error);
muljin.output.innerHTML = "Error: " + error;
});
}

window.addEventListener('load', loadMsal);
</script>
</body>
</html>
89 changes: 89 additions & 0 deletions example/Muljin.B2CMagicLink.Example/wwwroot/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Muljin Magic Link Tester</title>
</head>
<body>
<h2>Enter your email above to get a magic link</h2>
<div>
<input type="email" >
<button>Login</button>
<div id="output"></div>
</div>

<script>
var muljin = muljin || {};

(function()
{

function setup(){
const button = document.querySelector('button');
const input = document.querySelector('input');
const output = document.querySelector('#output');

var m = new MagicLinkExampe(button, input, output);
}

function MagicLinkExampe(button, input, output)
{
this.button = button;
this.input = input;
this.output = output;
this.called = false;
this.button.addEventListener('click', this.onClick.bind(this));
console.log("Ready");
}

MagicLinkExampe.prototype.onClick = function(event)
{
console.log("Called onclick");
if (event) {
event.preventDefault();
}

if(this.called){
return;
}
this,output.innerHTML = "Sending magic link to " + this.input.value;

this.called = true;
this.button.disabled = true;
this.input.disabled = true;

//xhr request
var req = new XMLHttpRequest();
//on state change events. Assume 200 is only correct status, anything over 400 is an error. everything else ignored for now.
var me = this;
req.onreadystatechange = function () {
if (req.status === 200) {
this.called = false;
this.output.innerHTML = "Request sent. Please check your email.";
} else if (req.status >= 400) {
this.called = false;
this.output.innerHTML = "Sorry there was an error. Please ensure your email was correct";
}
}.bind(me);

var curUrl = window.location.href;
curUrl = curUrl.replace("index.html", "");
if(!curUrl.endsWith("/")){
curUrl += "/";
}
curUrl += "auth/signin?email=" + this.input.value;

req.open("GET", curUrl);
// req.setRequestHeader("Content-type", "application/json");
req.send();
}

window.addEventListener('load', setup);

})();

</script>
</body>
</html>

0 comments on commit 7210578

Please sign in to comment.