summaryrefslogtreecommitdiff
path: root/slips/src/app/Marina/Web/Http/HttpGateway.cs
blob: 4b9131502e4e033b5327070ce589d165c2f16cef (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.Collections.Specialized;
using System.Security.Principal;
using System.Web;
using System.Web.Security;
using Marina.Infrastructure.Logging.Interfaces;
using Marina.Presentation;
using Marina.Web.Views;

namespace Marina.Web.Http {
	public class HttpGateway : IHttpGateway {
		public HttpGateway( IHttpContext context ) {
			_context = context;
		}

		public string Destination() {
			return _context.Request.RawUrl;
		}

		public void RedirectTo( IView view ) {
			try {
				_context.Server.Transfer( view.Name( ) );
			}
			catch ( Exception ex ) {
				Log.For( this ).CriticalError( ex.StackTrace );
			}
		}

		public void AddAuthenticationCookieFor( string username, long customerId ) {
			AddCookieToResponse( username, customerId );
			BindPrincipalToCurrentThread( username );
		}

		public T ParsePayloadFor< T >( PayloadKey< T > key ) {
			return key.ParseFrom( Payload( ) );
		}

		public bool ContainsPayload< T >( PayloadKey< T > key ) {
			try {
				ParsePayloadFor( key );
				return true;
			}
			catch ( PayloadKeyNotFoundException ) {
				return false;
			}
		}

		private NameValueCollection Payload() {
			return new NameValueCollection( _context.Request.Params );
		}

		private void BindPrincipalToCurrentThread( string username ) {
			_context.User = new GenericPrincipal( new GenericIdentity( username ), new string[] {"Customer"} );
		}

		private void AddCookieToResponse( string username, long customerId ) {
			FormsAuthenticationTicket ticket =
				new FormsAuthenticationTicket( 1, username, DateTime.Now, DateTime.Now.AddMinutes( 20 ), false,
				                               customerId.ToString( ) );

			_context.Response.Cookies.Add( CreateCookieFrom( ticket ) );
			_context.Response.Cookies.Add( new HttpCookie( PayloadKeys.CustomerId, customerId.ToString( ) ) );
		}

		private HttpCookie CreateCookieFrom( FormsAuthenticationTicket ticket ) {
			return new HttpCookie( FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt( ticket ) );
		}

		private IHttpContext _context;
	}
}