mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-19 05:55:19 +00:00
add expiry/size to otp
This commit is contained in:
@@ -15,20 +15,33 @@ import (
|
|||||||
|
|
||||||
type Otp struct{}
|
type Otp struct{}
|
||||||
|
|
||||||
|
type otpKey struct {
|
||||||
|
Secret string
|
||||||
|
Expiry uint
|
||||||
|
}
|
||||||
|
|
||||||
func (e *Otp) Generate(ctx context.Context, req *pb.GenerateRequest, rsp *pb.GenerateResponse) error {
|
func (e *Otp) Generate(ctx context.Context, req *pb.GenerateRequest, rsp *pb.GenerateResponse) error {
|
||||||
if len(req.Id) == 0 {
|
if len(req.Id) == 0 {
|
||||||
return errors.BadRequest("otp.generate", "missing id")
|
return errors.BadRequest("otp.generate", "missing id")
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if a key exists for the user
|
// check if a key exists for the user
|
||||||
var secret string
|
okey := new(otpKey)
|
||||||
|
|
||||||
if err := cache.Context(ctx).Get(req.Id, &secret); err != nil {
|
if req.Expiry <= 0 {
|
||||||
|
req.Expiry = 60
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.Size <= 0 {
|
||||||
|
req.Size = 6
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := cache.Context(ctx).Get("otp:"+req.Id, &okey); err != nil || okey == nil {
|
||||||
// generate a key
|
// generate a key
|
||||||
key, err := totp.Generate(totp.GenerateOpts{
|
key, err := totp.Generate(totp.GenerateOpts{
|
||||||
Issuer: "Micro",
|
Issuer: "Micro",
|
||||||
AccountName: req.Id,
|
AccountName: req.Id,
|
||||||
Period: 60,
|
Period: 300,
|
||||||
Algorithm: otp.AlgorithmSHA1,
|
Algorithm: otp.AlgorithmSHA1,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -36,19 +49,24 @@ func (e *Otp) Generate(ctx context.Context, req *pb.GenerateRequest, rsp *pb.Gen
|
|||||||
return errors.InternalServerError("otp.generate", "failed to generate code")
|
return errors.InternalServerError("otp.generate", "failed to generate code")
|
||||||
}
|
}
|
||||||
|
|
||||||
secret = key.Secret()
|
okey = &otpKey{
|
||||||
|
Secret: key.Secret(),
|
||||||
|
Expiry: uint(req.Expiry),
|
||||||
|
}
|
||||||
|
|
||||||
if err := cache.Context(ctx).Set(req.Id, secret, time.Now().Add(time.Minute*5)); err != nil {
|
if err := cache.Context(ctx).Set("otp:"+req.Id, okey, time.Now().Add(time.Minute*5)); err != nil {
|
||||||
logger.Error("Failed to store secret: %v", err)
|
logger.Error("Failed to store secret: %v", err)
|
||||||
return errors.InternalServerError("otp.generate", "failed to generate code")
|
return errors.InternalServerError("otp.generate", "failed to generate code")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.Info("generating the code: ", okey.Secret, " ", okey.Expiry)
|
||||||
|
|
||||||
// generate a new code
|
// generate a new code
|
||||||
code, err := totp.GenerateCodeCustom(secret, time.Now(), totp.ValidateOpts{
|
code, err := totp.GenerateCodeCustom(okey.Secret, time.Now(), totp.ValidateOpts{
|
||||||
Period: 60,
|
Period: uint(req.Expiry),
|
||||||
Skew: 1,
|
Skew: 1,
|
||||||
Digits: otp.DigitsSix,
|
Digits: otp.Digits(req.Size),
|
||||||
Algorithm: otp.AlgorithmSHA1,
|
Algorithm: otp.AlgorithmSHA1,
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -56,6 +74,16 @@ func (e *Otp) Generate(ctx context.Context, req *pb.GenerateRequest, rsp *pb.Gen
|
|||||||
return errors.InternalServerError("otp.generate", "failed to generate code: %v", err)
|
return errors.InternalServerError("otp.generate", "failed to generate code: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// we have to replaced the cached value if the expiry is different
|
||||||
|
if v := uint(req.Expiry); v != okey.Expiry {
|
||||||
|
okey.Expiry = v
|
||||||
|
|
||||||
|
if err := cache.Context(ctx).Set("otp:"+req.Id, okey, time.Now().Add(time.Minute*5)); err != nil {
|
||||||
|
logger.Error("Failed to store secret: %v", err)
|
||||||
|
return errors.InternalServerError("otp.generate", "failed to generate code")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// return the code
|
// return the code
|
||||||
rsp.Code = code
|
rsp.Code = code
|
||||||
|
|
||||||
@@ -70,17 +98,18 @@ func (e *Otp) Validate(ctx context.Context, req *pb.ValidateRequest, rsp *pb.Val
|
|||||||
return errors.BadRequest("otp.generate", "missing code")
|
return errors.BadRequest("otp.generate", "missing code")
|
||||||
}
|
}
|
||||||
|
|
||||||
var secret string
|
key := new(otpKey)
|
||||||
|
|
||||||
if err := cache.Context(ctx).Get(req.Id, &secret); err != nil {
|
if err := cache.Context(ctx).Get("otp:"+req.Id, &key); err != nil {
|
||||||
logger.Error("Failed to get secret from store: %v", err)
|
logger.Error("Failed to get secret from store: %v", err)
|
||||||
return errors.InternalServerError("otp.generate", "failed to validate code")
|
return errors.InternalServerError("otp.generate", "failed to validate code")
|
||||||
}
|
}
|
||||||
|
|
||||||
ok, err := totp.ValidateCustom(req.Code, secret, time.Now(), totp.ValidateOpts{
|
logger.Info("validating the code: ", key.Secret, " ", key.Expiry)
|
||||||
Period: 60,
|
ok, err := totp.ValidateCustom(req.Code, key.Secret, time.Now(), totp.ValidateOpts{
|
||||||
|
Period: key.Expiry,
|
||||||
Skew: 1,
|
Skew: 1,
|
||||||
Digits: otp.DigitsSix,
|
Digits: otp.Digits(len(req.Code)),
|
||||||
Algorithm: otp.AlgorithmSHA1,
|
Algorithm: otp.AlgorithmSHA1,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -28,6 +28,10 @@ type GenerateRequest struct {
|
|||||||
|
|
||||||
// unique id, email or user to generate an OTP for
|
// unique id, email or user to generate an OTP for
|
||||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
||||||
|
// number of characters (default: 6)
|
||||||
|
Size int64 `protobuf:"varint,2,opt,name=size,proto3" json:"size,omitempty"`
|
||||||
|
// expiration in seconds (default: 300)
|
||||||
|
Expiry int64 `protobuf:"varint,3,opt,name=expiry,proto3" json:"expiry,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x *GenerateRequest) Reset() {
|
func (x *GenerateRequest) Reset() {
|
||||||
@@ -69,12 +73,26 @@ func (x *GenerateRequest) GetId() string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (x *GenerateRequest) GetSize() int64 {
|
||||||
|
if x != nil {
|
||||||
|
return x.Size
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *GenerateRequest) GetExpiry() int64 {
|
||||||
|
if x != nil {
|
||||||
|
return x.Expiry
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
type GenerateResponse struct {
|
type GenerateResponse struct {
|
||||||
state protoimpl.MessageState
|
state protoimpl.MessageState
|
||||||
sizeCache protoimpl.SizeCache
|
sizeCache protoimpl.SizeCache
|
||||||
unknownFields protoimpl.UnknownFields
|
unknownFields protoimpl.UnknownFields
|
||||||
|
|
||||||
// 6 digit one time pass code
|
// one time pass code
|
||||||
Code string `protobuf:"bytes,1,opt,name=code,proto3" json:"code,omitempty"`
|
Code string `protobuf:"bytes,1,opt,name=code,proto3" json:"code,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,7 +135,7 @@ func (x *GenerateResponse) GetCode() string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate the code
|
// Validate the OTP code
|
||||||
type ValidateRequest struct {
|
type ValidateRequest struct {
|
||||||
state protoimpl.MessageState
|
state protoimpl.MessageState
|
||||||
sizeCache protoimpl.SizeCache
|
sizeCache protoimpl.SizeCache
|
||||||
@@ -227,27 +245,30 @@ var File_proto_otp_proto protoreflect.FileDescriptor
|
|||||||
|
|
||||||
var file_proto_otp_proto_rawDesc = []byte{
|
var file_proto_otp_proto_rawDesc = []byte{
|
||||||
0x0a, 0x0f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x6f, 0x74, 0x70, 0x2e, 0x70, 0x72, 0x6f, 0x74,
|
0x0a, 0x0f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x6f, 0x74, 0x70, 0x2e, 0x70, 0x72, 0x6f, 0x74,
|
||||||
0x6f, 0x12, 0x03, 0x6f, 0x74, 0x70, 0x22, 0x21, 0x0a, 0x0f, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61,
|
0x6f, 0x12, 0x03, 0x6f, 0x74, 0x70, 0x22, 0x4d, 0x0a, 0x0f, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61,
|
||||||
0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18,
|
0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18,
|
||||||
0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x22, 0x26, 0x0a, 0x10, 0x47, 0x65, 0x6e,
|
0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x73, 0x69, 0x7a,
|
||||||
0x65, 0x72, 0x61, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x12, 0x0a,
|
0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x04, 0x73, 0x69, 0x7a, 0x65, 0x12, 0x16, 0x0a,
|
||||||
0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x63, 0x6f, 0x64,
|
0x06, 0x65, 0x78, 0x70, 0x69, 0x72, 0x79, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, 0x65,
|
||||||
0x65, 0x22, 0x35, 0x0a, 0x0f, 0x56, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71,
|
0x78, 0x70, 0x69, 0x72, 0x79, 0x22, 0x26, 0x0a, 0x10, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74,
|
||||||
0x75, 0x65, 0x73, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09,
|
0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64,
|
||||||
0x52, 0x02, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x02, 0x20, 0x01,
|
0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x22, 0x35, 0x0a,
|
||||||
0x28, 0x09, 0x52, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x22, 0x2c, 0x0a, 0x10, 0x56, 0x61, 0x6c, 0x69,
|
0x0f, 0x56, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74,
|
||||||
0x64, 0x61, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x18, 0x0a, 0x07,
|
0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64,
|
||||||
0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x73,
|
0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04,
|
||||||
0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x32, 0x7b, 0x0a, 0x03, 0x4f, 0x74, 0x70, 0x12, 0x39, 0x0a,
|
0x63, 0x6f, 0x64, 0x65, 0x22, 0x2c, 0x0a, 0x10, 0x56, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x65,
|
||||||
0x08, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x12, 0x14, 0x2e, 0x6f, 0x74, 0x70, 0x2e,
|
0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x73, 0x75, 0x63, 0x63,
|
||||||
0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a,
|
0x65, 0x73, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x73, 0x75, 0x63, 0x63, 0x65,
|
||||||
0x15, 0x2e, 0x6f, 0x74, 0x70, 0x2e, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x52, 0x65,
|
0x73, 0x73, 0x32, 0x7b, 0x0a, 0x03, 0x4f, 0x74, 0x70, 0x12, 0x39, 0x0a, 0x08, 0x47, 0x65, 0x6e,
|
||||||
0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x39, 0x0a, 0x08, 0x56, 0x61, 0x6c, 0x69,
|
0x65, 0x72, 0x61, 0x74, 0x65, 0x12, 0x14, 0x2e, 0x6f, 0x74, 0x70, 0x2e, 0x47, 0x65, 0x6e, 0x65,
|
||||||
0x64, 0x61, 0x74, 0x65, 0x12, 0x14, 0x2e, 0x6f, 0x74, 0x70, 0x2e, 0x56, 0x61, 0x6c, 0x69, 0x64,
|
0x72, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x15, 0x2e, 0x6f, 0x74,
|
||||||
0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x15, 0x2e, 0x6f, 0x74, 0x70,
|
0x70, 0x2e, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e,
|
||||||
0x2e, 0x56, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73,
|
0x73, 0x65, 0x22, 0x00, 0x12, 0x39, 0x0a, 0x08, 0x56, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x65,
|
||||||
0x65, 0x22, 0x00, 0x42, 0x0d, 0x5a, 0x0b, 0x2e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3b, 0x6f,
|
0x12, 0x14, 0x2e, 0x6f, 0x74, 0x70, 0x2e, 0x56, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x65, 0x52,
|
||||||
0x74, 0x70, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x15, 0x2e, 0x6f, 0x74, 0x70, 0x2e, 0x56, 0x61, 0x6c,
|
||||||
|
0x69, 0x64, 0x61, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42,
|
||||||
|
0x0d, 0x5a, 0x0b, 0x2e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3b, 0x6f, 0x74, 0x70, 0x62, 0x06,
|
||||||
|
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|||||||
@@ -13,10 +13,14 @@ service Otp {
|
|||||||
message GenerateRequest {
|
message GenerateRequest {
|
||||||
// unique id, email or user to generate an OTP for
|
// unique id, email or user to generate an OTP for
|
||||||
string id = 1;
|
string id = 1;
|
||||||
|
// number of characters (default: 6)
|
||||||
|
int64 size = 2;
|
||||||
|
// expiration in seconds (default: 300)
|
||||||
|
int64 expiry = 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
message GenerateResponse {
|
message GenerateResponse {
|
||||||
// 6 digit one time pass code
|
// one time pass code
|
||||||
string code = 1;
|
string code = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,7 +28,7 @@ message GenerateResponse {
|
|||||||
message ValidateRequest {
|
message ValidateRequest {
|
||||||
// unique id, email or user for which the code was generated
|
// unique id, email or user for which the code was generated
|
||||||
string id = 1;
|
string id = 1;
|
||||||
// 6 digit one time pass code to validate
|
// one time pass code to validate
|
||||||
string code = 2;
|
string code = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
2
pkg/cache/cache.go
vendored
2
pkg/cache/cache.go
vendored
@@ -103,6 +103,7 @@ func (c *cache) Get(key string, val interface{}) error {
|
|||||||
if err != nil && err == store.ErrNotFound {
|
if err != nil && err == store.ErrNotFound {
|
||||||
return ErrNotFound
|
return ErrNotFound
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
if len(recs) == 0 {
|
if len(recs) == 0 {
|
||||||
return ErrNotFound
|
return ErrNotFound
|
||||||
@@ -111,6 +112,7 @@ func (c *cache) Get(key string, val interface{}) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// put it in the cache for future use
|
// put it in the cache for future use
|
||||||
// set in the lru
|
// set in the lru
|
||||||
rec := recs[0]
|
rec := recs[0]
|
||||||
|
|||||||
Reference in New Issue
Block a user