-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenGenerator.kt
More file actions
123 lines (109 loc) · 3.52 KB
/
TokenGenerator.kt
File metadata and controls
123 lines (109 loc) · 3.52 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
/**
* FCS API - Token Generator (Kotlin)
*
* 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
*/
package com.fcsapi
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
import com.google.gson.Gson
import com.google.gson.annotations.SerializedName
/**
* Token data class
*/
data class TokenData(
@SerializedName("_token") val token: String,
@SerializedName("_expiry") val expiry: Long,
@SerializedName("_public_key") val publicKey: String
)
/**
* Token generator for FCS API
* Token expiry options (in seconds): 300 (5min), 900 (15min), 1800 (30min), 3600 (1hr), 86400 (24hr)
*
* @param accessKey Your API access key (get from https://fcsapi.com/dashboard)
* @param publicKey Your public key (get from https://fcsapi.com/dashboard)
* @param tokenExpiry Token expiry in seconds (default: 3600 = 1 hour)
*/
class FcsTokenGenerator(
private val accessKey: String = "YOUR_ACCESS_KEY_HERE",
private val publicKey: String = "YOUR_PUBLIC_KEY_HERE",
private val tokenExpiry: Long = 3600
) {
/**
* Generate token for frontend authentication
* @return TokenData with token, expiry, and public_key
*/
fun generateToken(): TokenData {
val expiry = System.currentTimeMillis() / 1000 + tokenExpiry
val message = "$publicKey$expiry"
val mac = Mac.getInstance("HmacSHA256")
val secretKey = SecretKeySpec(accessKey.toByteArray(), "HmacSHA256")
mac.init(secretKey)
val hash = mac.doFinal(message.toByteArray())
val token = hash.joinToString("") { "%02x".format(it) }
return TokenData(token, expiry, publicKey)
}
/**
* Generate token and return as JSON string
* @return JSON string of token data
*/
fun toJson(): String {
return Gson().toJson(generateToken())
}
/**
* Generate HTML meta tags for token
* @return HTML meta tags string
*/
fun getMetaTags(): String {
val tokenData = generateToken()
return """
<meta name="fcs-public-key" content="${tokenData.publicKey}">
<meta name="fcs-token" content="${tokenData.token}">
<meta name="fcs-token-expiry" content="${tokenData.expiry}">
""".trimIndent()
}
}
// ============================================
// USAGE EXAMPLES
// ============================================
fun main() {
// Example 1: Generate token
val generator = FcsTokenGenerator("your_access_key", "your_public_key")
val tokenData = generator.generateToken()
println("Token Data: $tokenData")
// Example 2: Get as JSON
println("\nJSON: ${generator.toJson()}")
// Example 3: Get meta tags
println("\nMeta Tags:\n${generator.getMetaTags()}")
}
// Spring Boot example:
// @RestController
// class FcsTokenController {
// @GetMapping("/fcs-token")
// fun getToken(): TokenData {
// val generator = FcsTokenGenerator(
// System.getenv("FCS_ACCESS_KEY"),
// System.getenv("FCS_PUBLIC_KEY")
// )
// return generator.generateToken()
// }
// }
// Ktor example:
// routing {
// get("/fcs-token") {
// val generator = FcsTokenGenerator(
// System.getenv("FCS_ACCESS_KEY"),
// System.getenv("FCS_PUBLIC_KEY")
// )
// call.respond(generator.generateToken())
// }
// }