This repository has been archived by the owner on Nov 3, 2022. It is now read-only.
forked from openrasta/archived-openrasta-legacy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
BasicAuthenticationScheme.cs
55 lines (42 loc) · 1.63 KB
/
BasicAuthenticationScheme.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using OpenRasta.Web;
namespace OpenRasta.Authentication.Basic
{
public class BasicAuthenticationScheme : IAuthenticationScheme
{
const string SCHEME = "Basic";
private readonly IBasicAuthenticator _basicAuthenticator;
public string Name { get { return SCHEME; } }
public BasicAuthenticationScheme(IBasicAuthenticator basicAuthenticator)
{
_basicAuthenticator = basicAuthenticator;
}
public AuthenticationResult Authenticate(IRequest request)
{
BasicAuthRequestHeader credentials = ExtractBasicHeader(request.Headers["Authorization"]);
if (credentials != null)
{
return _basicAuthenticator.Authenticate(credentials);
}
return new AuthenticationResult.MalformedCredentials();
}
public void Challenge(IResponse response)
{
response.Headers["WWW-Authenticate"] = string.Format("{0} realm=\"{1}\"", SCHEME, _basicAuthenticator.Realm);
}
internal static BasicAuthRequestHeader ExtractBasicHeader(string value)
{
try
{
var basicBase64Credentials = value.Split(' ')[1];
var basicCredentials = basicBase64Credentials.FromBase64String().Split(':');
if (basicCredentials.Length != 2)
return null;
return new BasicAuthRequestHeader(basicCredentials[0], basicCredentials[1]);
}
catch
{
return null;
}
}
}
}