-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.go
51 lines (41 loc) · 1.03 KB
/
login.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
50
51
package memory
import (
"context"
"strings"
"github.com/ViBiOh/auth/v2/pkg/argon"
"github.com/ViBiOh/auth/v2/pkg/ident"
"github.com/ViBiOh/auth/v2/pkg/model"
"golang.org/x/crypto/bcrypt"
)
func (s Service) Login(_ context.Context, login, password string) (model.User, error) {
user, ok := s.ident[login]
if !ok {
return model.User{}, ident.ErrInvalidCredentials
}
switch {
case strings.HasPrefix(string(user.password), "$argon2id"):
if argon.CompareHashAndPassword(string(user.password), password) == nil {
return user.User, nil
}
default:
if bcrypt.CompareHashAndPassword(user.password, []byte(password)) == nil {
return user.User, nil
}
}
return model.User{}, ident.ErrInvalidCredentials
}
func (s Service) IsAuthorized(_ context.Context, user model.User, profile string) bool {
profiles, ok := s.auth[user.ID]
if !ok {
return false
}
if len(profile) == 0 {
return true
}
for _, listedProfile := range profiles {
if strings.EqualFold(profile, listedProfile) {
return true
}
}
return false
}