-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from Purdue-ACM-SIGAPP/dakshesh-login-test
API Authentication using Okta Auth0 [DO NOT MERGE]
- Loading branch information
Showing
7 changed files
with
138 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,61 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Twilio; | ||
using Twilio.Rest.Verify.V2.Service; | ||
using dotenv.net; | ||
using Microsoft.AspNetCore.Authorization; | ||
|
||
namespace SimpleWebAppReact.Controllers | ||
{ | ||
{ | ||
|
||
[ApiController] | ||
[Route("api/[controller]")] | ||
public class MyController : ControllerBase | ||
{ | ||
[HttpGet("message")] // Define the route for this action | ||
[Authorize] | ||
public IActionResult GetMessage() | ||
{ | ||
return Ok(new { message = "Hello from ASP.NET Core!" }); | ||
} | ||
|
||
// Send Twillio Verification Email | ||
|
||
[HttpGet("send-verification-email")] | ||
public async Task<IActionResult> SendVerificationEmail() | ||
{ | ||
DotEnv.Load(); | ||
|
||
string accountSid = Environment.GetEnvironmentVariable("TWILLIO_ACCOUNT_SID"); | ||
|
||
Console.WriteLine(accountSid); | ||
|
||
string authToken = Environment.GetEnvironmentVariable("TWILLIO_AUTH_TOKEN"); | ||
string testRecipient = Environment.GetEnvironmentVariable("TEST_RECIPIENT"); | ||
string templateId = Environment.GetEnvironmentVariable("TWILLIO_TEMPLATE_ID"); | ||
string testName = Environment.GetEnvironmentVariable("TEST_NAME"); | ||
string serviceSid = Environment.GetEnvironmentVariable("TWILLIO_SERVICE_SID"); | ||
|
||
TwilioClient.Init(accountSid, authToken); | ||
|
||
var verification = await VerificationResource.CreateAsync( | ||
channel: "email", | ||
to: testRecipient, | ||
channelConfiguration: new Dictionary< | ||
string, | ||
Object>() { { "template_id", templateId }, { "from", testRecipient }, { "from_name", testName } }, | ||
pathServiceSid: serviceSid); | ||
|
||
Console.WriteLine(verification.Sid); | ||
if (verification.Status == "pending") | ||
{ | ||
return Ok(new { message = "Verification email sent successfully" }); | ||
} | ||
else | ||
{ | ||
return BadRequest(new { message = "Failed to send verification email" }); | ||
} | ||
} | ||
} | ||
|
||
|
||
} |
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 Microsoft.AspNetCore.Authorization; | ||
namespace SimpleWebAppReact; | ||
public class HasScopeHandler : AuthorizationHandler<HasScopeRequirement> | ||
{ | ||
protected override Task HandleRequirementAsync( | ||
AuthorizationHandlerContext context, | ||
HasScopeRequirement requirement | ||
) { | ||
// If user does not have the scope claim, get out of here | ||
if (!context.User.HasClaim(c => c.Type == "scope" && c.Issuer == requirement.Issuer)) | ||
return Task.CompletedTask; | ||
|
||
|
||
// Split the scopes string into an array | ||
var scopes = context.User | ||
.FindFirst(c => c.Type == "scope" && c.Issuer == requirement.Issuer).Value.Split(' '); | ||
|
||
// Succeed if the scope array contains the required scope | ||
if (scopes.Any(s => s == requirement.Scope)) | ||
context.Succeed(requirement); | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
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,13 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
|
||
public class HasScopeRequirement : IAuthorizationRequirement | ||
{ | ||
public string Issuer { get; } | ||
public string Scope { get; } | ||
|
||
public HasScopeRequirement(string scope, string issuer) | ||
{ | ||
Scope = scope ?? throw new ArgumentNullException(nameof(scope)); | ||
Issuer = issuer ?? throw new ArgumentNullException(nameof(issuer)); | ||
} | ||
} |
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