From d946b993ee4863a256a8212ebd3830cab8d6670e Mon Sep 17 00:00:00 2001 From: Tue Date: Mon, 7 Nov 2022 11:21:27 +0700 Subject: [PATCH] build auth sms --- client/auth_sms.go | 37 +++++++++++++++++++++++++++++++++++++ model/auth_sms_request.go | 7 +++++++ model/auth_sms_response.go | 1 + subject/auth_sms.go | 15 +++++++++++++++ 4 files changed, 60 insertions(+) create mode 100644 client/auth_sms.go create mode 100644 model/auth_sms_request.go create mode 100644 model/auth_sms_response.go create mode 100644 subject/auth_sms.go diff --git a/client/auth_sms.go b/client/auth_sms.go new file mode 100644 index 0000000..c86ad50 --- /dev/null +++ b/client/auth_sms.go @@ -0,0 +1,37 @@ +package client + +import ( + "encoding/json" + "errors" + "git.selly.red/Selly-Modules/natsio" + "git.selly.red/Selly-Modules/natsio/model" + "git.selly.red/Selly-Modules/natsio/subject" +) + +// AuthSMS ... +type AuthSMS struct{} + +// GetAuthSMS ... +func GetAuthSMS() AuthSMS { + return AuthSMS{} +} + +func (s AuthSMS) CheckPermission(p model.CheckPermissionRequest) error { + msg, err := natsio.GetServer().Request(subject.AuthSMS.CheckPermission, toBytes(p)) + if err != nil { + return err + } + + var r struct { + Error string `json:"error"` + } + + if err = json.Unmarshal(msg.Data, &r); err != nil { + return err + } + if r.Error != "" { + return errors.New(r.Error) + } + + return nil +} diff --git a/model/auth_sms_request.go b/model/auth_sms_request.go new file mode 100644 index 0000000..7ee616a --- /dev/null +++ b/model/auth_sms_request.go @@ -0,0 +1,7 @@ +package model + +type CheckPermissionRequest struct { + Value []string `json:"value"` + ID string `json:"_id"` + DeviceID string `json:"deviceId"` +} diff --git a/model/auth_sms_response.go b/model/auth_sms_response.go new file mode 100644 index 0000000..8b53790 --- /dev/null +++ b/model/auth_sms_response.go @@ -0,0 +1 @@ +package model diff --git a/subject/auth_sms.go b/subject/auth_sms.go new file mode 100644 index 0000000..92fea0c --- /dev/null +++ b/subject/auth_sms.go @@ -0,0 +1,15 @@ +package subject + +import "fmt" + +func getAuthSMSValue(val string) string { + return fmt.Sprintf("%s.%s", prefixes.AuthSMS, val) +} + +var AuthSMS = struct { + // AuthSMS + CheckPermission string +}{ + // Users + CheckPermission: getAuthSMSValue("check_permission"), +}