M3O SDK Concept

This commit is contained in:
Ben Toogood
2020-11-25 16:06:44 +00:00
parent 7818a050c2
commit 5fa6c1244f
21 changed files with 1293 additions and 524 deletions

View File

@@ -0,0 +1,99 @@
package handler
import (
"crypto/rand"
otp "github.com/m3o/m3o-go/examples/otp/proto"
"github.com/m3o/m3o-go/context"
"github.com/m3o/m3o-go/errors"
"github.com/m3o/m3o-go/sms"
"github.com/m3o/m3o-go/store/keyvalue"
)
const (
otpChars = "1234567890"
otpLength = 6
otpTTL = time.Minute * 5
)
var (
errMissingPhoneNumber = errors.BadRequest("Missing PhoneNumber")
errMissingCode = errors.BadRequest("Missing Code")
errInvalidCode = errors.BadRequest("Invalid Code")
)
// OTP satisfies the OTP handler interface. Note: this handler is an exampple and not intended for
// production use - the handler does not enforce verification limits so it would be possible to brute
// force the verification process. It is advised to use a retry limit of 3 in real-world applications.
type OTP struct {
SMS sms.Service
Store keyvalue.Store
}
// Send a one time password to the phone number provided
func (o *OTP) Send(ctx context.Context, req *otp.SendRequest) (*otp.SendResponse, error) {
// validate the request
if len(req.PhoneNumber) == 0 {
return nil, errMissingPhoneNumber
}
// generate and send the OPT
otp := generateOTP()
msg := fmt.Sprintf("Your M3O verification code is %v", otp
if err := o.SMS.Send(req.PhoneNumber, msg); err != nil {
return nil, err
}
// write the code to the store
err := o.Store.Write(req.PhoneNumber, otp, keyvalue.WriteOptions{
Expiry: time.Now().Add(otpTTL),
})
if err != nil {
return nil, errors.InternalServerError("Error writing OTP to the store")
}
// return no error, indicating the verification code send successfully
return &otp.SendResponse{}, nil
}
// Validate a one time password
func (o *OTP) Verify(ctx context.Context, req *otp.VerifyRequest) (*otp.VerifyResponse, error) {
// validate the request
if len(req.PhoneNumber) == 0 {
return nil, errMissingPhoneNumber
}
if len(req.Code) == 0 {
return nil, errMissingCode
}
// lookup the phone number from the store
rec, err := o.Store.Read(req.PhoneNumber)
if err == keyvalue.ErrRecordNotFound {
return nil, errMissingCode
} else if err != nil {
return nil, errors.InternalServerError("Error reading OTP from the store")
}
// ensure the codes match
if rec.Value != req.Code {
return nil, errInvalidCode
}
// return no error, indicating the verification was successful
return &otp.VerifyResponse{}, nil
}
func generateOTP() (string, error) {
buffer := make([]byte, otpLength)
_, err := rand.Read(buffer)
if err != nil {
return "", err
}
otpCharsLength := len(otpChars)
for i := 0; i < otpLength; i++ {
buffer[i] = otpChars[int(buffer[i])%otpCharsLength]
}
return string(buffer), nil
}

View File

@@ -0,0 +1,23 @@
package handler
import (
"testing"
"github.com/m3o/m3o-go/sms"
"github.com/m3o/m3o-go/store/keyvalue"
)
func TestHandler(t *testing.T) {
h := OTP{
SMS: sms.NewMock(),
Store: keyvalue.NewMock(),
}
t.Run("Send", func(t *testing.T) {
})
t.Run("Verify", func(t *testing.T) {
})
}

19
examples/otp/main.go Normal file
View File

@@ -0,0 +1,19 @@
package otp
import (
"github.com/m3o/m3o-go/examples/otp/handler"
"github.com/m3o/m3o-go/server"
"github.com/m3o/m3o-go/sms"
"github.com/m3o/m3o-go/store/keyvalue"
)
func main() {
// register the handler
server.RegisterHandler(handler.OTP{
SMS: sms.NewClient(),
Store: keyvalue.NewClient(),
})
// run the server
server.Run()
}

View File

@@ -0,0 +1,339 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.23.0
// protoc v3.13.0
// source: otp/proto/otp.proto
package otp
import (
proto "github.com/golang/protobuf/proto"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
// This is a compile-time assertion that a sufficiently up-to-date version
// of the legacy proto package is being used.
const _ = proto.ProtoPackageIsVersion4
type SendRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
PhoneNumber string `protobuf:"bytes,1,opt,name=phone_number,json=phoneNumber,proto3" json:"phone_number,omitempty"`
}
func (x *SendRequest) Reset() {
*x = SendRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_otp_proto_otp_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *SendRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*SendRequest) ProtoMessage() {}
func (x *SendRequest) ProtoReflect() protoreflect.Message {
mi := &file_otp_proto_otp_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use SendRequest.ProtoReflect.Descriptor instead.
func (*SendRequest) Descriptor() ([]byte, []int) {
return file_otp_proto_otp_proto_rawDescGZIP(), []int{0}
}
func (x *SendRequest) GetPhoneNumber() string {
if x != nil {
return x.PhoneNumber
}
return ""
}
type SendResponse struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
}
func (x *SendResponse) Reset() {
*x = SendResponse{}
if protoimpl.UnsafeEnabled {
mi := &file_otp_proto_otp_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *SendResponse) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*SendResponse) ProtoMessage() {}
func (x *SendResponse) ProtoReflect() protoreflect.Message {
mi := &file_otp_proto_otp_proto_msgTypes[1]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use SendResponse.ProtoReflect.Descriptor instead.
func (*SendResponse) Descriptor() ([]byte, []int) {
return file_otp_proto_otp_proto_rawDescGZIP(), []int{1}
}
type VerifyRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
PhoneNumber string `protobuf:"bytes,1,opt,name=phone_number,json=phoneNumber,proto3" json:"phone_number,omitempty"`
Code string `protobuf:"bytes,2,opt,name=code,proto3" json:"code,omitempty"`
}
func (x *VerifyRequest) Reset() {
*x = VerifyRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_otp_proto_otp_proto_msgTypes[2]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *VerifyRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*VerifyRequest) ProtoMessage() {}
func (x *VerifyRequest) ProtoReflect() protoreflect.Message {
mi := &file_otp_proto_otp_proto_msgTypes[2]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use VerifyRequest.ProtoReflect.Descriptor instead.
func (*VerifyRequest) Descriptor() ([]byte, []int) {
return file_otp_proto_otp_proto_rawDescGZIP(), []int{2}
}
func (x *VerifyRequest) GetPhoneNumber() string {
if x != nil {
return x.PhoneNumber
}
return ""
}
func (x *VerifyRequest) GetCode() string {
if x != nil {
return x.Code
}
return ""
}
type VerifyResponse struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
}
func (x *VerifyResponse) Reset() {
*x = VerifyResponse{}
if protoimpl.UnsafeEnabled {
mi := &file_otp_proto_otp_proto_msgTypes[3]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *VerifyResponse) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*VerifyResponse) ProtoMessage() {}
func (x *VerifyResponse) ProtoReflect() protoreflect.Message {
mi := &file_otp_proto_otp_proto_msgTypes[3]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use VerifyResponse.ProtoReflect.Descriptor instead.
func (*VerifyResponse) Descriptor() ([]byte, []int) {
return file_otp_proto_otp_proto_rawDescGZIP(), []int{3}
}
var File_otp_proto_otp_proto protoreflect.FileDescriptor
var file_otp_proto_otp_proto_rawDesc = []byte{
0x0a, 0x13, 0x6f, 0x74, 0x70, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x6f, 0x74, 0x70, 0x2e,
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x6f,
0x74, 0x70, 0x22, 0x30, 0x0a, 0x0b, 0x53, 0x65, 0x6e, 0x64, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73,
0x74, 0x12, 0x21, 0x0a, 0x0c, 0x70, 0x68, 0x6f, 0x6e, 0x65, 0x5f, 0x6e, 0x75, 0x6d, 0x62, 0x65,
0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x70, 0x68, 0x6f, 0x6e, 0x65, 0x4e, 0x75,
0x6d, 0x62, 0x65, 0x72, 0x22, 0x0e, 0x0a, 0x0c, 0x53, 0x65, 0x6e, 0x64, 0x52, 0x65, 0x73, 0x70,
0x6f, 0x6e, 0x73, 0x65, 0x22, 0x46, 0x0a, 0x0d, 0x56, 0x65, 0x72, 0x69, 0x66, 0x79, 0x52, 0x65,
0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x21, 0x0a, 0x0c, 0x70, 0x68, 0x6f, 0x6e, 0x65, 0x5f, 0x6e,
0x75, 0x6d, 0x62, 0x65, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x70, 0x68, 0x6f,
0x6e, 0x65, 0x4e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65,
0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x22, 0x10, 0x0a, 0x0e,
0x56, 0x65, 0x72, 0x69, 0x66, 0x79, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x32, 0x85,
0x01, 0x0a, 0x03, 0x4f, 0x54, 0x50, 0x12, 0x3b, 0x0a, 0x04, 0x53, 0x65, 0x6e, 0x64, 0x12, 0x18,
0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x6f, 0x74, 0x70, 0x2e, 0x53, 0x65, 0x6e,
0x64, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x19, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70,
0x6c, 0x65, 0x2e, 0x6f, 0x74, 0x70, 0x2e, 0x53, 0x65, 0x6e, 0x64, 0x52, 0x65, 0x73, 0x70, 0x6f,
0x6e, 0x73, 0x65, 0x12, 0x41, 0x0a, 0x06, 0x56, 0x65, 0x72, 0x69, 0x66, 0x79, 0x12, 0x1a, 0x2e,
0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x6f, 0x74, 0x70, 0x2e, 0x56, 0x65, 0x72, 0x69,
0x66, 0x79, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1b, 0x2e, 0x65, 0x78, 0x61, 0x6d,
0x70, 0x6c, 0x65, 0x2e, 0x6f, 0x74, 0x70, 0x2e, 0x56, 0x65, 0x72, 0x69, 0x66, 0x79, 0x52, 0x65,
0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x2e, 0x5a, 0x2c, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62,
0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6d, 0x33, 0x6f, 0x2f, 0x6d, 0x33, 0x6f, 0x2d, 0x67, 0x6f, 0x2d,
0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x73, 0x2f, 0x6f, 0x74, 0x70, 0x2f, 0x70, 0x72, 0x6f,
0x74, 0x6f, 0x3b, 0x6f, 0x74, 0x70, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
file_otp_proto_otp_proto_rawDescOnce sync.Once
file_otp_proto_otp_proto_rawDescData = file_otp_proto_otp_proto_rawDesc
)
func file_otp_proto_otp_proto_rawDescGZIP() []byte {
file_otp_proto_otp_proto_rawDescOnce.Do(func() {
file_otp_proto_otp_proto_rawDescData = protoimpl.X.CompressGZIP(file_otp_proto_otp_proto_rawDescData)
})
return file_otp_proto_otp_proto_rawDescData
}
var file_otp_proto_otp_proto_msgTypes = make([]protoimpl.MessageInfo, 4)
var file_otp_proto_otp_proto_goTypes = []interface{}{
(*SendRequest)(nil), // 0: example.otp.SendRequest
(*SendResponse)(nil), // 1: example.otp.SendResponse
(*VerifyRequest)(nil), // 2: example.otp.VerifyRequest
(*VerifyResponse)(nil), // 3: example.otp.VerifyResponse
}
var file_otp_proto_otp_proto_depIdxs = []int32{
0, // 0: example.otp.OTP.Send:input_type -> example.otp.SendRequest
2, // 1: example.otp.OTP.Verify:input_type -> example.otp.VerifyRequest
1, // 2: example.otp.OTP.Send:output_type -> example.otp.SendResponse
3, // 3: example.otp.OTP.Verify:output_type -> example.otp.VerifyResponse
2, // [2:4] is the sub-list for method output_type
0, // [0:2] is the sub-list for method input_type
0, // [0:0] is the sub-list for extension type_name
0, // [0:0] is the sub-list for extension extendee
0, // [0:0] is the sub-list for field type_name
}
func init() { file_otp_proto_otp_proto_init() }
func file_otp_proto_otp_proto_init() {
if File_otp_proto_otp_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_otp_proto_otp_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*SendRequest); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_otp_proto_otp_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*SendResponse); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_otp_proto_otp_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*VerifyRequest); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_otp_proto_otp_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*VerifyResponse); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_otp_proto_otp_proto_rawDesc,
NumEnums: 0,
NumMessages: 4,
NumExtensions: 0,
NumServices: 1,
},
GoTypes: file_otp_proto_otp_proto_goTypes,
DependencyIndexes: file_otp_proto_otp_proto_depIdxs,
MessageInfos: file_otp_proto_otp_proto_msgTypes,
}.Build()
File_otp_proto_otp_proto = out.File
file_otp_proto_otp_proto_rawDesc = nil
file_otp_proto_otp_proto_goTypes = nil
file_otp_proto_otp_proto_depIdxs = nil
}

View File

@@ -0,0 +1,101 @@
// Code generated by protoc-gen-micro. DO NOT EDIT.
// source: otp/proto/otp.proto
package otp
import (
fmt "fmt"
math "math"
proto "github.com/golang/protobuf/proto"
context "context"
api "github.com/micro/micro/v3/service/api"
client "github.com/micro/micro/v3/service/client"
server "github.com/micro/micro/v3/service/server"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
// Reference imports to suppress errors if they are not otherwise used.
var _ api.Endpoint
var _ context.Context
var _ client.Option
var _ server.Option
// Api Endpoints for OTP service
func NewOTPEndpoints() []*api.Endpoint {
return []*api.Endpoint{}
}
// Client API for OTP service
type OTPService interface {
Send(ctx context.Context, in *SendRequest, opts ...client.CallOption) (*SendResponse, error)
Verify(ctx context.Context, in *VerifyRequest, opts ...client.CallOption) (*VerifyResponse, error)
}
type oTPService struct {
c client.Client
name string
}
func NewOTPClient() OTPService {
return &oTPService{}
}
func NewOTPMock() OTPService {
return &oTPService{}
}
func (c *oTPService) Send(ctx context.Context, in *SendRequest, opts ...client.CallOption) (*SendResponse, error) {
req := c.c.NewRequest(c.name, "OTP.Send", in)
out := new(SendResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *oTPService) Verify(ctx context.Context, in *VerifyRequest, opts ...client.CallOption) (*VerifyResponse, error) {
req := c.c.NewRequest(c.name, "OTP.Verify", in)
out := new(VerifyResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// Server API for OTP service
type OTPHandler interface {
Send(context.Context, *SendRequest) (*SendResponse, error)
Verify(context.Context, *VerifyRequest) (*VerifyResponse, error)
}
type oTPHandler struct {
OTPHandler
}
func (h *oTPHandler) Send(ctx context.Context, req *SendRequest) (*SendResponse, error) {
return h.OTPHandler.Send(ctx, req)
}
func (h *oTPHandler) Verify(ctx context.Context, req *VerifyRequest) (*VerifyResponse, error) {
return h.OTPHandler.Verify(ctx, req)
}

View File

@@ -0,0 +1,22 @@
syntax = "proto3";
package example.otp;
option go_package = "github.com/m3o/m3o-go-examples/otp/proto;otp";
service OTP {
rpc Send(SendRequest) returns (SendResponse);
rpc Verify(VerifyRequest) returns (VerifyResponse);
}
message SendRequest {
string phone_number = 1;
}
message SendResponse {}
message VerifyRequest {
string phone_number = 1;
string code = 2;
}
message VerifyResponse {}