-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
authenticated.go
74 lines (56 loc) · 1.65 KB
/
authenticated.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
package service
import (
"github.com/tidepool-org/platform/application"
authClient "github.com/tidepool-org/platform/auth/client"
"github.com/tidepool-org/platform/errors"
"github.com/tidepool-org/platform/platform"
)
type Authenticated struct {
*Service
authClient *authClient.Client
}
func NewAuthenticated() *Authenticated {
return &Authenticated{
Service: New(),
}
}
func (a *Authenticated) Initialize(provider application.Provider) error {
if err := a.Service.Initialize(provider); err != nil {
return err
}
return a.initializeAuthClient()
}
func (a *Authenticated) Terminate() {
a.terminateAuthClient()
a.Service.Terminate()
}
func (a *Authenticated) initializeAuthClient() error {
a.Logger().Debug("Loading auth client config")
cfg := authClient.NewConfig()
cfg.UserAgent = a.UserAgent()
cfg.ExternalConfig.UserAgent = a.UserAgent()
if err := cfg.Load(a.ConfigReporter().WithScopes("auth", "client")); err != nil {
return errors.Wrap(err, "unable to load auth client config")
}
a.Logger().Debug("Creating auth client")
clnt, err := authClient.NewClient(cfg, platform.AuthorizeAsService, a.Name(), a.Logger())
if err != nil {
return errors.Wrap(err, "unable to create auth client")
}
a.authClient = clnt
a.Logger().Debug("Starting auth client")
if err = a.authClient.Start(); err != nil {
return errors.Wrap(err, "unable to start auth client")
}
a.SetAuthClient(a.authClient)
return nil
}
func (a *Authenticated) terminateAuthClient() {
if a.authClient != nil {
a.Logger().Debug("Closing auth client")
a.authClient.Close()
a.Logger().Debug("Destroying auth client")
a.authClient = nil
a.SetAuthClient(nil)
}
}