|
| 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 | + |
0 commit comments