summaryrefslogtreecommitdiff
path: root/shrine/utils/auth/auth.go
diff options
context:
space:
mode:
authorBobby <[email protected]>2026-03-07 08:52:35 +0530
committerBobby <[email protected]>2026-03-07 08:52:35 +0530
commit82409d6b83de1baab69c166af8f04c6e9fe9069f (patch)
tree678b3ee2242b20da49c8cf1ff0ec509d0c8ef1e1 /shrine/utils/auth/auth.go
parenta97d1ad37463107b462958d92f596ebb80254b77 (diff)
downloadpagoda-82409d6b83de1baab69c166af8f04c6e9fe9069f.tar.xz
pagoda-82409d6b83de1baab69c166af8f04c6e9fe9069f.zip
feat: Implement letter service with CRUD operations and message handling
- Added letter service to manage letters, including listing, creating, and editing letters and messages. - Implemented functionality for sending and receiving messages within letters. - Introduced pagination for letter listings and message retrieval. - Added attachment upload capabilities for letters. - Created detailed responses for letter and message retrieval. feat: Introduce stats service for user statistics - Added a service to retrieve user statistics, including newest and online citizens. feat: Create ticket service for user support tickets - Implemented ticket management service with functionalities to create, reply, and update tickets. - Added support for ticket categories and their management. feat: Add verification service for user account verification - Implemented functionality to send verification emails for account activation. feat: Develop warning service for user warnings - Added service to issue warnings to users, deactivate warnings, and list user warnings. feat: Create email templates for account status notifications - Added HTML templates for account ban and disable notifications. feat: Define request and response types for account, ticket, letter, and warning services - Created structured request and response types for various services to ensure consistent data handling. feat: Implement utility functions for authentication and sanitization - Added functions for validating user hierarchy and sanitizing HTML input. - Implemented token generation and hashing utilities for secure operations.
Diffstat (limited to 'shrine/utils/auth/auth.go')
-rw-r--r--shrine/utils/auth/auth.go26
1 files changed, 4 insertions, 22 deletions
diff --git a/shrine/utils/auth/auth.go b/shrine/utils/auth/auth.go
index 88bce99..44db75a 100644
--- a/shrine/utils/auth/auth.go
+++ b/shrine/utils/auth/auth.go
@@ -1,13 +1,10 @@
package auth
import (
- "crypto/hmac"
- "crypto/rand"
- "crypto/sha256"
- "encoding/hex"
"shrine/config"
"shrine/models"
"shrine/repositories"
+ "shrine/utils/crypto"
"shrine/utils/meta"
"strings"
"time"
@@ -20,21 +17,6 @@ const (
tokenHashKey = "__auth_token_hash"
)
-func GenerateToken() (string, error) {
- bytes := make([]byte, 32)
- _, err := rand.Read(bytes)
- if err != nil {
- return "", err
- }
- return hex.EncodeToString(bytes), nil
-}
-
-func HashToken(rawToken string) string {
- mac := hmac.New(sha256.New, []byte(config.Server.Secret))
- mac.Write([]byte(rawToken))
- return hex.EncodeToString(mac.Sum(nil))
-}
-
func IsAuthenticated(context *fiber.Ctx) bool {
header, ok := meta.Request(context).Header("Authorization")
if !ok || !strings.HasPrefix(header, "Bearer ") {
@@ -42,7 +24,7 @@ func IsAuthenticated(context *fiber.Ctx) bool {
}
rawToken := strings.TrimPrefix(header, "Bearer ")
- tokenHash := HashToken(rawToken)
+ tokenHash := crypto.HashToken(rawToken)
token, err := repositories.FindValidToken(tokenHash)
if err != nil {
@@ -108,7 +90,7 @@ func GetTokenHash(context *fiber.Ctx) string {
}
func IssueToken(context *fiber.Ctx, userID uint) (string, error) {
- token, err := GenerateToken()
+ token, err := crypto.GenerateToken()
if err != nil {
return "", err
}
@@ -117,7 +99,7 @@ func IssueToken(context *fiber.Ctx, userID uint) (string, error) {
userAgent, _ := request.Header("User-Agent")
record := models.Token{
- TokenHash: HashToken(token),
+ TokenHash: crypto.HashToken(token),
UserID: userID,
ExpiresAt: time.Now().Add(config.Server.TokenExpiry),
IPAddress: request.IP,