-
Notifications
You must be signed in to change notification settings - Fork 10
/
auth.go
55 lines (46 loc) · 1.96 KB
/
auth.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
52
53
54
55
/*
Package auth contains any known Authenticator extensions that may be selected at
build-time for use in kwild. Authenticator extensions are used to expand the
type of signatures that may be verified, and define address derivation for the
public keys of the corresponding type.
Build constraints a.k.a. build tags are used to enable extensions in a kwild
binary. See README.md in the extensions package for more information.
*/
package auth
import (
"errors"
"fmt"
"strings"
"github.com/kwilteam/kwil-db/core/crypto/auth"
// internal/ident is home to the Authenticator registry used by kwild, as
// well as the registry-powered verification functions used by kwild. The
// RegisterAuthenticator helper is provided here so that extension
// implementations may register themselves on import, but it would be fine
// to shift that responsibility to the importing code in kwild (these stubs)
)
var (
// ErrAuthenticatorExists is returned when an authenticator is already registered
ErrAuthenticatorExists = errors.New("authenticator already exists")
// ErrAuthenticatorNotFound is returned when an authenticator is not found
ErrAuthenticatorNotFound = errors.New("authenticator not found")
)
// registeredAuthenticators is the Authenticator registry used by kwild.
var registeredAuthenticators = make(map[string]auth.Authenticator)
// RegisterAuthenticator registers an authenticator with a given name
func RegisterAuthenticator(name string, auth auth.Authenticator) error {
name = strings.ToLower(name)
if _, ok := registeredAuthenticators[name]; ok {
return fmt.Errorf("%w: %s", ErrAuthenticatorExists, name)
}
registeredAuthenticators[name] = auth
return nil
}
// getAuthenticator returns an authenticator by the name it was registered with
func GetAuthenticator(name string) (auth.Authenticator, error) {
name = strings.ToLower(name)
auth, ok := registeredAuthenticators[name]
if !ok {
return nil, fmt.Errorf("%w: %s", ErrAuthenticatorNotFound, name)
}
return auth, nil
}