Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 173 lines (157 sloc) 3.214 kb
aaffb24 @mehrdada Initial commit
mehrdada authored
1 using System;
2 using System.IO;
12e0724 @mehrdada Added version outside cookie and roles.
mehrdada authored
3 using System.Security.Principal;
aaffb24 @mehrdada Initial commit
mehrdada authored
4
5 namespace AppHarbor.Web.Security
6 {
7 public class AuthenticationCookie
8 {
12e0724 @mehrdada Added version outside cookie and roles.
mehrdada authored
9 private readonly short _cookieType;
aaffb24 @mehrdada Initial commit
mehrdada authored
10 private readonly Guid _id;
11 private readonly bool _persistent;
12 private DateTime _issueDate;
13 private readonly string _name;
14 private readonly byte[] _tag;
12e0724 @mehrdada Added version outside cookie and roles.
mehrdada authored
15 private readonly string[] _roles;
aaffb24 @mehrdada Initial commit
mehrdada authored
16
17 private AuthenticationCookie(byte[] data)
18 {
19 using (var memoryStream = new MemoryStream(data))
20 {
21 using (var binaryReader = new BinaryReader(memoryStream))
22 {
12e0724 @mehrdada Added version outside cookie and roles.
mehrdada authored
23 _cookieType = binaryReader.ReadInt16();
aaffb24 @mehrdada Initial commit
mehrdada authored
24 _id = new Guid(binaryReader.ReadBytes(16));
25 _persistent = binaryReader.ReadBoolean();
26 _issueDate = DateTime.FromBinary(binaryReader.ReadInt64());
27 _name = binaryReader.ReadString();
12e0724 @mehrdada Added version outside cookie and roles.
mehrdada authored
28 var rolesLength = binaryReader.ReadInt16();
29 _roles = new string[rolesLength];
30 for (int i = 0; i < _roles.Length; i++)
31 {
32 _roles[i] = binaryReader.ReadString();
33 }
aaffb24 @mehrdada Initial commit
mehrdada authored
34 var tagLength = binaryReader.ReadInt16();
35 if (tagLength == 0)
36 {
37 _tag = null;
38 }
39 else
40 {
41 _tag = binaryReader.ReadBytes(tagLength);
42 }
43 }
44 }
45 }
46
12e0724 @mehrdada Added version outside cookie and roles.
mehrdada authored
47 public AuthenticationCookie(short cookieType, Guid id, bool persistent, string name, string[] roles = null, byte[] tag = null)
aaffb24 @mehrdada Initial commit
mehrdada authored
48 {
12e0724 @mehrdada Added version outside cookie and roles.
mehrdada authored
49 _cookieType = cookieType;
aaffb24 @mehrdada Initial commit
mehrdada authored
50 _id = id;
51 _persistent = persistent;
52 _name = name;
12e0724 @mehrdada Added version outside cookie and roles.
mehrdada authored
53 _roles = roles ?? new string[0];
aaffb24 @mehrdada Initial commit
mehrdada authored
54 _tag = tag;
55 _issueDate = DateTime.UtcNow;
56 }
57
12e0724 @mehrdada Added version outside cookie and roles.
mehrdada authored
58 public IPrincipal GetPrincipal()
59 {
44caf25 @mehrdada Refactored CookieIdentity to take a reference to
mehrdada authored
60 var identity = new CookieIdentity(this);
12e0724 @mehrdada Added version outside cookie and roles.
mehrdada authored
61 return new GenericPrincipal(identity, _roles);
62 }
63
aaffb24 @mehrdada Initial commit
mehrdada authored
64 public byte[] Serialize()
65 {
66 using (var memoryStream = new MemoryStream())
67 {
68 using (var binaryWriter = new BinaryWriter(memoryStream))
69 {
12e0724 @mehrdada Added version outside cookie and roles.
mehrdada authored
70 binaryWriter.Write(_cookieType);
aaffb24 @mehrdada Initial commit
mehrdada authored
71 binaryWriter.Write(_id.ToByteArray());
72 binaryWriter.Write(_persistent);
73 binaryWriter.Write(_issueDate.ToBinary());
74 binaryWriter.Write(_name);
12e0724 @mehrdada Added version outside cookie and roles.
mehrdada authored
75 if (_roles == null)
76 {
77 binaryWriter.Write((short)0);
78 }
79 else
80 {
81 binaryWriter.Write((short)_roles.Length);
82 foreach (var role in _roles)
83 {
84 binaryWriter.Write(role);
85 }
86 }
aaffb24 @mehrdada Initial commit
mehrdada authored
87 if (_tag == null)
88 {
89 binaryWriter.Write((short)0);
90 }
91 else
92 {
93 binaryWriter.Write((short)_tag.Length);
94 binaryWriter.Write(_tag);
95 }
96 }
97 return memoryStream.ToArray();
98 }
99 }
100
101 public static AuthenticationCookie Deserialize(byte[] data)
102 {
103 return new AuthenticationCookie(data);
104 }
105
106 public void Renew()
107 {
108 _issueDate = DateTime.UtcNow;
109 }
110
111 public bool IsExpired(TimeSpan validity)
112 {
e77596e @mehrdada Check the expiration date of persistent cookies too
mehrdada authored
113 return _issueDate.Add(validity) <= DateTime.UtcNow;
aaffb24 @mehrdada Initial commit
mehrdada authored
114 }
115
116 public DateTime IssueDate
117 {
118 get
119 {
120 return _issueDate;
121 }
122 }
123
124 public bool Persistent
125 {
126 get
127 {
128 return _persistent;
129 }
130 }
131
132 public string Name
133 {
134 get
135 {
136 return _name;
137 }
138 }
139
12e0724 @mehrdada Added version outside cookie and roles.
mehrdada authored
140 public short CookieType
141 {
142 get
143 {
144 return _cookieType;
145 }
146 }
147
aaffb24 @mehrdada Initial commit
mehrdada authored
148 public Guid Id
149 {
150 get
151 {
152 return _id;
153 }
154 }
155
12e0724 @mehrdada Added version outside cookie and roles.
mehrdada authored
156 public string[] Roles
157 {
158 get
159 {
160 return _roles;
161 }
162 }
163
aaffb24 @mehrdada Initial commit
mehrdada authored
164 public byte[] Tag
165 {
166 get
167 {
168 return _tag;
169 }
170 }
171 }
172 }
Something went wrong with that request. Please try again.