2020-05-20 12:28:08 +00:00
|
|
|
package twilio
|
|
|
|
|
2024-09-26 07:14:33 +00:00
|
|
|
import (
|
|
|
|
newTwilio "github.com/twilio/twilio-go"
|
|
|
|
verify "github.com/twilio/twilio-go/rest/verify/v2"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
|
|
|
)
|
|
|
|
|
2023-03-16 17:24:30 +00:00
|
|
|
type Config struct {
|
2024-09-26 07:14:33 +00:00
|
|
|
SID string
|
|
|
|
Token string
|
|
|
|
SenderNumber string
|
|
|
|
VerifyServiceSID string
|
2022-02-21 12:22:20 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 17:24:30 +00:00
|
|
|
func (t *Config) IsValid() bool {
|
2022-02-21 12:22:20 +00:00
|
|
|
return t.SID != "" && t.Token != "" && t.SenderNumber != ""
|
2020-05-20 12:28:08 +00:00
|
|
|
}
|
2024-09-26 07:14:33 +00:00
|
|
|
|
|
|
|
func (t *Config) VerifyCode(verificationID, code string) error {
|
|
|
|
client := newTwilio.NewRestClientWithParams(newTwilio.ClientParams{Username: t.SID, Password: t.Token})
|
|
|
|
checkParams := &verify.CreateVerificationCheckParams{}
|
|
|
|
checkParams.SetVerificationSid(verificationID)
|
|
|
|
checkParams.SetCode(code)
|
|
|
|
resp, err := client.VerifyV2.CreateVerificationCheck(t.VerifyServiceSID, checkParams)
|
|
|
|
if err != nil || resp.Status == nil {
|
|
|
|
return zerrors.ThrowInvalidArgument(err, "TWILI-JK3ta", "Errors.User.Code.NotFound")
|
|
|
|
}
|
|
|
|
switch *resp.Status {
|
|
|
|
case "approved":
|
|
|
|
return nil
|
|
|
|
case "expired":
|
|
|
|
return zerrors.ThrowInvalidArgument(nil, "TWILI-SF3ba", "Errors.User.Code.Expired")
|
|
|
|
case "max_attempts_reached":
|
|
|
|
return zerrors.ThrowInvalidArgument(nil, "TWILI-Ok39a", "Errors.User.Code.NotFound")
|
|
|
|
default:
|
|
|
|
return zerrors.ThrowInvalidArgument(nil, "TWILI-Skwe4", "Errors.User.Code.Invalid")
|
|
|
|
}
|
|
|
|
}
|