Skip to content

Commit

Permalink
Merge pull request #57 from Purdue-ACM-SIGAPP/claim-email
Browse files Browse the repository at this point in the history
Add email to user claims
  • Loading branch information
AndrewZacharyLiu authored Jan 29, 2025
2 parents 8434a22 + 4d82257 commit f498765
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
30 changes: 24 additions & 6 deletions Controllers/AuthController.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
using RestSharp;
using Microsoft.AspNetCore.Mvc;

namespace SimpleWebAppReact.Controllers;

using Microsoft.AspNetCore.Mvc;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

/* A work in progress controller that wraps basic Okta provided endpoints, such as /token. will be only used for development */
[ApiController]
[Route("api/[controller]")]
Expand Down Expand Up @@ -85,6 +80,29 @@ public IActionResult GetUserRoles()
Roles = roles
});
}

[HttpGet("email")]
[Authorize]
public IActionResult GetUserEmail()
{
var email = User.Claims
.FirstOrDefault(c => c.Type == ClaimTypes.Email)?
.Value;

if (email == null)
{
return NotFound(new
{
Message = "No email found for the user."
});
}

return Ok(new
{
Message = "User email retrieved successfully.",
Email = email
});
}

[Authorize(Roles = "test")]
[HttpGet("gatekeep-test")]
Expand Down
7 changes: 7 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@
{
claimsIdentity.AddClaim(new Claim(ClaimTypes.Role, role));
}

// Add email
var emailClaim = claimsIdentity.FindFirst("https://my-app.example.com/email");
if (emailClaim != null)
{
claimsIdentity.AddClaim(new Claim(ClaimTypes.Email, emailClaim.Value));
}
}
}
};
Expand Down

0 comments on commit f498765

Please sign in to comment.