-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from labbsr0x/usercredentials
Usercredentials
- Loading branch information
Showing
28 changed files
with
777 additions
and
272 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,7 @@ var serveCmd = &cobra.Command{ | |
} | ||
} | ||
|
||
return nil | ||
return err | ||
}, | ||
} | ||
|
||
|
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,35 +1,134 @@ | ||
package db | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
"time" | ||
|
||
"github.com/labbsr0x/whisper/misc" | ||
|
||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/google/uuid" | ||
|
||
"github.com/labbsr0x/goh/gohtypes" | ||
|
||
"github.com/jinzhu/gorm" | ||
_ "github.com/jinzhu/gorm/dialects/mysql" | ||
) | ||
|
||
// UserCredential holds the information from a user credential | ||
type UserCredential struct { | ||
ID string `gorm:"primary_key;not null;"` | ||
Username string `gorm:"unique_index;not null;"` | ||
Email string `gorm:"index"` | ||
Password string `gorm:"not null;"` | ||
Salt string `gorm:"not null;"` | ||
CreatedAt time.Time | ||
UpdatedAt time.Time | ||
} | ||
|
||
// UserCredentialsDAO defines the methods that can be performed | ||
type UserCredentialsDAO interface { | ||
CreateUserCredential(username, password, clientID string) (string, error) | ||
UpdateUserCredential(userID uuid.UUID, username, password string) error | ||
DeleteUserCredential(userID uuid.UUID) error | ||
CreateUserCredential(username, password, email string) (string, error) | ||
UpdateUserCredential(username, email, password string) error | ||
GetUserCredential(username string) (UserCredential, error) | ||
InitFromDatabaseURL(dbURL string) UserCredentialsDAO | ||
CheckCredentials(username, password string) (bool, error) | ||
} | ||
|
||
// DefaultUserCredentialsDAO a default UserCredentialsDAO interface implementation | ||
type DefaultUserCredentialsDAO struct { | ||
DatabaseURL string | ||
SecretKey string | ||
} | ||
|
||
// InitFromDatabaseURL initializes a defualt user credentials DAO from web builder | ||
func (dao *DefaultUserCredentialsDAO) InitFromDatabaseURL(dbURL string) UserCredentialsDAO { | ||
u, err := url.Parse(dbURL) | ||
gohtypes.PanicIfError("Unable to parse db url", 500, err) | ||
dao.DatabaseURL = strings.Replace(u.String(), u.Scheme+"://", "", 1) | ||
|
||
gohtypes.PanicIfError("Not possible to migrate db", 500, dao.migrate()) | ||
|
||
dao.SecretKey = "y6VaBTeP5ROoUcPPAThW" | ||
return dao | ||
} | ||
|
||
// migrate initializes a migration routine to synchronize db and model | ||
func (dao *DefaultUserCredentialsDAO) migrate() error { | ||
db, err := gorm.Open("mysql", dao.DatabaseURL) | ||
if err == nil { | ||
defer db.Close() | ||
db.AutoMigrate(&UserCredential{}) | ||
} | ||
logrus.Error(err) | ||
return err | ||
} | ||
|
||
// CreateUserCredential creates a user | ||
func (dao *DefaultUserCredentialsDAO) CreateUserCredential(username, password, clientID string) (string, error) { | ||
return "", nil | ||
func (dao *DefaultUserCredentialsDAO) CreateUserCredential(username, password, email string) (string, error) { | ||
db, err := gorm.Open("mysql", dao.DatabaseURL) | ||
if err == nil { | ||
defer db.Close() | ||
salt := misc.GenerateSalt() | ||
hPassword := misc.GetEncryptedPassword(dao.SecretKey, password, salt) | ||
userCredential := UserCredential{ID: uuid.New().String(), Username: username, Password: hPassword, Email: email, Salt: salt} | ||
db.NewRecord(userCredential) | ||
|
||
db.Create(&userCredential) | ||
|
||
if !db.NewRecord(userCredential) { | ||
return userCredential.ID, nil | ||
} | ||
|
||
err = fmt.Errorf("Unable to create an user credential: %v", db.GetErrors()) | ||
} | ||
return "", err | ||
} | ||
|
||
// UpdateUserCredential updates a user | ||
func (dao *DefaultUserCredentialsDAO) UpdateUserCredential(userID uuid.UUID, username, password string) error { | ||
return nil | ||
func (dao *DefaultUserCredentialsDAO) UpdateUserCredential(username, email, password string) error { | ||
db, err := gorm.Open("mysql", dao.DatabaseURL) | ||
if err == nil { | ||
defer db.Close() | ||
|
||
salt := misc.GenerateSalt() | ||
hPassword := misc.GetEncryptedPassword(dao.SecretKey, password, salt) | ||
|
||
userCredential := UserCredential{} | ||
db.Where("username = ?", username).First(&userCredential) | ||
|
||
userCredential.Password = hPassword | ||
userCredential.Salt = salt | ||
userCredential.Email = email | ||
|
||
db = db.Save(userCredential) | ||
err = db.Error | ||
} | ||
return err | ||
} | ||
|
||
// GetUserCredential gets an user credential | ||
func (dao *DefaultUserCredentialsDAO) GetUserCredential(username string) (UserCredential, error) { | ||
userCredential := UserCredential{} | ||
db, err := gorm.Open("mysql", dao.DatabaseURL) | ||
if err == nil { | ||
defer db.Close() | ||
|
||
db = db.Where("username = ?", username).First(&userCredential) | ||
err = db.Error | ||
} | ||
return userCredential, err | ||
} | ||
|
||
// DeleteUserCredential deletes the user | ||
func (dao *DefaultUserCredentialsDAO) DeleteUserCredential(userID uuid.UUID) error { | ||
return nil | ||
// CheckCredentials verifies if the informed credentials are valid | ||
func (dao *DefaultUserCredentialsDAO) CheckCredentials(username, password string) (bool, error) { | ||
userCredential, err := dao.GetUserCredential(username) | ||
if err == nil { | ||
hPassword := misc.GetEncryptedPassword(dao.SecretKey, password, userCredential.Salt) | ||
return hPassword == userCredential.Password, nil | ||
} | ||
return false, err | ||
} |
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,15 @@ | ||
#!/bin/bash | ||
|
||
# Functions | ||
ok() { echo -e '\e[32m'$1'\e[m'; } # Green | ||
|
||
MYSQL=`which mysql` | ||
|
||
Q1="CREATE DATABASE IF NOT EXISTS hydra;" | ||
Q2="CREATE DATABASE IF NOT EXISTS whisper;" | ||
|
||
SQL="${Q1}${Q2}" | ||
|
||
$MYSQL -uroot -p$MYSQL_ROOT_PASSWORD -e "${SQL}" | ||
|
||
ok "Created databases hydra and whisper" |
Oops, something went wrong.