-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenGenerator.cs
More file actions
125 lines (110 loc) · 4.08 KB
/
TokenGenerator.cs
File metadata and controls
125 lines (110 loc) · 4.08 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* FCS API - Token Generator (C#)
*
* Generate secure tokens for frontend JavaScript authentication
*
* Usage:
* 1. Set your accessKey and publicKey
* 2. Call GenerateToken()
* 3. Pass the token data to your frontend JavaScript
*
* @package FcsApi
* @author FCS API <support@fcsapi.com>
* @link https://fcsapi.com
*/
using System;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
namespace FcsApi
{
public class TokenGenerator
{
// Token expiry options (in seconds):
// 300 (5min), 900 (15min), 1800 (30min), 3600 (1hr), 86400 (24hr)
private readonly string _accessKey;
private readonly string _publicKey;
private readonly int _tokenExpiry;
/// <summary>
/// Initialize token generator
/// </summary>
/// <param name="accessKey">Your API access key (get from https://fcsapi.com/dashboard)</param>
/// <param name="publicKey">Your public key (get from https://fcsapi.com/dashboard)</param>
/// <param name="tokenExpiry">Token expiry in seconds (default: 3600 = 1 hour)</param>
public TokenGenerator(string accessKey = "YOUR_ACCESS_KEY_HERE", string publicKey = "YOUR_PUBLIC_KEY_HERE", int tokenExpiry = 3600)
{
_accessKey = accessKey;
_publicKey = publicKey;
_tokenExpiry = tokenExpiry;
}
/// <summary>
/// Generate token for frontend authentication
/// </summary>
/// <returns>Dictionary with _token, _expiry, _public_key</returns>
public Dictionary<string, object> GenerateToken()
{
var expiry = DateTimeOffset.UtcNow.ToUnixTimeSeconds() + _tokenExpiry;
var message = _publicKey + expiry.ToString();
var token = ComputeHmacSha256(message, _accessKey);
return new Dictionary<string, object>
{
{ "_token", token },
{ "_expiry", expiry },
{ "_public_key", _publicKey }
};
}
/// <summary>
/// Generate token and return as JSON string
/// </summary>
/// <returns>JSON string of token data</returns>
public string ToJson()
{
return JsonSerializer.Serialize(GenerateToken());
}
/// <summary>
/// Generate HTML meta tags for token
/// </summary>
/// <returns>HTML meta tags string</returns>
public string GetMetaTags()
{
var token = GenerateToken();
return $"<meta name=\"fcs-public-key\" content=\"{token["_public_key"]}\">\n" +
$"<meta name=\"fcs-token\" content=\"{token["_token"]}\">\n" +
$"<meta name=\"fcs-token-expiry\" content=\"{token["_expiry"]}\">";
}
private static string ComputeHmacSha256(string message, string key)
{
var keyBytes = Encoding.UTF8.GetBytes(key);
var messageBytes = Encoding.UTF8.GetBytes(message);
using var hmac = new HMACSHA256(keyBytes);
var hashBytes = hmac.ComputeHash(messageBytes);
return BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
}
}
}
// ============================================
// USAGE EXAMPLES
// ============================================
// Example 1: Generate token
// var generator = new TokenGenerator("your_access_key", "your_public_key");
// var tokenData = generator.GenerateToken();
// Console.WriteLine($"Token: {tokenData["_token"]}");
// Example 2: Get as JSON
// Console.WriteLine(generator.ToJson());
// Example 3: Get meta tags
// Console.WriteLine(generator.GetMetaTags());
// ASP.NET Core example:
// [ApiController]
// [Route("api/[controller]")]
// public class FcsTokenController : ControllerBase
// {
// [HttpGet]
// public IActionResult GetToken()
// {
// var generator = new TokenGenerator(
// Environment.GetEnvironmentVariable("FCS_ACCESS_KEY"),
// Environment.GetEnvironmentVariable("FCS_PUBLIC_KEY")
// );
// return Ok(generator.GenerateToken());
// }
// }