/
realmode.go
49 lines (37 loc) · 1.31 KB
/
realmode.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
43
44
45
46
47
48
49
// +build !devmode
package auth
import (
"context"
"golang.org/x/crypto/bcrypt"
opentracing "github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
)
// When devmode is not enabled, do actual validation
func (a *auth) ValidateByID(ctx context.Context, id string, password string) (bool, error) {
fullSpan, ctx := opentracing.StartSpanFromContext(ctx, "Validate")
defer fullSpan.Finish()
hashSpan := opentracing.StartSpan("Hash password", opentracing.ChildOf(fullSpan.Context()))
hashed, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
hashSpan.Finish()
if err != nil {
err = errors.Wrap(err, "failed to hash supplied password")
fullSpan.SetTag("error", true)
fullSpan.SetTag("error.object", err)
return false, err
}
entry, err := a.db.GetUserByID(ctx, id)
if err != nil {
err = errors.Wrap(err, "unexpected error while finding user")
fullSpan.SetTag("error", true)
fullSpan.SetTag("error.object", err)
return false, err
}
if entry == nil {
return false, nil
}
compareSpan := opentracing.StartSpan("Compare hash and password", opentracing.ChildOf(fullSpan.Context()))
// bcrypt handles the salt in the encoded value for us
valid := bcrypt.CompareHashAndPassword([]byte(entry.PasswordHashWithSalt), hashed) != nil
compareSpan.Finish()
return valid, nil
}