1
0
forked from baron/baron-sso

SMS 발송 및 Redis 기반 인증 코드 검증, JWT 발급 기능 구현

This commit is contained in:
2026-01-06 16:32:43 +09:00
parent 362b6b60d4
commit 659ccfbe53
7 changed files with 170 additions and 9 deletions

View File

@@ -0,0 +1,62 @@
package service
import (
"context"
"os"
"time"
"github.com/go-redis/redis/v8"
)
var ctx = context.Background()
type RedisService struct {
Client *redis.Client
}
// NewRedisService creates and returns a new RedisService
func NewRedisService() (*RedisService, error) {
redisAddr := os.Getenv("REDIS_ADDR")
if redisAddr == "" {
redisAddr = "localhost:6379" // Fallback for local dev without Docker
}
rdb := redis.NewClient(&redis.Options{
Addr: redisAddr,
})
// Ping the server to check the connection
if _, err := rdb.Ping(ctx).Result(); err != nil {
return nil, err
}
return &RedisService{Client: rdb}, nil
}
// StoreVerificationCode saves the SMS verification code with a 3-minute expiration
func (s *RedisService) StoreVerificationCode(phone, code string) error {
// Key format: "sms_verify:01012345678"
key := "sms_verify:" + phone
expiration := 3 * time.Minute
err := s.Client.Set(ctx, key, code, expiration).Err()
return err
}
// GetVerificationCode retrieves the SMS verification code
func (s *RedisService) GetVerificationCode(phone string) (string, error) {
key := "sms_verify:" + phone
code, err := s.Client.Get(ctx, key).Result()
if err == redis.Nil {
// Key does not exist (expired or incorrect phone number)
return "", nil
} else if err != nil {
return "", err
}
return code, nil
}
// DeleteVerificationCode removes the verification code after successful verification
func (s *RedisService) DeleteVerificationCode(phone string) error {
key := "sms_verify:" + phone
return s.Client.Del(ctx, key).Err()
}

View File

@@ -12,11 +12,11 @@ import (
"net/http"
"os"
"strconv"
"strings"
"time"
"baron-sso-backend/internal/domain"
)
type SmsServiceImpl struct {
accessKey string
secretKey string
@@ -25,11 +25,16 @@ type SmsServiceImpl struct {
}
func NewSmsService() domain.SmsService {
// Sanitize sender phone number right after reading from env
rawSenderPhone := os.Getenv("NAVER_SENDER_PHONE_NUMBER")
sanitizedSenderPhone := strings.ReplaceAll(rawSenderPhone, "-", "")
log.Printf("[서비스 초기화] 발신자 번호 처리: 원본='%s', 정제 후='%s'", rawSenderPhone, sanitizedSenderPhone)
return &SmsServiceImpl{
accessKey: os.Getenv("NAVER_CLOUD_ACCESS_KEY"),
secretKey: os.Getenv("NAVER_CLOUD_SECRET_KEY"),
serviceID: os.Getenv("NAVER_CLOUD_SERVICE_ID"),
senderPhone: os.Getenv("NAVER_SENDER_PHONE_NUMBER"),
senderPhone: sanitizedSenderPhone,
}
}