feat: token based authentication, authenticate route, token storage
This commit is contained in:
@@ -14,18 +14,20 @@ import (
|
||||
// Define constants for the token scope. For now we just define the scope "activation"
|
||||
// but we'll add additional scopes later in the book.
|
||||
const (
|
||||
ScopeActivation = "activation"
|
||||
ScopeActivation = "activation"
|
||||
ScopeAuthentication = "authentication" // Include a new authentication scope.
|
||||
|
||||
)
|
||||
|
||||
// Define a Token struct to hold the data for an individual token. This includes the
|
||||
// plaintext and hashed versions of the token, associated user ID, expiry time and
|
||||
// scope.
|
||||
type Token struct {
|
||||
Plaintext string
|
||||
Hash []byte
|
||||
UserID int64
|
||||
Expiry time.Time
|
||||
Scope string
|
||||
Plaintext string `json:"token"`
|
||||
Hash []byte `json:"-"`
|
||||
UserID int64 `json:"-"`
|
||||
Expiry time.Time `json:"expiry"`
|
||||
Scope string `json:"-"`
|
||||
}
|
||||
|
||||
func generateToken(userID int64, ttl time.Duration, scope string) (*Token, error) {
|
||||
|
||||
@@ -39,6 +39,14 @@ type password struct {
|
||||
hash []byte
|
||||
}
|
||||
|
||||
// Declare a new AnonymousUser variable.
|
||||
var AnonymousUser = &User{}
|
||||
|
||||
// Check if a User instance is the AnonymousUser.
|
||||
func (u *User) IsAnonymous() bool {
|
||||
return u == AnonymousUser
|
||||
}
|
||||
|
||||
// The Set() method calculates the bcrypt hash of a plaintext password, and stores both
|
||||
// the hash and the plaintext versions in the struct.
|
||||
func (p *password) Set(plaintextPassword string) error {
|
||||
|
||||
Reference in New Issue
Block a user