mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-11 19:04:35 +00:00
add expiry/size to otp
This commit is contained in:
@@ -15,20 +15,33 @@ import (
|
||||
|
||||
type Otp struct{}
|
||||
|
||||
type otpKey struct {
|
||||
Secret string
|
||||
Expiry uint
|
||||
}
|
||||
|
||||
func (e *Otp) Generate(ctx context.Context, req *pb.GenerateRequest, rsp *pb.GenerateResponse) error {
|
||||
if len(req.Id) == 0 {
|
||||
return errors.BadRequest("otp.generate", "missing id")
|
||||
}
|
||||
|
||||
// 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
|
||||
key, err := totp.Generate(totp.GenerateOpts{
|
||||
Issuer: "Micro",
|
||||
AccountName: req.Id,
|
||||
Period: 60,
|
||||
Period: 300,
|
||||
Algorithm: otp.AlgorithmSHA1,
|
||||
})
|
||||
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")
|
||||
}
|
||||
|
||||
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)
|
||||
return errors.InternalServerError("otp.generate", "failed to generate code")
|
||||
}
|
||||
}
|
||||
|
||||
logger.Info("generating the code: ", okey.Secret, " ", okey.Expiry)
|
||||
|
||||
// generate a new code
|
||||
code, err := totp.GenerateCodeCustom(secret, time.Now(), totp.ValidateOpts{
|
||||
Period: 60,
|
||||
code, err := totp.GenerateCodeCustom(okey.Secret, time.Now(), totp.ValidateOpts{
|
||||
Period: uint(req.Expiry),
|
||||
Skew: 1,
|
||||
Digits: otp.DigitsSix,
|
||||
Digits: otp.Digits(req.Size),
|
||||
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)
|
||||
}
|
||||
|
||||
// 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
|
||||
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")
|
||||
}
|
||||
|
||||
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)
|
||||
return errors.InternalServerError("otp.generate", "failed to validate code")
|
||||
}
|
||||
|
||||
ok, err := totp.ValidateCustom(req.Code, secret, time.Now(), totp.ValidateOpts{
|
||||
Period: 60,
|
||||
logger.Info("validating the code: ", key.Secret, " ", key.Expiry)
|
||||
ok, err := totp.ValidateCustom(req.Code, key.Secret, time.Now(), totp.ValidateOpts{
|
||||
Period: key.Expiry,
|
||||
Skew: 1,
|
||||
Digits: otp.DigitsSix,
|
||||
Digits: otp.Digits(len(req.Code)),
|
||||
Algorithm: otp.AlgorithmSHA1,
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -28,6 +28,10 @@ type GenerateRequest struct {
|
||||
|
||||
// unique id, email or user to generate an OTP for
|
||||
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() {
|
||||
@@ -69,12 +73,26 @@ func (x *GenerateRequest) GetId() string {
|
||||
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 {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
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"`
|
||||
}
|
||||
|
||||
@@ -117,7 +135,7 @@ func (x *GenerateResponse) GetCode() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Validate the code
|
||||
// Validate the OTP code
|
||||
type ValidateRequest struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
@@ -227,27 +245,30 @@ var File_proto_otp_proto protoreflect.FileDescriptor
|
||||
|
||||
var file_proto_otp_proto_rawDesc = []byte{
|
||||
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,
|
||||
0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x22, 0x26, 0x0a, 0x10, 0x47, 0x65, 0x6e,
|
||||
0x65, 0x72, 0x61, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x12, 0x0a,
|
||||
0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x63, 0x6f, 0x64,
|
||||
0x65, 0x22, 0x35, 0x0a, 0x0f, 0x56, 0x61, 0x6c, 0x69, 0x64, 0x61, 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, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x02, 0x20, 0x01,
|
||||
0x28, 0x09, 0x52, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x22, 0x2c, 0x0a, 0x10, 0x56, 0x61, 0x6c, 0x69,
|
||||
0x64, 0x61, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x18, 0x0a, 0x07,
|
||||
0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x73,
|
||||
0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x32, 0x7b, 0x0a, 0x03, 0x4f, 0x74, 0x70, 0x12, 0x39, 0x0a,
|
||||
0x08, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x12, 0x14, 0x2e, 0x6f, 0x74, 0x70, 0x2e,
|
||||
0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a,
|
||||
0x15, 0x2e, 0x6f, 0x74, 0x70, 0x2e, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x52, 0x65,
|
||||
0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x39, 0x0a, 0x08, 0x56, 0x61, 0x6c, 0x69,
|
||||
0x64, 0x61, 0x74, 0x65, 0x12, 0x14, 0x2e, 0x6f, 0x74, 0x70, 0x2e, 0x56, 0x61, 0x6c, 0x69, 0x64,
|
||||
0x61, 0x74, 0x65, 0x52, 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,
|
||||
0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x73, 0x69, 0x7a,
|
||||
0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x04, 0x73, 0x69, 0x7a, 0x65, 0x12, 0x16, 0x0a,
|
||||
0x06, 0x65, 0x78, 0x70, 0x69, 0x72, 0x79, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, 0x65,
|
||||
0x78, 0x70, 0x69, 0x72, 0x79, 0x22, 0x26, 0x0a, 0x10, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74,
|
||||
0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64,
|
||||
0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x22, 0x35, 0x0a,
|
||||
0x0f, 0x56, 0x61, 0x6c, 0x69, 0x64, 0x61, 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,
|
||||
0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04,
|
||||
0x63, 0x6f, 0x64, 0x65, 0x22, 0x2c, 0x0a, 0x10, 0x56, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x65,
|
||||
0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x73, 0x75, 0x63, 0x63,
|
||||
0x65, 0x73, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x73, 0x75, 0x63, 0x63, 0x65,
|
||||
0x73, 0x73, 0x32, 0x7b, 0x0a, 0x03, 0x4f, 0x74, 0x70, 0x12, 0x39, 0x0a, 0x08, 0x47, 0x65, 0x6e,
|
||||
0x65, 0x72, 0x61, 0x74, 0x65, 0x12, 0x14, 0x2e, 0x6f, 0x74, 0x70, 0x2e, 0x47, 0x65, 0x6e, 0x65,
|
||||
0x72, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x15, 0x2e, 0x6f, 0x74,
|
||||
0x70, 0x2e, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e,
|
||||
0x73, 0x65, 0x22, 0x00, 0x12, 0x39, 0x0a, 0x08, 0x56, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x65,
|
||||
0x12, 0x14, 0x2e, 0x6f, 0x74, 0x70, 0x2e, 0x56, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x65, 0x52,
|
||||
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 (
|
||||
|
||||
@@ -13,10 +13,14 @@ service Otp {
|
||||
message GenerateRequest {
|
||||
// unique id, email or user to generate an OTP for
|
||||
string id = 1;
|
||||
// number of characters (default: 6)
|
||||
int64 size = 2;
|
||||
// expiration in seconds (default: 300)
|
||||
int64 expiry = 3;
|
||||
}
|
||||
|
||||
message GenerateResponse {
|
||||
// 6 digit one time pass code
|
||||
// one time pass code
|
||||
string code = 1;
|
||||
}
|
||||
|
||||
@@ -24,7 +28,7 @@ message GenerateResponse {
|
||||
message ValidateRequest {
|
||||
// unique id, email or user for which the code was generated
|
||||
string id = 1;
|
||||
// 6 digit one time pass code to validate
|
||||
// one time pass code to validate
|
||||
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 {
|
||||
return ErrNotFound
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(recs) == 0 {
|
||||
return ErrNotFound
|
||||
@@ -111,6 +112,7 @@ func (c *cache) Get(key string, val interface{}) error {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
// put it in the cache for future use
|
||||
// set in the lru
|
||||
rec := recs[0]
|
||||
|
||||
Reference in New Issue
Block a user