WeddingThankYou 1.0.0.0
결혼식 이후 감사 인사와 사진, 계좌 안내, 연락처를 모바일 페이지로 전달하는 감사장 서비스입니다.
로딩중...
검색중...
일치하는것 없음
SuperAdminSessionTokenService.cs
이 파일의 문서화 페이지로 가기
1using System.Security.Cryptography;
2using System.Text;
3
5
15{
32 string CreateToken();
33
58 bool ValidateToken(string? token);
59}
60
70{
79 private static readonly TimeSpan TokenLifetime = TimeSpan.FromHours(8);
88 private readonly WeddingOptions _options;
89
107 {
108 _options = options;
109 }
110
127 public string CreateToken()
128 {
129 var expires = DateTimeOffset.UtcNow.Add(TokenLifetime).ToUnixTimeSeconds();
130 var payload = $"v1:{expires}:{Guid.NewGuid():N}";
131 var signature = Sign(payload);
132 return $"{Encode(payload)}.{Encode(signature)}";
133 }
134
159 public bool ValidateToken(string? token)
160 {
161 if (string.IsNullOrWhiteSpace(token))
162 {
163 return false;
164 }
165
166 var parts = token.Split('.', 2);
167 if (parts.Length != 2)
168 {
169 return false;
170 }
171
172 var payload = DecodeString(parts[0]);
173 var signature = DecodeBytes(parts[1]);
174 if (string.IsNullOrWhiteSpace(payload) || signature.Length == 0)
175 {
176 return false;
177 }
178
179 var fields = payload.Split(':', 3);
180 if (fields.Length != 3 || fields[0] != "v1" || !long.TryParse(fields[1], out var expires))
181 {
182 return false;
183 }
184
185 if (DateTimeOffset.UtcNow.ToUnixTimeSeconds() > expires)
186 {
187 return false;
188 }
189
190 var expected = Sign(payload);
191 return CryptographicOperations.FixedTimeEquals(expected, signature);
192 }
193
218 private byte[] Sign(string payload)
219 {
220 var secret = string.IsNullOrWhiteSpace(_options.SuperAdminPassword)
221 ? "WeddingThankYou.SuperAdminSession.EmptySecret"
222 : _options.SuperAdminPassword;
223 using var hmac = new HMACSHA256(Encoding.UTF8.GetBytes("WeddingThankYou.SuperAdminSession:" + secret));
224 return hmac.ComputeHash(Encoding.UTF8.GetBytes(payload));
225 }
226
251 private static string Encode(string value) => Encode(Encoding.UTF8.GetBytes(value));
252
277 private static string Encode(byte[] value) =>
278 Convert.ToBase64String(value)
279 .TrimEnd('=')
280 .Replace('+', '-')
281 .Replace('/', '_');
282
307 private static string DecodeString(string value)
308 {
309 var bytes = DecodeBytes(value);
310 return bytes.Length == 0 ? "" : Encoding.UTF8.GetString(bytes);
311 }
312
337 private static byte[] DecodeBytes(string value)
338 {
339 try
340 {
341 var normalized = value.Replace('-', '+').Replace('_', '/');
342 normalized = normalized.PadRight(normalized.Length + (4 - normalized.Length % 4) % 4, '=');
343 return Convert.FromBase64String(normalized);
344 }
345 catch
346 {
347 return [];
348 }
349 }
350}