-
Notifications
You must be signed in to change notification settings - Fork 24
/
token_source.go
49 lines (39 loc) · 1.19 KB
/
token_source.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
package google
import (
"context"
"go.uber.org/fx"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"github.com/fluxninja/aperture/v2/pkg/config"
tokenconfig "github.com/fluxninja/aperture/v2/pkg/google/config"
"github.com/fluxninja/aperture/v2/pkg/log"
)
// SourceIn stores fields and parameters provided to the Token Source constructor.
type SourceIn struct {
fx.In
Unmarshaller config.Unmarshaller
}
// Module provides a singleton pointer to oauth2.TokenSource via FX.
func Module() fx.Option {
return fx.Options(
fx.Provide(provideGoogleTokenSource),
)
}
const tokenSourceConfigKey = "token_source"
func provideGoogleTokenSource(in SourceIn) (oauth2.TokenSource, error) {
log.Info().Msg("Initializing Google Token Source")
var config tokenconfig.Config
if err := in.Unmarshaller.UnmarshalKey(tokenSourceConfigKey, &config); err != nil {
log.Error().Err(err).Msg("Unable to unmarshal configuration")
return nil, err
}
if !config.Enabled {
return nil, nil
}
tokenSource, err := google.DefaultTokenSource(context.Background(), config.Scopes...)
if err != nil {
log.Error().Err(err).Msg("Failed to create Google Token Source")
return nil, err
}
return tokenSource, nil
}