-
Notifications
You must be signed in to change notification settings - Fork 7
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 #4 from eclipserporg/feature/add_authorization
Add basic authorization
- Loading branch information
Showing
11 changed files
with
167 additions
and
38 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using app.Models; | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.Extensions.Options; | ||
using System.Security.Claims; | ||
using System.Text; | ||
using System.Text.Encodings.Web; | ||
|
||
namespace app.Middlewares | ||
{ | ||
public class BasicAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions> | ||
{ | ||
private readonly IConfiguration _configuration; | ||
private readonly Credentials _credentials; | ||
|
||
public BasicAuthenticationHandler( | ||
IOptionsMonitor<AuthenticationSchemeOptions> options, | ||
ILoggerFactory logger, | ||
UrlEncoder encoder, | ||
ISystemClock clock, | ||
IOptions<Credentials> credentials, | ||
IConfiguration configuration) : base(options, logger, encoder, clock) | ||
{ | ||
_configuration = configuration; | ||
_credentials = credentials.Value; | ||
} | ||
|
||
protected override async Task<AuthenticateResult> HandleAuthenticateAsync() | ||
{ | ||
string authHeader = Request.Headers["Authorization"]; | ||
if (authHeader != null && authHeader.StartsWith("Basic")) | ||
{ | ||
|
||
var authHeaderValue = authHeader.Replace("Basic ", ""); | ||
var decodedAuthHeaderValue = Encoding.UTF8.GetString(Convert.FromBase64String(authHeaderValue)); | ||
var userPassArray = decodedAuthHeaderValue.Split(":"); | ||
var extractedUsername = userPassArray[0]; | ||
var extractedPassword = userPassArray[1]; | ||
|
||
if (string.Equals(_credentials.Username, extractedUsername) && string.Equals(extractedPassword, _credentials.Password)) | ||
{ | ||
var claims = new[] { new Claim(ClaimTypes.Name, _credentials.Username) }; | ||
var identity = new ClaimsIdentity(claims, Scheme.Name); | ||
var principal = new ClaimsPrincipal(identity); | ||
var ticket = new AuthenticationTicket(principal, Scheme.Name); | ||
|
||
return AuthenticateResult.Success(ticket); | ||
} | ||
} | ||
return AuthenticateResult.Fail("Failed to authenticate"); | ||
} | ||
} | ||
|
||
} |
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,8 @@ | ||
namespace app.Models | ||
{ | ||
public class Credentials | ||
{ | ||
public string Username { get; set; } | ||
public string Password { get; set; } | ||
} | ||
} |
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,18 @@ | ||
namespace app.Models | ||
{ | ||
public class ResponsePairDto | ||
{ | ||
public ResponsePairDto() | ||
{ | ||
} | ||
|
||
public ResponsePairDto(bool status, string message) | ||
{ | ||
Status = status; | ||
Message = message; | ||
} | ||
|
||
public bool Status { get; set; } | ||
public string Message { get; set; } | ||
} | ||
} |
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