-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
180 lines (154 loc) · 4 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"gopkg.in/AlecAivazis/survey.v1"
)
// set at compile-time using -ldflags "-X main.version=$VERSION"
var version string
type verifyPasswordRequest struct {
Email string `json:"email"`
Password string `json:"password"`
ReturnSecureToken bool `json:"returnSecureToken"`
}
type verifyPasswordResponse struct {
Kind string `json:"king"`
LocalID string `json:"localId"`
Email string `json:"email"`
DisplayName string `json:"displayName"`
IDToken IDToken `json:"idToken"`
Registered bool `json:"registered"`
RefreshToken string `json:"refreshToken"`
expiresIn string `json:""expiresIn`
}
type badRequestResponse struct {
Error struct {
Code int `json:"code"`
Message string `json:"message"`
Errors []struct {
Message string `json:"message"`
Domain string `json:"domain"`
Reason string `json:"reason"`
}
} `json:"error"`
}
type JWTPayload map[string]interface{}
func (p JWTPayload) Print() {
for k, v := range p {
fmt.Printf("%s: %v\n", k, v)
}
}
type IDToken string
func (t IDToken) Claims() (JWTPayload, error) {
parts := strings.Split(string(t), ".")
decoded, err := base64.RawStdEncoding.DecodeString(parts[1])
if err != nil {
return nil, err
}
var payload map[string]interface{}
err = json.Unmarshal(decoded, &payload)
if err != nil {
fmt.Printf("%v", err)
return nil, err
}
return payload, nil
}
func verifyPassword(key, email, password string) (*verifyPasswordResponse, error) {
vpReq := verifyPasswordRequest{
Email: email,
Password: password,
ReturnSecureToken: true,
}
j, err := json.Marshal(vpReq)
if err != nil {
return nil, err
}
// build the URL including Query params
v := url.Values{}
v.Set("key", key)
u := url.URL{
Scheme: "https",
Host: "www.googleapis.com",
Path: "identitytoolkit/v3/relyingparty/verifyPassword",
ForceQuery: false,
RawQuery: v.Encode(),
}
// build and execute the request
req, err := http.NewRequest("POST", u.String(), bytes.NewBuffer(j))
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
if err != nil {
return nil, err
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
//fmt.Println(string(body))
if resp.StatusCode >= 400 {
var badReqRes badRequestResponse
err = json.Unmarshal(body, &badReqRes)
if err != nil {
return nil, err
}
fmt.Fprintf(os.Stderr, "%d %s\n", badReqRes.Error.Code, badReqRes.Error.Message)
os.Exit(1)
}
var vpRes verifyPasswordResponse
err = json.Unmarshal(body, &vpRes)
if err != nil {
return nil, err
}
return &vpRes, nil
}
func main() {
var key, email, password string
showVersion := flag.Bool("v", false, "Shows the version of the command line tool.")
flag.StringVar(&key, "w", "", "Web API Key (Project Overview -> Users and Permissions -> General).")
flag.Parse()
if *showVersion {
fmt.Println(version)
os.Exit(0)
}
if key == "" {
fmt.Printf("You must specify a Web API Key using the -w flag.\nSee Firebase (Project Overview -> Users and Permissions -> General).\n")
os.Exit(1)
}
fmt.Printf("Web API Key: %s\n", key)
promptE := &survey.Input{
Message: "Email:",
}
survey.AskOne(promptE, &email, nil)
promptP := &survey.Password{
Message: "Password:",
}
survey.AskOne(promptP, &password, nil)
vpr, err := verifyPassword(key, email, password)
if err != nil {
fmt.Fprintf(os.Stderr, "error verifying credentials: %v", err)
os.Exit(1)
}
fmt.Printf("Display Name: %s\n", vpr.DisplayName)
fmt.Printf("Email: %s\n", vpr.Email)
fmt.Printf("\nIDToken:\n%s\n", vpr.IDToken)
claims, err := vpr.IDToken.Claims()
if err != nil {
fmt.Fprintf(os.Stderr, "error extracting claims: %v", err)
os.Exit(1)
}
fmt.Println()
fmt.Println("Claims:")
claims.Print()
os.Exit(0)
}