-
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.
- Loading branch information
Showing
10 changed files
with
296 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
JWT_PRIVATE_KEY= | ||
JWT_ISSUER= | ||
JWT_ISSUER= | ||
POSTGRES_ADDRESS= | ||
POSTGRES_DB= | ||
POSTGRES_USER= | ||
POSTGRES_PASSWORD= |
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 +1 @@ | ||
0.1.0 | ||
0.2.0 |
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,10 +1,22 @@ | ||
package model | ||
|
||
import "fmt" | ||
|
||
type LoginData struct { | ||
Username string `json:"username"` | ||
PasswordHash string `json:"passwordHash"` | ||
Username string `json:"username"` | ||
Password string `json:"password"` | ||
} | ||
|
||
func (l LoginData) IsValid() bool { | ||
return l.Username != "" | ||
} | ||
|
||
type RegisteredUser struct { | ||
Id string | ||
Username string | ||
PasswordHash string `pg:"password"` | ||
} | ||
|
||
func (r *RegisteredUser) String() string { | ||
return fmt.Sprintf("RegisteredUser<%s, %s, %s>", r.Id, r.Username, r.PasswordHash) | ||
} |
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,51 @@ | ||
package verify | ||
|
||
import ( | ||
"context" | ||
"github.com/go-pg/pg/v10" | ||
"github.com/go-pg/pg/v10/orm" | ||
"github.com/l12u/userm/internal/model" | ||
"golang.org/x/crypto/bcrypt" | ||
) | ||
|
||
type PostgresVerifier struct { | ||
db *pg.DB | ||
} | ||
|
||
func NewPostgresVerifier(opt *pg.Options) *PostgresVerifier { | ||
db := pg.Connect(opt) | ||
err := db.Ping(context.Background()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// setup tables if not exists | ||
err = db.Model(&model.RegisteredUser{}).CreateTable(&orm.CreateTableOptions{ | ||
IfNotExists: true, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return &PostgresVerifier{ | ||
db: db, | ||
} | ||
} | ||
|
||
func (p *PostgresVerifier) Verify(user string, pw string) (bool, error) { | ||
u := &model.RegisteredUser{} | ||
err := p.db.Model(u).Where("? = ?", pg.Ident("username"), user).Select() | ||
if err != nil { | ||
if err == pg.ErrNoRows { | ||
return false, ErrNotFound | ||
} | ||
return false, err | ||
} | ||
|
||
err = bcrypt.CompareHashAndPassword([]byte(u.PasswordHash), []byte(pw)) | ||
if err != nil { | ||
return false, ErrHashDoesntMatch | ||
} | ||
|
||
return true, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Package verify is for verifying the credentials of a user are valid. | ||
package verify | ||
|
||
import "errors" | ||
|
||
var ( | ||
ErrNotFound = errors.New("could not find entry for given user") | ||
ErrHashDoesntMatch = errors.New("hashed password is not hash of the given password") | ||
) | ||
|
||
type Verifier interface { | ||
Verify(user string, pw string) (bool, error) | ||
} | ||
|
||
type SimpleVerifier struct { | ||
} | ||
|
||
func (s *SimpleVerifier) Verify(_ string, _ string) (bool, error) { | ||
return true, nil | ||
} |