-
Notifications
You must be signed in to change notification settings - Fork 4
/
token.go
112 lines (106 loc) · 2.92 KB
/
token.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package dads
import (
"encoding/base64"
"fmt"
"os"
"strconv"
"sync"
"time"
"github.com/LF-Engineering/da-ds/build"
"github.com/LF-Engineering/dev-analytics-libraries/auth0"
"github.com/LF-Engineering/dev-analytics-libraries/elastic"
"github.com/LF-Engineering/dev-analytics-libraries/http"
"github.com/LF-Engineering/dev-analytics-libraries/slack"
jsoniter "github.com/json-iterator/go"
)
var (
gAuth0Client *auth0.ClientProvider
gTokenEnv string
gTokenEnvMtx *sync.Mutex
)
// InitializeAuth0 - initializes Auth0 client using data stored in AUTH0_DATA
func InitializeAuth0() error {
var err error
auth0DataB64 := os.Getenv("AUTH0_DATA")
if auth0DataB64 == "" {
return fmt.Errorf("you must specify AUTH0_DATA (so the program can generate an API token) or specify token with JWT_TOKEN")
}
var auth0Data []byte
auth0Data, err = base64.StdEncoding.DecodeString(auth0DataB64)
if err != nil {
Printf("decode base64 error: %+v\n", err)
return err
}
//fmt.Printf("auth0Data: %v\n", auth0Data)
var data map[string]string
err = jsoniter.Unmarshal([]byte(auth0Data), &data)
if err != nil {
Printf("unmarshal error: %+v\n", err)
return err
}
AddRedacted(data["es_url"], true)
AddRedacted(data["es_user"], true)
AddRedacted(data["es_pass"], true)
AddRedacted(data["client_id"], true)
AddRedacted(data["client_secret"], true)
AddRedacted(data["audience"], true)
AddRedacted(data["url"], true)
AddRedacted(data["slack_webhook_url"], true)
// Providers
httpClientProvider := http.NewClientProvider(60 * time.Second)
esCacheClientProvider, err := elastic.NewClientProvider(
&elastic.Params{
URL: data["es_url"],
Username: data["es_user"],
Password: data["es_pass"],
})
if err != nil {
Printf("ES client provider error: %+v\n", err)
return err
}
appName := build.AppName
ds := os.Getenv("DA_DS")
if ds != "" {
appName += "-" + ds
}
appNameVersion := fmt.Sprintf("%s-%v", appName, strconv.FormatInt(time.Now().Unix(), 10))
slackProvider := slack.New(data["slack_webhook_url"])
gAuth0Client, err = auth0.NewAuth0Client(
data["env"],
data["grant_type"],
data["client_id"],
data["client_secret"],
data["audience"],
data["url"],
httpClientProvider,
esCacheClientProvider,
&slackProvider,
appNameVersion,
)
if err == nil {
gTokenEnv = data["env"]
}
Printf("initialized %s auth0\n", gTokenEnv)
return err
}
// GetAPIToken - return an API token to use dev-analytics-api API calls
// If JWT_TOKEN env is specified - just use the provided token without any checks
// Else get auth0 data from AUTH0_DATA and generate/reuse a token stored in ES cache
func GetAPIToken() (string, error) {
envToken := os.Getenv("JWT_TOKEN")
if envToken != "" {
return envToken, nil
}
if gTokenEnvMtx != nil {
gTokenEnvMtx.Lock()
defer gTokenEnvMtx.Unlock()
}
if gTokenEnv == "" {
err := InitializeAuth0()
if err != nil {
return "", err
}
}
token, err := gAuth0Client.GetToken()
return token, err
}