feat:
* encrypted userid * added totp_verified column in user table * started test for totp
This commit is contained in:
@@ -3,13 +3,8 @@ package arangodb
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"image/png"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/pquerna/otp/totp"
|
||||
@@ -62,44 +57,14 @@ func (p *provider) ValidatePasscode(ctx context.Context, passcode string, id str
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("error while getting user details")
|
||||
}
|
||||
|
||||
// validate passcode inputted by user
|
||||
for {
|
||||
status := totp.Validate(passcode, *user.TotpSecret)
|
||||
status := totp.Validate(passcode, *user.TotpSecret)
|
||||
if !user.TotpVerified {
|
||||
if status {
|
||||
user.TotpVerified = true
|
||||
p.UpdateUser(ctx, user)
|
||||
return status, nil
|
||||
}
|
||||
return status, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (p *provider) GenerateKeysTOTP() (*rsa.PublicKey, error) {
|
||||
key := os.Getenv("TOTP_PRIVATE_KEY")
|
||||
var privateKey *rsa.PrivateKey
|
||||
if key == "" {
|
||||
privateKey, err := rsa.GenerateKey(rand.Reader, 1024)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
privateKeyPEM := encodePrivateKeyToPEM(privateKey)
|
||||
os.Setenv("TOTP_PRIVATE_KEY", string(privateKeyPEM))
|
||||
}
|
||||
publicKey := privateKey.PublicKey
|
||||
return &publicKey, nil
|
||||
}
|
||||
|
||||
func encodePrivateKeyToPEM(privateKey *rsa.PrivateKey) []byte {
|
||||
// Marshal the private key to DER format.
|
||||
privateKeyBytes := x509.MarshalPKCS1PrivateKey(privateKey)
|
||||
|
||||
// Create a PEM block for the private key.
|
||||
privateKeyPEMBlock := &pem.Block{
|
||||
Type: "RSA PRIVATE KEY",
|
||||
Bytes: privateKeyBytes,
|
||||
}
|
||||
|
||||
// Encode the PEM block to PEM format.
|
||||
privateKeyPEM := pem.EncodeToMemory(privateKeyPEMBlock)
|
||||
|
||||
return privateKeyPEM
|
||||
return status, nil
|
||||
}
|
||||
|
@@ -3,13 +3,8 @@ package cassandradb
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"image/png"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/pquerna/otp/totp"
|
||||
@@ -62,44 +57,14 @@ func (p *provider) ValidatePasscode(ctx context.Context, passcode string, id str
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("error while getting user details")
|
||||
}
|
||||
|
||||
// validate passcode inputted by user
|
||||
for {
|
||||
status := totp.Validate(passcode, *user.TotpSecret)
|
||||
status := totp.Validate(passcode, *user.TotpSecret)
|
||||
if !user.TotpVerified {
|
||||
if status {
|
||||
user.TotpVerified = true
|
||||
p.UpdateUser(ctx, user)
|
||||
return status, nil
|
||||
}
|
||||
return status, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (p *provider) GenerateKeysTOTP() (*rsa.PublicKey, error) {
|
||||
key := os.Getenv("TOTP_PRIVATE_KEY")
|
||||
var privateKey *rsa.PrivateKey
|
||||
if key == "" {
|
||||
privateKey, err := rsa.GenerateKey(rand.Reader, 1024)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
privateKeyPEM := encodePrivateKeyToPEM(privateKey)
|
||||
os.Setenv("TOTP_PRIVATE_KEY", string(privateKeyPEM))
|
||||
}
|
||||
publicKey := privateKey.PublicKey
|
||||
return &publicKey, nil
|
||||
}
|
||||
|
||||
func encodePrivateKeyToPEM(privateKey *rsa.PrivateKey) []byte {
|
||||
// Marshal the private key to DER format.
|
||||
privateKeyBytes := x509.MarshalPKCS1PrivateKey(privateKey)
|
||||
|
||||
// Create a PEM block for the private key.
|
||||
privateKeyPEMBlock := &pem.Block{
|
||||
Type: "RSA PRIVATE KEY",
|
||||
Bytes: privateKeyBytes,
|
||||
}
|
||||
|
||||
// Encode the PEM block to PEM format.
|
||||
privateKeyPEM := pem.EncodeToMemory(privateKeyPEMBlock)
|
||||
|
||||
return privateKeyPEM
|
||||
return status, nil
|
||||
}
|
||||
|
@@ -3,13 +3,8 @@ package couchbase
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"image/png"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/pquerna/otp/totp"
|
||||
@@ -62,44 +57,14 @@ func (p *provider) ValidatePasscode(ctx context.Context, passcode string, id str
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("error while getting user details")
|
||||
}
|
||||
|
||||
// validate passcode inputted by user
|
||||
for {
|
||||
status := totp.Validate(passcode, *user.TotpSecret)
|
||||
status := totp.Validate(passcode, *user.TotpSecret)
|
||||
if !user.TotpVerified {
|
||||
if status {
|
||||
user.TotpVerified = true
|
||||
p.UpdateUser(ctx, user)
|
||||
return status, nil
|
||||
}
|
||||
return status, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (p *provider) GenerateKeysTOTP() (*rsa.PublicKey, error) {
|
||||
key := os.Getenv("TOTP_PRIVATE_KEY")
|
||||
var privateKey *rsa.PrivateKey
|
||||
if key == "" {
|
||||
privateKey, err := rsa.GenerateKey(rand.Reader, 1024)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
privateKeyPEM := encodePrivateKeyToPEM(privateKey)
|
||||
os.Setenv("TOTP_PRIVATE_KEY", string(privateKeyPEM))
|
||||
}
|
||||
publicKey := privateKey.PublicKey
|
||||
return &publicKey, nil
|
||||
}
|
||||
|
||||
func encodePrivateKeyToPEM(privateKey *rsa.PrivateKey) []byte {
|
||||
// Marshal the private key to DER format.
|
||||
privateKeyBytes := x509.MarshalPKCS1PrivateKey(privateKey)
|
||||
|
||||
// Create a PEM block for the private key.
|
||||
privateKeyPEMBlock := &pem.Block{
|
||||
Type: "RSA PRIVATE KEY",
|
||||
Bytes: privateKeyBytes,
|
||||
}
|
||||
|
||||
// Encode the PEM block to PEM format.
|
||||
privateKeyPEM := pem.EncodeToMemory(privateKeyPEMBlock)
|
||||
|
||||
return privateKeyPEM
|
||||
return status, nil
|
||||
}
|
||||
|
@@ -3,13 +3,8 @@ package dynamodb
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"image/png"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/pquerna/otp/totp"
|
||||
@@ -62,44 +57,14 @@ func (p *provider) ValidatePasscode(ctx context.Context, passcode string, id str
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("error while getting user details")
|
||||
}
|
||||
|
||||
// validate passcode inputted by user
|
||||
for {
|
||||
status := totp.Validate(passcode, *user.TotpSecret)
|
||||
status := totp.Validate(passcode, *user.TotpSecret)
|
||||
if !user.TotpVerified {
|
||||
if status {
|
||||
user.TotpVerified = true
|
||||
p.UpdateUser(ctx, user)
|
||||
return status, nil
|
||||
}
|
||||
return status, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (p *provider) GenerateKeysTOTP() (*rsa.PublicKey, error) {
|
||||
key := os.Getenv("TOTP_PRIVATE_KEY")
|
||||
var privateKey *rsa.PrivateKey
|
||||
if key == "" {
|
||||
privateKey, err := rsa.GenerateKey(rand.Reader, 1024)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
privateKeyPEM := encodePrivateKeyToPEM(privateKey)
|
||||
os.Setenv("TOTP_PRIVATE_KEY", string(privateKeyPEM))
|
||||
}
|
||||
publicKey := privateKey.PublicKey
|
||||
return &publicKey, nil
|
||||
}
|
||||
|
||||
func encodePrivateKeyToPEM(privateKey *rsa.PrivateKey) []byte {
|
||||
// Marshal the private key to DER format.
|
||||
privateKeyBytes := x509.MarshalPKCS1PrivateKey(privateKey)
|
||||
|
||||
// Create a PEM block for the private key.
|
||||
privateKeyPEMBlock := &pem.Block{
|
||||
Type: "RSA PRIVATE KEY",
|
||||
Bytes: privateKeyBytes,
|
||||
}
|
||||
|
||||
// Encode the PEM block to PEM format.
|
||||
privateKeyPEM := pem.EncodeToMemory(privateKeyPEMBlock)
|
||||
|
||||
return privateKeyPEM
|
||||
return status, nil
|
||||
}
|
||||
|
@@ -3,13 +3,8 @@ package mongodb
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"image/png"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/pquerna/otp/totp"
|
||||
@@ -62,44 +57,14 @@ func (p *provider) ValidatePasscode(ctx context.Context, passcode string, id str
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("error while getting user details")
|
||||
}
|
||||
|
||||
// validate passcode inputted by user
|
||||
for {
|
||||
status := totp.Validate(passcode, *user.TotpSecret)
|
||||
status := totp.Validate(passcode, *user.TotpSecret)
|
||||
if !user.TotpVerified {
|
||||
if status {
|
||||
user.TotpVerified = true
|
||||
p.UpdateUser(ctx, user)
|
||||
return status, nil
|
||||
}
|
||||
return status, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (p *provider) GenerateKeysTOTP() (*rsa.PublicKey, error) {
|
||||
key := os.Getenv("TOTP_PRIVATE_KEY")
|
||||
var privateKey *rsa.PrivateKey
|
||||
if key == "" {
|
||||
privateKey, err := rsa.GenerateKey(rand.Reader, 1024)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
privateKeyPEM := encodePrivateKeyToPEM(privateKey)
|
||||
os.Setenv("TOTP_PRIVATE_KEY", string(privateKeyPEM))
|
||||
}
|
||||
publicKey := privateKey.PublicKey
|
||||
return &publicKey, nil
|
||||
}
|
||||
|
||||
func encodePrivateKeyToPEM(privateKey *rsa.PrivateKey) []byte {
|
||||
// Marshal the private key to DER format.
|
||||
privateKeyBytes := x509.MarshalPKCS1PrivateKey(privateKey)
|
||||
|
||||
// Create a PEM block for the private key.
|
||||
privateKeyPEMBlock := &pem.Block{
|
||||
Type: "RSA PRIVATE KEY",
|
||||
Bytes: privateKeyBytes,
|
||||
}
|
||||
|
||||
// Encode the PEM block to PEM format.
|
||||
privateKeyPEM := pem.EncodeToMemory(privateKeyPEMBlock)
|
||||
|
||||
return privateKeyPEM
|
||||
return status, nil
|
||||
}
|
||||
|
@@ -3,13 +3,8 @@ package provider_template
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"image/png"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/pquerna/otp/totp"
|
||||
@@ -62,44 +57,14 @@ func (p *provider) ValidatePasscode(ctx context.Context, passcode string, id str
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("error while getting user details")
|
||||
}
|
||||
|
||||
// validate passcode inputted by user
|
||||
for {
|
||||
status := totp.Validate(passcode, *user.TotpSecret)
|
||||
status := totp.Validate(passcode, *user.TotpSecret)
|
||||
if !user.TotpVerified {
|
||||
if status {
|
||||
user.TotpVerified = true
|
||||
p.UpdateUser(ctx, user)
|
||||
return status, nil
|
||||
}
|
||||
return status, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (p *provider) GenerateKeysTOTP() (*rsa.PublicKey, error) {
|
||||
key := os.Getenv("TOTP_PRIVATE_KEY")
|
||||
var privateKey *rsa.PrivateKey
|
||||
if key == "" {
|
||||
privateKey, err := rsa.GenerateKey(rand.Reader, 1024)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
privateKeyPEM := encodePrivateKeyToPEM(privateKey)
|
||||
os.Setenv("TOTP_PRIVATE_KEY", string(privateKeyPEM))
|
||||
}
|
||||
publicKey := privateKey.PublicKey
|
||||
return &publicKey, nil
|
||||
}
|
||||
|
||||
func encodePrivateKeyToPEM(privateKey *rsa.PrivateKey) []byte {
|
||||
// Marshal the private key to DER format.
|
||||
privateKeyBytes := x509.MarshalPKCS1PrivateKey(privateKey)
|
||||
|
||||
// Create a PEM block for the private key.
|
||||
privateKeyPEMBlock := &pem.Block{
|
||||
Type: "RSA PRIVATE KEY",
|
||||
Bytes: privateKeyBytes,
|
||||
}
|
||||
|
||||
// Encode the PEM block to PEM format.
|
||||
privateKeyPEM := pem.EncodeToMemory(privateKeyPEMBlock)
|
||||
|
||||
return privateKeyPEM
|
||||
return status, nil
|
||||
}
|
||||
|
@@ -3,8 +3,6 @@ package sql
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"fmt"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"image/png"
|
||||
@@ -60,18 +58,14 @@ func (p *provider) ValidatePasscode(ctx context.Context, passcode string, id str
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("error while getting user details")
|
||||
}
|
||||
|
||||
// validate passcode inputted by user
|
||||
|
||||
status := totp.Validate(passcode, *user.TotpSecret)
|
||||
if !user.TotpVerified {
|
||||
if status {
|
||||
user.TotpVerified = true
|
||||
p.UpdateUser(ctx, user)
|
||||
return status, nil
|
||||
}
|
||||
return status, nil
|
||||
}
|
||||
return status, nil
|
||||
}
|
||||
|
||||
func (p *provider) GenerateKeysTOTP() (*rsa.PublicKey, error) {
|
||||
privateKey, err := rsa.GenerateKey(rand.Reader, 1024)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
publicKey := privateKey.PublicKey
|
||||
return &publicKey, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user