forked from remind101/empire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
243 lines (206 loc) · 5.68 KB
/
auth.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package main
import (
"fmt"
"net/url"
"os"
"strings"
"time"
"github.com/bgentry/speakeasy"
"github.com/remind101/empire/cmd/emp/hkclient"
"github.com/remind101/empire/pkg/heroku"
)
var cmdAuthorize = &Command{
Run: runAuthorize,
Usage: "authorize",
Category: "emp",
Short: "procure a temporary privileged token" + extra,
Long: `
Have heroku-agent procure and store a temporary privileged token
that will bypass any requirement for a second authentication factor.
Example:
$ emp authorize
Enter email: user@test.com
Enter two-factor auth code:
Authorization successful.
`,
}
func runAuthorize(cmd *Command, args []string) {
if len(args) != 0 {
cmd.PrintUsage()
os.Exit(2)
}
if os.Getenv("HEROKU_AGENT_SOCK") == "" {
printFatal("Authorize must be used with heroku-agent; please set " +
"HEROKU_AGENT_SOCK")
}
var twoFactorCode string
fmt.Printf("Enter two-factor auth code: ")
if _, err := fmt.Scanln(&twoFactorCode); err != nil {
printFatal("reading two-factor auth code: " + err.Error())
}
client.AdditionalHeaders.Set("Heroku-Two-Factor-Code", twoFactorCode)
// No-op: GET /apps with max=0. heroku-agent will detect that a two-factor
// code was included and attempt to procure a temporary token. This token
// will then be re-used automatically on subsequent requests.
_, err := client.AppList(&heroku.ListRange{Field: "name", Max: 0})
must(err)
fmt.Println("Authorization successful.")
}
var cmdCreds = &Command{
Run: runCreds,
Usage: "creds",
Category: "emp",
Short: "show credentials" + extra,
Long: `Creds shows credentials that will be used for API calls.`,
}
func runCreds(cmd *Command, args []string) {
var err error
nrc, err = hkclient.LoadNetRc()
if err != nil {
printFatal(err.Error())
}
u, err := url.Parse(apiURL)
if err != nil {
printFatal("could not parse API url: " + err.Error())
}
user, pass, err := nrc.GetCreds(u)
if err != nil {
printFatal("could not get credentials: " + err.Error())
}
fmt.Println(user, pass)
}
var cmdLogin = &Command{
Run: runLogin,
Usage: "login",
Category: "emp",
Short: "log in to your Heroku account" + extra,
Long: `
Log in with your Heroku credentials. Input is accepted by typing
on the terminal. On unix machines, you can also pipe a password
on standard input.
Example:
$ emp login
Enter email: user@test.com
Enter password:
Login successful.
`,
}
func runLogin(cmd *Command, args []string) {
if len(args) != 0 {
cmd.PrintUsage()
os.Exit(2)
}
oldEmail := client.Username
var email string
if oldEmail == "" {
fmt.Printf("Enter email: ")
} else {
fmt.Printf("Enter email [%s]: ", oldEmail)
}
_, err := fmt.Scanln(&email)
switch {
case err != nil && err.Error() != "unexpected newline":
printFatal(err.Error())
case email == "" && oldEmail == "":
printFatal("email is required.")
case email == "":
email = oldEmail
}
// NOTE: gopass doesn't support multi-byte chars on Windows
password, err := readPassword("Enter password: ")
switch {
case err == nil:
case err.Error() == "unexpected newline":
printFatal("password is required.")
default:
printFatal(err.Error())
}
address, token, err := attemptLogin(email, password, "")
if err != nil {
if herror, ok := err.(heroku.Error); ok && herror.Id == "two_factor" {
// 2FA requested, attempt 2FA login
var twoFactorCode string
fmt.Printf("Enter two-factor auth code: ")
if _, err := fmt.Scanln(&twoFactorCode); err != nil {
printFatal("reading two-factor auth code: " + err.Error())
}
address, token, err = attemptLogin(email, password, twoFactorCode)
must(err)
} else {
must(err)
}
}
nrc, err = hkclient.LoadNetRc()
if err != nil {
printFatal("loading netrc: " + err.Error())
}
err = nrc.SaveCreds(address, email, token)
if err != nil {
printFatal("saving new token: " + err.Error())
}
fmt.Println("Logged in.")
}
func readPassword(prompt string) (password string, err error) {
if acceptPasswordFromStdin && !isTerminalIn {
_, err = fmt.Scanln(&password)
return
}
// NOTE: speakeasy may not support multi-byte chars on Windows
return speakeasy.Ask("Enter password: ")
}
func attemptLogin(username, password, twoFactorCode string) (hostname, token string, err error) {
description := "emp login from " + time.Now().UTC().Format(time.RFC3339)
expires := 2592000 // 30 days
opts := heroku.OAuthAuthorizationCreateOpts{
Description: &description,
ExpiresIn: &expires,
}
req, err := client.NewRequest("POST", "/oauth/authorizations", &opts, nil)
if err != nil {
return "", "", fmt.Errorf("unknown error when creating login request: %s", err.Error())
}
req.SetBasicAuth(username, password)
if twoFactorCode != "" {
req.Header.Set("Heroku-Two-Factor-Code", twoFactorCode)
}
var auth heroku.OAuthAuthorization
if err = client.DoReq(req, &auth); err != nil {
return
}
if auth.AccessToken == nil {
return "", "", fmt.Errorf("access token missing from Heroku API login response")
}
return req.Host, auth.AccessToken.Token, nil
}
var cmdLogout = &Command{
Run: runLogout,
Usage: "logout",
Category: "emp",
Short: "log out of your Heroku account" + extra,
Long: `
Log out of your Heroku account and remove credentials from
this machine.
Example:
$ emp logout
Logged out.
`,
}
func runLogout(cmd *Command, args []string) {
if len(args) != 0 {
cmd.PrintUsage()
os.Exit(2)
}
u, err := url.Parse(client.URL)
if err != nil {
printFatal("couldn't parse client URL: " + err.Error())
}
nrc, err = hkclient.LoadNetRc()
if err != nil {
printError(err.Error())
}
err = removeCreds(strings.Split(u.Host, ":")[0])
if err != nil {
printFatal("saving new netrc: " + err.Error())
}
fmt.Println("Logged out.")
}