Skip to content

Commit daa51d5

Browse files
committed
Initial commit: FCS API Token Generator - 10 languages
0 parents  commit daa51d5

11 files changed

Lines changed: 1311 additions & 0 deletions

README.md

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
# FCS API - Token Generator
2+
3+
Generate secure tokens for frontend JavaScript authentication in **10 programming languages**.
4+
5+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6+
7+
## Available Languages
8+
9+
| Language | File |
10+
|----------|------|
11+
| PHP | `token-generator.php` |
12+
| Python | `token-generator.py` |
13+
| JavaScript | `token-generator.js` |
14+
| Java | `TokenGenerator.java` |
15+
| Ruby | `token_generator.rb` |
16+
| C# | `TokenGenerator.cs` |
17+
| Go | `token_generator.go` |
18+
| Rust | `token_generator.rs` |
19+
| Swift | `TokenGenerator.swift` |
20+
| Kotlin | `TokenGenerator.kt` |
21+
22+
## Installation
23+
24+
### Clone Full Repository
25+
```bash
26+
git clone https://github.com/fcsapi/token-generator
27+
```
28+
29+
### Download Single File
30+
```bash
31+
# PHP only
32+
curl -O https://raw.githubusercontent.com/fcsapi/token-generator/main/token-generator.php
33+
34+
# Python only
35+
curl -O https://raw.githubusercontent.com/fcsapi/token-generator/main/token-generator.py
36+
37+
# JavaScript only
38+
curl -O https://raw.githubusercontent.com/fcsapi/token-generator/main/token-generator.js
39+
40+
# Java only
41+
curl -O https://raw.githubusercontent.com/fcsapi/token-generator/main/TokenGenerator.java
42+
43+
# Ruby only
44+
curl -O https://raw.githubusercontent.com/fcsapi/token-generator/main/token_generator.rb
45+
46+
# C# only
47+
curl -O https://raw.githubusercontent.com/fcsapi/token-generator/main/TokenGenerator.cs
48+
49+
# Go only
50+
curl -O https://raw.githubusercontent.com/fcsapi/token-generator/main/token_generator.go
51+
52+
# Rust only
53+
curl -O https://raw.githubusercontent.com/fcsapi/token-generator/main/token_generator.rs
54+
55+
# Swift only
56+
curl -O https://raw.githubusercontent.com/fcsapi/token-generator/main/TokenGenerator.swift
57+
58+
# Kotlin only
59+
curl -O https://raw.githubusercontent.com/fcsapi/token-generator/main/TokenGenerator.kt
60+
```
61+
62+
### Or Download from GitHub
63+
Go to [GitHub](https://github.com/fcsapi/token-generator), click on the file you need, then click "Download raw file".
64+
65+
## Usage
66+
67+
### PHP
68+
```php
69+
$generator = new FcsTokenGenerator('your_access_key', 'your_public_key');
70+
$tokenData = $generator->generateToken();
71+
// Returns: ['_token' => '...', '_expiry' => 1234567890, '_public_key' => '...']
72+
```
73+
74+
### Python
75+
```python
76+
generator = FcsTokenGenerator('your_access_key', 'your_public_key')
77+
token_data = generator.generate_token()
78+
# Returns: {'_token': '...', '_expiry': 1234567890, '_public_key': '...'}
79+
```
80+
81+
### JavaScript (Node.js)
82+
```javascript
83+
const generator = new FcsTokenGenerator('your_access_key', 'your_public_key');
84+
const tokenData = generator.generateToken();
85+
// Returns: { _token: '...', _expiry: 1234567890, _public_key: '...' }
86+
```
87+
88+
### Java
89+
```java
90+
TokenGenerator generator = new TokenGenerator("your_access_key", "your_public_key");
91+
Map<String, Object> tokenData = generator.generateToken();
92+
```
93+
94+
### Ruby
95+
```ruby
96+
generator = FcsTokenGenerator.new('your_access_key', 'your_public_key')
97+
token_data = generator.generate_token
98+
```
99+
100+
### C#
101+
```csharp
102+
var generator = new TokenGenerator("your_access_key", "your_public_key");
103+
var tokenData = generator.GenerateToken();
104+
```
105+
106+
### Go
107+
```go
108+
generator := NewTokenGenerator("your_access_key", "your_public_key", 3600)
109+
tokenData := generator.GenerateToken()
110+
```
111+
112+
### Rust
113+
```rust
114+
let generator = TokenGenerator::new("your_access_key", "your_public_key", None);
115+
let token_data = generator.generate_token();
116+
```
117+
118+
### Swift
119+
```swift
120+
let generator = FcsTokenGenerator(accessKey: "your_access_key", publicKey: "your_public_key")
121+
let tokenData = generator.generateToken()
122+
```
123+
124+
### Kotlin
125+
```kotlin
126+
val generator = FcsTokenGenerator("your_access_key", "your_public_key")
127+
val tokenData = generator.generateToken()
128+
```
129+
130+
## Token Expiry Options
131+
132+
| Seconds | Duration |
133+
|---------|----------|
134+
| 300 | 5 minutes |
135+
| 900 | 15 minutes |
136+
| 1800 | 30 minutes |
137+
| 3600 | 1 hour (default) |
138+
| 86400 | 24 hours |
139+
140+
## How It Works
141+
142+
1. **Backend** generates a secure token using HMAC-SHA256
143+
2. **Backend** sends token data to frontend (via API or meta tags)
144+
3. **Frontend** uses token for FCS API calls (no API key exposed)
145+
146+
```
147+
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
148+
│ Backend │ │ Frontend │ │ FCS API │
149+
│ (Your Server) │ │ (Browser) │ │ │
150+
└────────┬────────┘ └────────┬────────┘ └────────┬────────┘
151+
│ │ │
152+
│ 1. Generate Token │ │
153+
│ (using access_key) │ │
154+
│ │ │
155+
│ 2. Send token data │ │
156+
│ ─────────────────────────>│ │
157+
│ │ │
158+
│ │ 3. API Request │
159+
│ │ (with token) │
160+
│ │ ─────────────────────────>│
161+
│ │ │
162+
│ │ 4. Response │
163+
│ │ <─────────────────────────│
164+
│ │ │
165+
```
166+
167+
## Get API Keys
168+
169+
1. Visit [FCS API](https://fcsapi.com)
170+
2. Sign up for a free account
171+
3. Get your **Access Key** and **Public Key** from the [dashboard](https://fcsapi.com/dashboard)
172+
173+
## Documentation
174+
175+
- [Forex API Documentation](https://fcsapi.com/document/forex-api)
176+
- [Crypto API Documentation](https://fcsapi.com/document/crypto-api)
177+
- [Stock API Documentation](https://fcsapi.com/document/stock-api)
178+
179+
## Support
180+
181+
- Email: support@fcsapi.com
182+
- Website: [fcsapi.com](https://fcsapi.com)
183+
184+
## License
185+
186+
MIT License - see [LICENSE](LICENSE) file for details.

TokenGenerator.cs

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/**
2+
* FCS API - Token Generator (C#)
3+
*
4+
* Generate secure tokens for frontend JavaScript authentication
5+
*
6+
* Usage:
7+
* 1. Set your accessKey and publicKey
8+
* 2. Call GenerateToken()
9+
* 3. Pass the token data to your frontend JavaScript
10+
*
11+
* @package FcsApi
12+
* @author FCS API <support@fcsapi.com>
13+
* @link https://fcsapi.com
14+
*/
15+
16+
using System;
17+
using System.Security.Cryptography;
18+
using System.Text;
19+
using System.Text.Json;
20+
21+
namespace FcsApi
22+
{
23+
public class TokenGenerator
24+
{
25+
// Token expiry options (in seconds):
26+
// 300 (5min), 900 (15min), 1800 (30min), 3600 (1hr), 86400 (24hr)
27+
28+
private readonly string _accessKey;
29+
private readonly string _publicKey;
30+
private readonly int _tokenExpiry;
31+
32+
/// <summary>
33+
/// Initialize token generator
34+
/// </summary>
35+
/// <param name="accessKey">Your API access key (get from https://fcsapi.com/dashboard)</param>
36+
/// <param name="publicKey">Your public key (get from https://fcsapi.com/dashboard)</param>
37+
/// <param name="tokenExpiry">Token expiry in seconds (default: 3600 = 1 hour)</param>
38+
public TokenGenerator(string accessKey = "YOUR_ACCESS_KEY_HERE", string publicKey = "YOUR_PUBLIC_KEY_HERE", int tokenExpiry = 3600)
39+
{
40+
_accessKey = accessKey;
41+
_publicKey = publicKey;
42+
_tokenExpiry = tokenExpiry;
43+
}
44+
45+
/// <summary>
46+
/// Generate token for frontend authentication
47+
/// </summary>
48+
/// <returns>Dictionary with _token, _expiry, _public_key</returns>
49+
public Dictionary<string, object> GenerateToken()
50+
{
51+
var expiry = DateTimeOffset.UtcNow.ToUnixTimeSeconds() + _tokenExpiry;
52+
var message = _publicKey + expiry.ToString();
53+
var token = ComputeHmacSha256(message, _accessKey);
54+
55+
return new Dictionary<string, object>
56+
{
57+
{ "_token", token },
58+
{ "_expiry", expiry },
59+
{ "_public_key", _publicKey }
60+
};
61+
}
62+
63+
/// <summary>
64+
/// Generate token and return as JSON string
65+
/// </summary>
66+
/// <returns>JSON string of token data</returns>
67+
public string ToJson()
68+
{
69+
return JsonSerializer.Serialize(GenerateToken());
70+
}
71+
72+
/// <summary>
73+
/// Generate HTML meta tags for token
74+
/// </summary>
75+
/// <returns>HTML meta tags string</returns>
76+
public string GetMetaTags()
77+
{
78+
var token = GenerateToken();
79+
return $"<meta name=\"fcs-public-key\" content=\"{token["_public_key"]}\">\n" +
80+
$"<meta name=\"fcs-token\" content=\"{token["_token"]}\">\n" +
81+
$"<meta name=\"fcs-token-expiry\" content=\"{token["_expiry"]}\">";
82+
}
83+
84+
private static string ComputeHmacSha256(string message, string key)
85+
{
86+
var keyBytes = Encoding.UTF8.GetBytes(key);
87+
var messageBytes = Encoding.UTF8.GetBytes(message);
88+
89+
using var hmac = new HMACSHA256(keyBytes);
90+
var hashBytes = hmac.ComputeHash(messageBytes);
91+
return BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
92+
}
93+
}
94+
}
95+
96+
// ============================================
97+
// USAGE EXAMPLES
98+
// ============================================
99+
100+
// Example 1: Generate token
101+
// var generator = new TokenGenerator("your_access_key", "your_public_key");
102+
// var tokenData = generator.GenerateToken();
103+
// Console.WriteLine($"Token: {tokenData["_token"]}");
104+
105+
// Example 2: Get as JSON
106+
// Console.WriteLine(generator.ToJson());
107+
108+
// Example 3: Get meta tags
109+
// Console.WriteLine(generator.GetMetaTags());
110+
111+
// ASP.NET Core example:
112+
// [ApiController]
113+
// [Route("api/[controller]")]
114+
// public class FcsTokenController : ControllerBase
115+
// {
116+
// [HttpGet]
117+
// public IActionResult GetToken()
118+
// {
119+
// var generator = new TokenGenerator(
120+
// Environment.GetEnvironmentVariable("FCS_ACCESS_KEY"),
121+
// Environment.GetEnvironmentVariable("FCS_PUBLIC_KEY")
122+
// );
123+
// return Ok(generator.GenerateToken());
124+
// }
125+
// }

0 commit comments

Comments
 (0)