-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.go
84 lines (65 loc) · 1.7 KB
/
task.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
package auth
import (
"context"
// Packages
server "github.com/mutablelogic/go-server"
"github.com/mutablelogic/go-server/pkg/provider"
// Namespace imports
. "github.com/djthorpe/go-errors"
)
///////////////////////////////////////////////////////////////////////////////
// TYPES
type auth struct {
jar TokenJar
tokenBytes int
bearer bool
}
// Check interfaces are satisfied
var _ server.Task = (*auth)(nil)
///////////////////////////////////////////////////////////////////////////////
// LIFECYCLE
// Create a new auth task from the configuration
func New(c Config) (*auth, error) {
task := new(auth)
// Set token jar
if c.TokenJar == nil {
return nil, ErrInternalAppError.With("missing 'tokenjar'")
} else {
task.jar = c.TokenJar
}
// Set token bytes
if c.TokenBytes <= 0 {
task.tokenBytes = defaultTokenBytes
} else {
task.tokenBytes = c.TokenBytes
}
// Set bearer
task.bearer = c.Bearer
// Return success
return task, nil
}
/////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// Return the label
func (task *auth) Label() string {
// TODO
return defaultName
}
// Run the task until the context is cancelled
func (task *auth) Run(ctx context.Context) error {
var result error
// Logger
logger := provider.Logger(ctx)
// If there are no tokens, then create a "root" token
if tokens := task.jar.Tokens(); len(tokens) == 0 {
token := NewToken(defaultRootNme, task.tokenBytes, 0, ScopeRoot)
logger.Printf(ctx, "Creating root token %q for scope %q", token.Value, ScopeRoot)
if err := task.jar.Create(token); err != nil {
return err
}
}
// Run the task until cancelled
<-ctx.Done()
// Return any errors
return result
}