-
-
Notifications
You must be signed in to change notification settings - Fork 37
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 #61 from ignatandrei/AddingAuthorizationWithBearer…
…Token adding registration
- Loading branch information
Showing
3 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/NetCore2Blockly/TestBlocklyHtml/Authorization/AuthorizationToken.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,38 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.IdentityModel.Tokens; | ||
using System; | ||
using System.IdentityModel.Tokens.Jwt; | ||
using System.Security.Claims; | ||
using System.Text; | ||
|
||
namespace TestBlocklyHtml.Authorization | ||
{ | ||
public class AuthorizationToken | ||
{ | ||
private readonly IConfiguration configuration; | ||
|
||
public AuthorizationToken(IConfiguration configuration) | ||
{ | ||
this.configuration = configuration; | ||
} | ||
|
||
public string GenerateFrom(string secretCode) | ||
{ | ||
var tokenHandler = new JwtSecurityTokenHandler(); | ||
var key = Encoding.ASCII.GetBytes(configuration["ApplicationSecret"]); | ||
var tokenDescriptor = new SecurityTokenDescriptor | ||
{ | ||
Subject = new ClaimsIdentity(new[]{ | ||
new Claim(ClaimTypes.Sid, secretCode) | ||
}), | ||
Expires = DateTime.Today.AddYears(100), | ||
SigningCredentials = new SigningCredentials( | ||
new SymmetricSecurityKey(key), | ||
SecurityAlgorithms.HmacSha512Signature) | ||
}; | ||
|
||
SecurityToken token = tokenHandler.CreateToken(tokenDescriptor); | ||
return tokenHandler.WriteToken(token); | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/NetCore2Blockly/TestBlocklyHtml/Controllers/RegistrationController.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,35 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Configuration; | ||
using TestBlocklyHtml.Authorization; | ||
|
||
namespace TestBlocklyHtml.Controllers | ||
{ | ||
[Route("api/[controller]")] | ||
public class RegistrationController : ControllerBase | ||
{ | ||
private const string ExpectedSecretCode = "blockly"; | ||
private readonly IConfiguration configuration; | ||
|
||
public RegistrationController(IConfiguration configuration) | ||
{ | ||
this.configuration = configuration; | ||
} | ||
|
||
[HttpPost] | ||
public IActionResult Register([FromBody] string secretCode) | ||
{ | ||
if (IsRegistrationSuccessful(secretCode)) | ||
{ | ||
var authorizationToken = new AuthorizationToken(configuration); | ||
return Ok(authorizationToken.GenerateFrom(secretCode)); | ||
} | ||
|
||
return BadRequest("invalid registration"); | ||
} | ||
|
||
private bool IsRegistrationSuccessful(string secretCode) | ||
{ | ||
return secretCode == ExpectedSecretCode; | ||
} | ||
} | ||
} |
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