summaryrefslogtreecommitdiff
path: root/slips/src/app/Marina/Task/AuthenticationTask.cs
blob: 75424a92c79edc714bdd98cfe2812a37716b1436 (plain)
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
using Marina.Domain.Interfaces;
using Marina.Domain.Repositories;
using Marina.Presentation.DTO;
using Marina.Web.Http;

namespace Marina.Task {
	public class AuthenticationTask : IAuthenticationTask {
		public AuthenticationTask( ICustomerRepository customers, IHttpGateway gateway ) {
			_customers = customers;
			_gateway = gateway;
		}

		public DisplayResponseLineDTO AuthenticateUserUsing( LoginCredentialsDTO credentials ) {
			if ( CheckIfPasswordMatches( credentials, _customers.FindBy( credentials.Username ) ) ) {
				_gateway.AddAuthenticationCookieFor( credentials.Username, _customers.FindBy( credentials.Username ).ID( ) );
				return ValidCredentialsMessage( credentials.Username );
			}
			else {
				return InvalidCredentialsMessage( );
			}
		}

		private static DisplayResponseLineDTO ValidCredentialsMessage( string username ) {
			return new DisplayResponseLineDTO( string.Format( "Currently logged in as {0}", username ) );
		}

		private static DisplayResponseLineDTO InvalidCredentialsMessage() {
			return new DisplayResponseLineDTO( "Invalid credentials specified" );
		}

		private static bool CheckIfPasswordMatches( LoginCredentialsDTO credentials, ICustomer customer ) {
			if ( customer != null && customer.Registration( ) != null ) {
				return customer.Registration( ).Password( ).Equals( credentials.Password );
			}
			return false;
		}

		private readonly ICustomerRepository _customers;
		private readonly IHttpGateway _gateway;
	}
}