-
Notifications
You must be signed in to change notification settings - Fork 1
/
eventdb-token.go
70 lines (60 loc) · 1.66 KB
/
eventdb-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
// package main provides a utility for creating service tokens for authenticating with eventdb via firebase
package main
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
firebase "firebase.google.com/go"
_ "github.com/lib/pq"
"google.golang.org/api/option"
)
func main() {
var (
firebaseProjectID = flag.String("project-id", "the-third-party", "The firebase project-id used for auth")
serviceAccount = flag.String("service-account", "", "Google service account JSON file associated with the firebase project")
apiKey = flag.String("api-key", "", "A Google API key associate with the firebase projet")
)
flag.Parse()
tokenName := flag.Arg(0)
if tokenName == "" {
log.Fatal("usage: eventdb-token <token name>")
}
ctx := context.Background()
app, err := firebase.NewApp(ctx, &firebase.Config{
ProjectID: *firebaseProjectID,
}, option.WithServiceAccountFile(*serviceAccount))
if err != nil {
log.Fatal(err)
}
auth, err := app.Auth(ctx)
if err != nil {
log.Fatal(err)
}
uid := fmt.Sprintf("service-%s", tokenName)
customToken, err := auth.CustomToken(uid)
if err != nil {
log.Fatal(err)
}
verifyReq, err := json.Marshal(map[string]interface{}{
"returnSecureToken": true,
"token": customToken,
})
if err != nil {
log.Fatal(err)
}
verifyURL := fmt.Sprintf("https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken?key=%s", *apiKey)
resp, err := http.Post(verifyURL, "application/json", bytes.NewReader(verifyReq))
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if _, err := io.Copy(os.Stdout, resp.Body); err != nil {
log.Fatal(err)
}
}