This is a combination of 4 commits.
Fix the image
This commit is contained in:
		
							
								
								
									
										22
									
								
								internal/helpers/email/email.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								internal/helpers/email/email.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,22 @@
 | 
			
		||||
package email
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"net/smtp"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type EmailConf struct {
 | 
			
		||||
	From string
 | 
			
		||||
	Password string
 | 
			
		||||
	SmtpHost string
 | 
			
		||||
	SmtpPort string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (e *EmailConf) SendEmail (to string, message string )error  {
 | 
			
		||||
	messageByte := []byte(message)
 | 
			
		||||
	auth := smtp.PlainAuth("", e.From, e.Password, e.SmtpHost)
 | 
			
		||||
 | 
			
		||||
	if err := smtp.SendMail(e.SmtpHost+":"+e.SmtpPort, auth, e.From, []string{to}, messageByte); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
} 
 | 
			
		||||
							
								
								
									
										14
									
								
								internal/helpers/hash/hash.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								internal/helpers/hash/hash.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,14 @@
 | 
			
		||||
package hash
 | 
			
		||||
 | 
			
		||||
import "golang.org/x/crypto/bcrypt"
 | 
			
		||||
 | 
			
		||||
func HashPassword(password string, cost int) (string, error) {
 | 
			
		||||
	bytes, err := bcrypt.GenerateFromPassword([]byte(password), cost)
 | 
			
		||||
	return string(bytes), err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func CheckPasswordHash(password, hash string) bool {
 | 
			
		||||
    err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
 | 
			
		||||
    return err == nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										21
									
								
								internal/helpers/hash/hash_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								internal/helpers/hash/hash_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
			
		||||
package hash_test
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/hash"
 | 
			
		||||
	"github.com/alecthomas/assert/v2"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestHashValid(t *testing.T) {
 | 
			
		||||
    password := "qwertyu9"
 | 
			
		||||
    hpass, err := hash.HashPassword(password, 10)
 | 
			
		||||
    assert.NoError(t, err)
 | 
			
		||||
    assert.True(t, hash.CheckPasswordHash(password, hpass))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestHashInvalid(t *testing.T) {
 | 
			
		||||
    password := "qwertyu9"
 | 
			
		||||
    invhash := "qwertyu9"
 | 
			
		||||
    assert.False(t, hash.CheckPasswordHash(password, invhash))
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user