-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
98 lines (78 loc) · 2.04 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/mousybusiness/firebase_token/creds"
"github.com/mousybusiness/firebase_token/fireb"
log "github.com/sirupsen/logrus"
"os"
"strings"
)
var (
err error
logLevel = log.ErrorLevel
)
func main() {
// allow log level override for debugging
ll := os.Getenv("LOG_LEVEL")
if ll != "" {
logLevel, err = log.ParseLevel(strings.ToLower(ll))
if err != nil {
log.Fatal(err)
}
}
log.SetLevel(logLevel)
log.SetOutput(os.Stderr)
port := "5000"
var cfg, apiKey, refresh string
var token bool
flag.StringVar(&apiKey, "apiKey", "", "Admin service account API key (see README.md)")
flag.StringVar(&cfg, "config", "", "Web app config JSON (see README.md)")
flag.StringVar(&refresh, "refresh", "", "Refresh without UI using existing refresh token")
flag.StringVar(&port, "port", "", "Redirect port")
flag.BoolVar(&token, "token", false, "Only returns IDToken if successful, useful for scripts")
flag.Parse()
if apiKey == "" {
log.Fatal("require key flag")
}
if cfg == "" {
log.Fatal("require config flag")
}
path := cfg
if _, err := os.Stat(path); os.IsNotExist(err) {
log.Fatal("config file doesn't exist")
}
b, err := os.ReadFile(path)
if err != nil {
log.Fatal(err)
}
var root fireb.Root
if err := json.Unmarshal(b, &root); err != nil {
log.Fatal(err)
}
config := root.Config
if config.ClientID == "" || config.ClientSecret == "" || config.TokenURI == "" || config.AuthURI == "" {
log.Fatal("malformed config")
}
config.APIKey = apiKey
log.Debugf("config: %v", config)
c := fireb.New(port, config)
var credentials *creds.Credentials
if refresh == "" {
credentials = c.Auth()
} else {
log.Debugf("using refresh token")
credentials, _ = c.Refresh(creds.RefreshToken(refresh))
}
if credentials == nil {
log.Fatal("failed")
}
if token {
fmt.Printf("%v", credentials.IDToken)
os.Exit(0)
}
fmt.Printf("UID: %v\n", credentials.UID)
fmt.Printf("ID_TOKEN: %v\n", credentials.IDToken)
fmt.Printf("REFRESH_TOKEN: %v\n", credentials.RefreshToken)
}