-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken_generator.go
More file actions
117 lines (102 loc) · 2.88 KB
/
token_generator.go
File metadata and controls
117 lines (102 loc) · 2.88 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
/**
* FCS API - Token Generator (Go)
*
* 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 <[email protected]>
* @link https://fcsapi.com
*/
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"time"
)
// TokenGenerator generates secure tokens for FCS API
type TokenGenerator struct {
AccessKey string
PublicKey string
TokenExpiry int64
}
// TokenData represents the generated token
type TokenData struct {
Token string `json:"_token"`
Expiry int64 `json:"_expiry"`
PublicKey string `json:"_public_key"`
}
// NewTokenGenerator creates a new token generator
// Token expiry options (in seconds): 300 (5min), 900 (15min), 1800 (30min), 3600 (1hr), 86400 (24hr)
func NewTokenGenerator(accessKey, publicKey string, tokenExpiry int64) *TokenGenerator {
if tokenExpiry == 0 {
tokenExpiry = 3600
}
return &TokenGenerator{
AccessKey: accessKey,
PublicKey: publicKey,
TokenExpiry: tokenExpiry,
}
}
// GenerateToken generates a token for frontend authentication
func (tg *TokenGenerator) GenerateToken() TokenData {
expiry := time.Now().Unix() + tg.TokenExpiry
message := fmt.Sprintf("%s%d", tg.PublicKey, expiry)
h := hmac.New(sha256.New, []byte(tg.AccessKey))
h.Write([]byte(message))
token := hex.EncodeToString(h.Sum(nil))
return TokenData{
Token: token,
Expiry: expiry,
PublicKey: tg.PublicKey,
}
}
// ToJSON returns the token data as JSON string
func (tg *TokenGenerator) ToJSON() (string, error) {
tokenData := tg.GenerateToken()
jsonBytes, err := json.Marshal(tokenData)
if err != nil {
return "", err
}
return string(jsonBytes), nil
}
// GetMetaTags returns HTML meta tags for the token
func (tg *TokenGenerator) GetMetaTags() string {
tokenData := tg.GenerateToken()
return fmt.Sprintf(`<meta name="fcs-public-key" content="%s">
<meta name="fcs-token" content="%s">
<meta name="fcs-token-expiry" content="%d">`,
tokenData.PublicKey,
tokenData.Token,
tokenData.Expiry)
}
// ============================================
// USAGE EXAMPLES
// ============================================
func main() {
// Example 1: Generate token
generator := NewTokenGenerator("your_access_key", "your_public_key", 3600)
tokenData := generator.GenerateToken()
fmt.Printf("Token Data: %+v\n", tokenData)
// Example 2: Get as JSON
jsonStr, _ := generator.ToJSON()
fmt.Printf("\nJSON: %s\n", jsonStr)
// Example 3: Get meta tags
fmt.Printf("\nMeta Tags:\n%s\n", generator.GetMetaTags())
}
// Gin example:
// func getFcsToken(c *gin.Context) {
// generator := NewTokenGenerator(
// os.Getenv("FCS_ACCESS_KEY"),
// os.Getenv("FCS_PUBLIC_KEY"),
// 3600,
// )
// c.JSON(200, generator.GenerateToken())
// }