Skip to content

Commit 437e882

Browse files
committed
Merge branch 'feature/87-security-logic'
2 parents e94246a + 6a1a675 commit 437e882

19 files changed

Lines changed: 1046 additions & 711 deletions
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//
2+
// AuthenticationResult.cs
3+
//
4+
// Author:
5+
// Craig Fowler <craig@craigfowler.me.uk>
6+
//
7+
// Copyright (c) 2016 Craig Fowler
8+
//
9+
// Permission is hereby granted, free of charge, to any person obtaining a copy
10+
// of this software and associated documentation files (the "Software"), to deal
11+
// in the Software without restriction, including without limitation the rights
12+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
// copies of the Software, and to permit persons to whom the Software is
14+
// furnished to do so, subject to the following conditions:
15+
//
16+
// The above copyright notice and this permission notice shall be included in
17+
// all copies or substantial portions of the Software.
18+
//
19+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
// THE SOFTWARE.
26+
using System;
27+
28+
namespace CSF.Security
29+
{
30+
/// <summary>
31+
/// Immutable type represents the result of an authentication attempt.
32+
/// </summary>
33+
public class AuthenticationResult
34+
{
35+
/// <summary>
36+
/// Gets a value indicating whether the credentials were found (usually meaning that a matching user was found in
37+
/// the db).
38+
/// </summary>
39+
/// <value><c>true</c> if the credentials were found; otherwise, <c>false</c>.</value>
40+
public bool CredentialsFound { get; private set; }
41+
42+
/// <summary>
43+
/// Gets a value indicating whether the credentials were verified.
44+
/// </summary>
45+
/// <value><c>true</c> if the credentials were verified; otherwise, <c>false</c>.</value>
46+
public bool CredentialsVerified { get; private set; }
47+
48+
/// <summary>
49+
/// Initializes a new instance of the <see cref="CSF.Security.AuthenticationResult"/> class.
50+
/// </summary>
51+
/// <param name="found">Whether or not the credentials were found.</param>
52+
/// <param name="verified">Whether or not the credentials were verified.</param>
53+
public AuthenticationResult(bool found, bool verified)
54+
{
55+
CredentialsFound = found;
56+
CredentialsVerified = verified;
57+
}
58+
}
59+
}
60+
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
//
2+
// AuthenticationService.cs
3+
//
4+
// Author:
5+
// Craig Fowler <craig@craigfowler.me.uk>
6+
//
7+
// Copyright (c) 2016 Craig Fowler
8+
//
9+
// Permission is hereby granted, free of charge, to any person obtaining a copy
10+
// of this software and associated documentation files (the "Software"), to deal
11+
// in the Software without restriction, including without limitation the rights
12+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
// copies of the Software, and to permit persons to whom the Software is
14+
// furnished to do so, subject to the following conditions:
15+
//
16+
// The above copyright notice and this permission notice shall be included in
17+
// all copies or substantial portions of the Software.
18+
//
19+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
// THE SOFTWARE.
26+
using System;
27+
28+
namespace CSF.Security
29+
{
30+
/// <summary>
31+
/// Abstract base type for an authentication service.
32+
/// </summary>
33+
public abstract class AuthenticationService<TEnteredCredentials,TStoredCredentials>
34+
: IAuthenticationService<TEnteredCredentials>, IAuthenticationService
35+
{
36+
#region fields
37+
38+
protected ICredentialsRepository<TEnteredCredentials,TStoredCredentials> CredentialsRepository { get; private set; }
39+
protected ICredentialVerifier<TEnteredCredentials,TStoredCredentials> CredentialsVerifier { get; private set; }
40+
41+
#endregion
42+
43+
#region methods
44+
45+
/// <summary>
46+
/// Attempts authentication using the given credentials.
47+
/// </summary>
48+
/// <param name="enteredCredentials">Entered credentials.</param>
49+
public virtual AuthenticationResult Authenticate(TEnteredCredentials enteredCredentials)
50+
{
51+
if(enteredCredentials == null)
52+
{
53+
throw new ArgumentNullException(nameof(enteredCredentials));
54+
}
55+
56+
var storedCredentials = CredentialsRepository.GetStoredCredentials(enteredCredentials);
57+
if(storedCredentials == null)
58+
{
59+
return new AuthenticationResult(false, false);
60+
}
61+
62+
var verified = CredentialsVerifier.Verify(enteredCredentials, storedCredentials);
63+
64+
return new AuthenticationResult(true, verified);
65+
}
66+
67+
#endregion
68+
69+
#region interface implementations
70+
71+
AuthenticationResult IAuthenticationService.Authenticate(object enteredCredentials)
72+
{
73+
return Authenticate((TEnteredCredentials) enteredCredentials);
74+
}
75+
76+
#endregion
77+
78+
#region constructor
79+
80+
/// <summary>
81+
/// Initializes a new instance of the <see cref="T:CSF.Security.AuthenticationService`2"/> class.
82+
/// </summary>
83+
/// <param name="repository">Credentials repository.</param>
84+
/// <param name="verifier">Credentials verifier.</param>
85+
public AuthenticationService(ICredentialsRepository<TEnteredCredentials,TStoredCredentials> repository,
86+
ICredentialVerifier<TEnteredCredentials,TStoredCredentials> verifier)
87+
{
88+
if(repository == null)
89+
{
90+
throw new ArgumentNullException(nameof(repository));
91+
}
92+
if(verifier == null)
93+
{
94+
throw new ArgumentNullException(nameof(verifier));
95+
}
96+
97+
CredentialsRepository = repository;
98+
CredentialsVerifier = verifier;
99+
}
100+
101+
#endregion
102+
}
103+
}
104+

CSF.Security/Base64KeyAndSalt.cs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//
2+
// Base64PasswordAndSalt.cs
3+
//
4+
// Author:
5+
// Craig Fowler <craig@craigfowler.me.uk>
6+
//
7+
// Copyright (c) 2016 Craig Fowler
8+
//
9+
// Permission is hereby granted, free of charge, to any person obtaining a copy
10+
// of this software and associated documentation files (the "Software"), to deal
11+
// in the Software without restriction, including without limitation the rights
12+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
// copies of the Software, and to permit persons to whom the Software is
14+
// furnished to do so, subject to the following conditions:
15+
//
16+
// The above copyright notice and this permission notice shall be included in
17+
// all copies or substantial portions of the Software.
18+
//
19+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
// THE SOFTWARE.
26+
using System;
27+
28+
namespace CSF.Security
29+
{
30+
/// <summary>
31+
/// Base type for stored credentials which includes a key and salt stored as Base64-encoded strings.
32+
/// </summary>
33+
public abstract class Base64KeyAndSalt : IStoredCredentialsWithKeyAndSalt
34+
{
35+
#region properties
36+
37+
/// <summary>
38+
/// Gets or sets the key data.
39+
/// </summary>
40+
/// <value>The key data.</value>
41+
public string KeyData { get; set; }
42+
43+
/// <summary>
44+
/// Gets or sets the salt data.
45+
/// </summary>
46+
/// <value>The salt data.</value>
47+
public string SaltData { get; set; }
48+
49+
#endregion
50+
51+
#region methods
52+
53+
/// <summary>
54+
/// Gets the key as a byte array.
55+
/// </summary>
56+
/// <returns>The key as a byte array.</returns>
57+
public virtual byte[] GetKeyAsByteArray()
58+
{
59+
if(KeyData == null)
60+
{
61+
return null;
62+
}
63+
64+
return Convert.FromBase64String(KeyData);
65+
}
66+
67+
/// <summary>
68+
/// Gets the salt as a byte array.
69+
/// </summary>
70+
/// <returns>The salt as a byte array.</returns>
71+
public virtual byte[] GetSaltAsByteArray()
72+
{
73+
if(SaltData == null)
74+
{
75+
return null;
76+
}
77+
78+
return Convert.FromBase64String(SaltData);
79+
}
80+
81+
#endregion
82+
}
83+
}
84+

CSF.Security/BinaryHashAndSaltPair.cs

Lines changed: 0 additions & 143 deletions
This file was deleted.

0 commit comments

Comments
 (0)