Skip to content

Commit

Permalink
feat email verified
Browse files Browse the repository at this point in the history
  • Loading branch information
febrihidayan committed Jan 14, 2024
1 parent a113222 commit 5a09036
Show file tree
Hide file tree
Showing 37 changed files with 762 additions and 179 deletions.
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ services:
MONGODB_PORT: '27017'
JWT_TOKEN_JTI: mnb23vcsrt756yuiomnbvcx98ertyuiop
JWT_EXPIRED: 60
APP_URL: "http://domain.com"
APP_SECRET_KEY: "key for AES-256"
ports:
- '8083:8083'
- '9083:9083'
Expand Down
2 changes: 2 additions & 0 deletions env/auth/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ RPC_PORT=":9083"
RPC_USER=":9084"
RPC_NOTIFICATION=":9086"
APP_TIMEOUT=60
APP_URL="http://domain.com"
APP_SECRET_KEY="key for AES-256"

MONGODB_USERNAME="root"
MONGODB_PASSWORD="root"
Expand Down
36 changes: 36 additions & 0 deletions krakend/krakend.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,42 @@
}
]
},
{
"endpoint": "/v1/auth/email/verified",
"method": "POST",
"output_encoding": "no-op",
"backend": [
{
"url_pattern": "/v1/auth/email/verified",
"host": [
"http://auth-go:8083"
],
"extra_config": {
"backend/http": {
"return_error_code": true
}
}
}
]
},
{
"endpoint": "/v1/auth/email/{token}",
"method": "GET",
"output_encoding": "no-op",
"backend": [
{
"url_pattern": "/v1/auth/email/{token}",
"host": [
"http://auth-go:8083"
],
"extra_config": {
"backend/http": {
"return_error_code": true
}
}
}
]
},
{
"endpoint": "/v1/auth/roles",
"method": "GET",
Expand Down
5 changes: 5 additions & 0 deletions pkg/lang/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ var (
ErrPermissionAlready = errors.New("The name permission is already.")
ErrUnsupportFile = errors.New("unsupport file type.")
ErrTemplateAlready = errors.New("The name template is already.")
EmailAddressVerified = errors.New("Your email address is verified.")
EmailAddressUnverified = errors.New("Your email address is unverified.")
TokenNotValid = errors.New("The token is not valid.")
TokenHasExpired = errors.New("The token has expired.")
UserNotFound = errors.New("User not found.")

Locales = map[string]string{
"filled": "The :attribute field is required.",
Expand Down
77 changes: 77 additions & 0 deletions pkg/utils/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ package utils

import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"html/template"
"io"
"net/http"
"os"
"path"
Expand Down Expand Up @@ -112,3 +117,75 @@ func ParseTemplate(templateFileName string, data interface{}) (string, error) {

return buf.String(), nil
}

// encrypt data using aes
// https://www.melvinvivas.com/how-to-encrypt-and-decrypt-data-using-aes
func ChiperEncrypt(stringToEncrypt string, keyString string) (string, error) {

//Since the key is in string, we need to convert decode it to bytes
key, _ := hex.DecodeString(keyString)
plaintext := []byte(stringToEncrypt)

//Create a new Cipher Block from the key
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}

//Create a new GCM - https://en.wikipedia.org/wiki/Galois/Counter_Mode
//https://golang.org/pkg/crypto/cipher/#NewGCM
aesGCM, err := cipher.NewGCM(block)
if err != nil {
return "", err
}

//Create a nonce. Nonce should be from GCM
nonce := make([]byte, aesGCM.NonceSize())
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
return "", err
}

//Encrypt the data using aesGCM.Seal
//Since we don't want to save the nonce somewhere else in this case, we add it as a prefix to the encrypted data. The first nonce argument in Seal is the prefix.
ciphertext := aesGCM.Seal(nonce, nonce, plaintext, nil)
return fmt.Sprintf("%x", ciphertext), nil
}

// decrypt data using aes
// https://www.melvinvivas.com/how-to-encrypt-and-decrypt-data-using-aes
func ChiperDecrypt(encryptedString string, keyString string) (string, error) {

key, _ := hex.DecodeString(keyString)
enc, _ := hex.DecodeString(encryptedString)

//Create a new Cipher Block from the key
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}

//Create a new GCM
aesGCM, err := cipher.NewGCM(block)
if err != nil {
return "", err
}

//Get the nonce size
nonceSize := aesGCM.NonceSize()

//Extract the nonce from the encrypted data
nonce, ciphertext := enc[:nonceSize], enc[nonceSize:]

//Decrypt the data
plaintext, err := aesGCM.Open(nil, nonce, ciphertext, nil)
if err != nil {
return "", err
}

return fmt.Sprintf("%s", plaintext), nil
}

func TimestampToTime(s string) time.Time {
i, _ := strconv.ParseInt(s, 10, 64)
return time.Unix(i, 0)
}
Loading

0 comments on commit 5a09036

Please sign in to comment.