-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create user useCase with password encryption
- Loading branch information
Showing
14 changed files
with
238 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package createUser | ||
|
||
const ( | ||
DefaultRoleID = 1 | ||
USER_DEFAULT_ROLE_ID = 1 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,21 @@ | ||
package createUser | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/quinntas/go-rest-template/internal/api/web" | ||
) | ||
|
||
type DTO struct { | ||
Email string `json:"email"` | ||
Password string `json:"password"` | ||
} | ||
|
||
func NewDTO(ctx context.Context) DTO { | ||
json := ctx.Value(web.JSON_CTX_KEY).(map[string]interface{}) | ||
|
||
return DTO{ | ||
Email: json["email"].(string), | ||
Password: json["password"].(string), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package encryption | ||
|
||
import ( | ||
"crypto/rand" | ||
"crypto/subtle" | ||
"encoding/hex" | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
"golang.org/x/crypto/pbkdf2" | ||
"golang.org/x/crypto/sha3" | ||
) | ||
|
||
type HashSalt struct { | ||
Result string | ||
} | ||
|
||
func GenerateSalt(length uint32) []byte { | ||
secret := make([]byte, length) | ||
_, _ = rand.Read(secret) | ||
return secret | ||
} | ||
|
||
type CryptoParams struct { | ||
value string | ||
Salt []byte | ||
Pepper string | ||
Iterations int | ||
Length int | ||
} | ||
|
||
func ParseEncryption(value string, hash string, pepper string) (*CryptoParams, error) { | ||
params := &CryptoParams{} | ||
|
||
hashParams := strings.Split(hash, "$") | ||
|
||
salt, err := hex.DecodeString(hashParams[1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
iterations, err := strconv.Atoi(hashParams[2]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
length, err := strconv.Atoi(hashParams[3]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
params.Salt = salt | ||
params.Iterations = iterations | ||
params.Length = length | ||
params.value = value | ||
params.Pepper = pepper | ||
|
||
return params, nil | ||
} | ||
|
||
func ConstantTimeStringCompare(a, b string) bool { | ||
return subtle.ConstantTimeCompare([]byte(a), []byte(b)) == 1 | ||
} | ||
|
||
func CompareEncryption(value string, hash string, pepper string) (bool, error) { | ||
hashParams, err := ParseEncryption(value, hash, pepper) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
newHash, err := GenerateEncryptionWithParams(hashParams) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return ConstantTimeStringCompare(hash, newHash), nil | ||
} | ||
|
||
func GenerateDefaultEncryption(value string, pepper string) (string, error) { | ||
return GenerateEncryptionWithParams(&CryptoParams{ | ||
value: value, | ||
Salt: GenerateSalt(32), | ||
Pepper: pepper, | ||
Iterations: 10000, | ||
Length: 32, | ||
}) | ||
} | ||
|
||
func GenerateEncryptionWithParams(cryptoParams *CryptoParams) (string, error) { | ||
hash := pbkdf2.Key([]byte(cryptoParams.Pepper+cryptoParams.value), cryptoParams.Salt, cryptoParams.Iterations, cryptoParams.Length, sha3.New256) | ||
|
||
hashHex := hex.EncodeToString(hash) | ||
saltHex := hex.EncodeToString(cryptoParams.Salt) | ||
|
||
result := fmt.Sprintf("sha256$%s$%d$%d$%s", saltHex, cryptoParams.Iterations, cryptoParams.Length, hashHex) | ||
|
||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters