|
| 1 | +package sms |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/base64" |
| 7 | + "encoding/json" |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | + "io" |
| 11 | + "net/http" |
| 12 | + "strings" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/nhost/hasura-auth/go/notifications" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + modicaAPIURL = "https://api.modicagroup.com/rest/sms/v2/messages" |
| 20 | + clientTimeoutSeconds = 30 |
| 21 | + maxIdleConns = 10 |
| 22 | + idleTimeoutSeconds = 30 |
| 23 | + tlsTimeoutSeconds = 10 |
| 24 | +) |
| 25 | + |
| 26 | +var ( |
| 27 | + ErrInvalidPhoneFormat = errors.New( |
| 28 | + "phone number must be in international format (e.g., +64211234567)", |
| 29 | + ) |
| 30 | + ErrEmptyMessage = errors.New("message content cannot be empty") |
| 31 | + ErrSMSAPIError = errors.New("SMS API error") |
| 32 | +) |
| 33 | + |
| 34 | +type ModicaSMS struct { |
| 35 | + client *http.Client |
| 36 | + username string |
| 37 | + password string |
| 38 | +} |
| 39 | + |
| 40 | +type modicaSMSRequest struct { |
| 41 | + Destination string `json:"destination"` |
| 42 | + Content string `json:"content"` |
| 43 | +} |
| 44 | + |
| 45 | +func NewModicaSMS( |
| 46 | + templates *notifications.Templates, |
| 47 | + otpGenerator func() (string, string, error), |
| 48 | + otpHasher func(string) (string, error), |
| 49 | + username string, password string, |
| 50 | + db DB, |
| 51 | +) *SMS { |
| 52 | + client := &http.Client{ //nolint:exhaustruct |
| 53 | + Timeout: clientTimeoutSeconds * time.Second, |
| 54 | + Transport: &http.Transport{ //nolint:exhaustruct |
| 55 | + MaxIdleConns: maxIdleConns, |
| 56 | + IdleConnTimeout: idleTimeoutSeconds * time.Second, |
| 57 | + DisableCompression: false, |
| 58 | + TLSHandshakeTimeout: tlsTimeoutSeconds * time.Second, |
| 59 | + }, |
| 60 | + } |
| 61 | + |
| 62 | + return NewSMS( |
| 63 | + &ModicaSMS{ |
| 64 | + client: client, |
| 65 | + username: username, |
| 66 | + password: password, |
| 67 | + }, |
| 68 | + otpGenerator, |
| 69 | + otpHasher, |
| 70 | + templates, |
| 71 | + db, |
| 72 | + ) |
| 73 | +} |
| 74 | + |
| 75 | +func (s *ModicaSMS) SendSMS(to string, body string) error { |
| 76 | + // Validate inputs according to Modica API requirements |
| 77 | + if !strings.HasPrefix(to, "+") { |
| 78 | + return ErrInvalidPhoneFormat |
| 79 | + } |
| 80 | + |
| 81 | + if len(body) == 0 { |
| 82 | + return ErrEmptyMessage |
| 83 | + } |
| 84 | + |
| 85 | + reqBody := modicaSMSRequest{ |
| 86 | + Destination: to, |
| 87 | + Content: body, |
| 88 | + } |
| 89 | + |
| 90 | + jsonData, err := json.Marshal(reqBody) |
| 91 | + if err != nil { |
| 92 | + return fmt.Errorf("failed to marshal request: %w", err) |
| 93 | + } |
| 94 | + |
| 95 | + req, err := http.NewRequestWithContext( |
| 96 | + context.Background(), |
| 97 | + http.MethodPost, |
| 98 | + modicaAPIURL, |
| 99 | + bytes.NewBuffer(jsonData), |
| 100 | + ) |
| 101 | + if err != nil { |
| 102 | + return fmt.Errorf("failed to create request: %w", err) |
| 103 | + } |
| 104 | + |
| 105 | + // Set headers |
| 106 | + req.Header.Set("Content-Type", "application/json") |
| 107 | + req.Header.Set("Authorization", "Basic "+s.basicAuth()) |
| 108 | + |
| 109 | + resp, err := s.client.Do(req) |
| 110 | + if err != nil { |
| 111 | + return fmt.Errorf("failed to send request: %w", err) |
| 112 | + } |
| 113 | + defer resp.Body.Close() |
| 114 | + |
| 115 | + if resp.StatusCode != http.StatusAccepted { |
| 116 | + bodyBytes, _ := io.ReadAll(resp.Body) |
| 117 | + |
| 118 | + return fmt.Errorf("%w: HTTP %d: %s", ErrSMSAPIError, resp.StatusCode, string(bodyBytes)) |
| 119 | + } |
| 120 | + |
| 121 | + return nil |
| 122 | +} |
| 123 | + |
| 124 | +func (s *ModicaSMS) basicAuth() string { |
| 125 | + auth := s.username + ":" + s.password |
| 126 | + return base64.StdEncoding.EncodeToString([]byte(auth)) |
| 127 | +} |
0 commit comments