File size: 6,406 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package basic

import (
	"context"
	"database/sql"
	"sync"

	"github.com/target/goalert/config"
	"github.com/target/goalert/permission"
	"github.com/target/goalert/util"
	"github.com/target/goalert/validation"
	"github.com/target/goalert/validation/validate"

	"github.com/pkg/errors"
	"golang.org/x/crypto/bcrypt"
)

// Store can create new user/pass links and validate a username and password. bcrypt is used
// for password storage & verification.
type Store struct {
	insert        *sql.Stmt
	getByUsername *sql.Stmt
	getByID       *sql.Stmt
	update        *sql.Stmt

	mx sync.Mutex
}

// NewStore creates a new DB. Error is returned if the prepared statements fail to register.
func NewStore(ctx context.Context, db *sql.DB) (*Store, error) {
	p := &util.Prepare{
		DB:  db,
		Ctx: ctx,
	}
	return &Store{
		insert:        p.P("INSERT INTO auth_basic_users (user_id, username, password_hash) VALUES ($1, $2, $3)"),
		getByUsername: p.P("SELECT user_id, password_hash FROM auth_basic_users WHERE username = $1"),
		getByID:       p.P("SELECT password_hash FROM auth_basic_users WHERE user_id = $1"),
		update:        p.P("UPDATE auth_basic_users SET password_hash = $2 WHERE user_id = $1"),
	}, p.Err
}

// HashedPassword is an interface that can be used to store a password.
type HashedPassword interface {
	Hash() string

	_private() // prevent external implementations
}

type hashed []byte

func (h hashed) Hash() string { return string(h) }
func (h hashed) _private()    {}

// ValidatedPassword represents a validated password for a UserID.
type ValidatedPassword interface {
	UserID() string

	_private() // prevent external implementations
}

type validated string

func (v validated) UserID() string { return string(v) }
func (v validated) _private()      {}

// ValidateBasicAuth returns an access denied error for non-admins when basic auth is disabled in configs.
func ValidateBasicAuth(ctx context.Context) error {
	if permission.Admin(ctx) {
		return nil
	}

	cfg := config.FromContext(ctx)
	if cfg.Auth.DisableBasic {
		return permission.NewAccessDenied("Basic auth is disabled by administrator.")
	}

	return nil
}

// NewHashedPassword will hash the given password and return a Password object.
func (b *Store) NewHashedPassword(ctx context.Context, password string) (HashedPassword, error) {
	err := ValidateBasicAuth(ctx)
	if err != nil {
		return nil, err
	}

	err = validate.Text("Password", password, 8, 200)
	if err != nil {
		return nil, err
	}

	b.mx.Lock()
	defer b.mx.Unlock()
	hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), 12)
	if err != nil {
		return nil, err
	}

	return hashed(hashedPassword), nil
}

// CreateTx should add a new entry for the username/password combination linking to userID.
// An error is returned if the username is not unique or the userID is invalid.
// Must have same user or admin role.
func (b *Store) CreateTx(ctx context.Context, tx *sql.Tx, userID, username string, password HashedPassword) error {
	err := ValidateBasicAuth(ctx)
	if err != nil {
		return err
	}

	err = permission.LimitCheckAny(ctx, permission.System, permission.Admin, permission.MatchUser(userID))
	if err != nil {
		return err
	}

	err = validate.Many(
		validate.UUID("UserID", userID),
		validate.Username("Username", username),
	)
	if err != nil {
		return err
	}

	_, err = tx.StmtContext(ctx, b.insert).ExecContext(ctx, userID, username, password.Hash())
	return err
}

// UpdateTx updates a user's password. oldPass is required if the current context is not an admin.
func (b *Store) UpdateTx(ctx context.Context, tx *sql.Tx, userID string, oldPass ValidatedPassword, newPass HashedPassword) error {
	err := ValidateBasicAuth(ctx)
	if err != nil {
		return err
	}

	err = permission.LimitCheckAny(ctx, permission.Admin, permission.MatchUser(userID))
	if err != nil {
		return err
	}

	err = validate.UUID("UserID", userID)
	if err != nil {
		return err
	}

	if oldPass != nil && oldPass.UserID() != userID {
		return validation.NewFieldError("OldPassword", "Password does not match User")
	}
	if (!permission.Admin(ctx) || permission.UserID(ctx) == userID) && oldPass == nil {
		return validation.NewFieldError("OldPassword", "Previous password required")
	}

	res, err := tx.StmtContext(ctx, b.update).ExecContext(ctx, userID, newPass.Hash())
	if err != nil {
		return err
	}

	count, err := res.RowsAffected()
	if err != nil {
		return err
	}

	if count == 0 {
		return validation.NewFieldError("UserID", "does not have basic auth configured")
	}

	return nil
}

// Validate should return a userID if the username and password match.
func (b *Store) Validate(ctx context.Context, username, password string) (string, error) {
	err := validate.Many(
		validate.Username("Username", username),
		validate.Text("Password", password, 1, 200),
	)
	if err != nil {
		return "", err
	}

	row := b.getByUsername.QueryRowContext(ctx, username)
	var userID, hashed string
	err = row.Scan(&userID, &hashed)
	if err != nil {
		if errors.Is(err, sql.ErrNoRows) {
			return "", errors.New("invalid username")
		}
		return "", errors.WithMessage(err, "user lookup failure")
	}

	// Since this can be CPU intensive, we'll only allow one at a time.
	b.mx.Lock()
	defer b.mx.Unlock()

	err = bcrypt.CompareHashAndPassword([]byte(hashed), []byte(password))
	if err != nil {
		return "", errors.WithMessage(err, "invalid password")
	}

	return userID, nil
}

// ValidatePassword will validate the password of the currently authenticated user.
func (b *Store) ValidatePassword(ctx context.Context, password string) (ValidatedPassword, error) {
	err := ValidateBasicAuth(ctx)
	if err != nil {
		return nil, err
	}

	err = permission.LimitCheckAny(ctx, permission.User)
	if err != nil {
		return nil, err
	}

	userID := permission.UserID(ctx)

	err = validate.Many(
		validate.UUID("UserID", userID),
		validate.Text("OldPassword", password, 8, 200),
	)
	if err != nil {
		return nil, err
	}

	var hash string
	err = b.getByID.QueryRowContext(ctx, userID).Scan(&hash)
	if errors.Is(err, sql.ErrNoRows) {
		return nil, errors.New("unknown userID")
	}
	if err != nil {
		return nil, errors.WithMessage(err, "user lookup failure")
	}

	b.mx.Lock()
	defer b.mx.Unlock()

	err = bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
	if err != nil {
		return nil, validation.NewFieldError("OldPassword", "invalid password")
	}

	return validated(userID), nil
}