-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
42 lines (33 loc) · 1.05 KB
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package model
import (
"time"
"github.com/google/uuid"
"golang.org/x/crypto/bcrypt"
)
// AuthData is model for authentication data
type AuthData struct {
SourceID uuid.UUID `db:"source_id"`
SourceType AuthDataSourceType `db:"source_type"`
Password string `db:"password"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
// DbTable returns DB table name
func (AuthData) DbTable() string {
return "auth_data"
}
// AuthDataSourceType is source type for authentication
type AuthDataSourceType string
// AuthDataSourceType values
const (
AuthDataSourceTypeUser = AuthDataSourceType("user") // User
AuthDataSourceTypeNutritionist = AuthDataSourceType("nutritionist") // Nutritionist
)
func HashPassword(password string) (string, error) {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
return string(bytes), err
}
func CheckPasswordHash(hash, password string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}