-
Notifications
You must be signed in to change notification settings - Fork 14
/
auth.go
85 lines (63 loc) · 2.04 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
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
package bob
import (
"errors"
"fmt"
"github.com/benchkram/errz"
"github.com/benchkram/bob/pkg/auth"
"github.com/benchkram/bob/pkg/usererror"
)
func (b *B) CreateAuthContext(name, token string) (err error) {
defer errz.Recover(&err)
err = b.authStore.CreateContext(name, token)
if errors.Is(err, auth.ErrAlreadyExists) {
return usererror.Wrapm(err, fmt.Sprintf("failed to create authentication context [%s]", name))
}
return nil
}
func (b *B) DeleteAuthContext(name string) (err error) {
defer errz.Recover(&err)
err = b.authStore.DeleteContext(name)
if errors.Is(err, auth.ErrNotFound) {
return usererror.Wrapm(err, fmt.Sprintf("failed to delete context [%s]", name))
}
return err
}
func (b *B) AuthContexts() ([]auth.Context, error) {
// no usererror needed
return b.authStore.Contexts()
}
func (b *B) AuthContext(name string) (authCtx auth.Context, err error) {
defer errz.Recover(&err)
authCtx, err = b.authStore.Context(name)
if errors.Is(err, auth.ErrNotFound) {
return auth.Context{}, usererror.Wrapm(err, fmt.Sprintf("failed to retrieve authentication context [%s]", name))
}
errz.Fatal(err)
return authCtx, nil
}
func (b *B) CurrentAuthContext() (curr auth.Context, err error) {
defer errz.Recover(&err)
curr, err = b.authStore.CurrentContext()
if errors.Is(err, auth.ErrNotFound) {
return auth.Context{}, usererror.Wrapm(err, "failed to retrieve current authentication context")
}
errz.Fatal(err)
return curr, nil
}
func (b *B) SetCurrentAuthContext(name string) (err error) {
defer errz.Recover(&err)
err = b.authStore.SetCurrentContext(name)
if errors.Is(err, auth.ErrNotFound) {
return usererror.Wrapm(err, fmt.Sprintf("failed to set current authentication context [%s]", name))
}
return nil
}
func (b *B) UpdateAuthContext(name string, token string) (err error) {
defer errz.Recover(&err)
err = b.authStore.UpdateContext(name, token)
if errors.Is(err, auth.ErrNotFound) {
return usererror.Wrapm(err, fmt.Sprintf("failed to update authentication context [%s]", name))
}
errz.Fatal(err)
return nil
}