-
Notifications
You must be signed in to change notification settings - Fork 110
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 #6 from AzureAD/security
Added token validation code for development and for publishing
- Loading branch information
Showing
9 changed files
with
203 additions
and
68 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
Microsoft.SCIM.WebHostSample/Controllers/TokenController.cs
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,67 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.IdentityModel.Tokens; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IdentityModel.Tokens.Jwt; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.SCIM.WebHostSample.Controllers | ||
{ | ||
// Controller for generating a bearer token for authorization during testing. | ||
// This is not meant to replace proper Oauth for authentication purposes. | ||
[Route("scim/token")] | ||
[ApiController] | ||
public class TokenController : ControllerBase | ||
{ | ||
private readonly IConfiguration _configuration; | ||
|
||
private const int defaultTokenExpiration = 120; | ||
|
||
public TokenController(IConfiguration Configuration) | ||
{ | ||
_configuration = Configuration; | ||
} | ||
|
||
private string GenerateJSONWebToken() | ||
{ | ||
// Create token key | ||
SymmetricSecurityKey securityKey = | ||
new SymmetricSecurityKey(Encoding.UTF8.GetBytes(this._configuration["Token:IssuerSigningKey"])); | ||
SigningCredentials credentials = | ||
new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256); | ||
|
||
// Set token expiration | ||
DateTime startTime = DateTime.UtcNow; | ||
DateTime expiryTime; | ||
double tokenExpiration; | ||
if (double.TryParse(this._configuration["Token:TokenLifetimeInMins"], out tokenExpiration)) | ||
expiryTime = startTime.AddMinutes(tokenExpiration); | ||
else | ||
expiryTime = startTime.AddMinutes(defaultTokenExpiration); | ||
|
||
// Generate the token | ||
JwtSecurityToken token = | ||
new JwtSecurityToken( | ||
this._configuration["Token:TokenIssuer"], | ||
this._configuration["Token:TokenAudience"], | ||
null, | ||
notBefore: startTime, | ||
expires: expiryTime, | ||
signingCredentials: credentials); | ||
|
||
string result = new JwtSecurityTokenHandler().WriteToken(token); | ||
return result; | ||
} | ||
|
||
[HttpGet] | ||
public ActionResult Get() | ||
{ | ||
string tokenString = this.GenerateJSONWebToken(); | ||
return this.Ok(new { token = tokenString }); | ||
} | ||
|
||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
Microsoft.SCIM.WebHostSample/Microsoft.SCIM.WebHostSample.csproj
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
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