* encrypted userid
* added totp_verified column in user table
* started test for totp
This commit is contained in:
lemonScaletech
2023-09-06 18:49:54 +05:30
parent bbb1cf6301
commit a3fa0eb6cd
13 changed files with 157 additions and 289 deletions

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}