-
Notifications
You must be signed in to change notification settings - Fork 229
/
login.go
47 lines (40 loc) · 1.01 KB
/
login.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
// Package login handles login and authentication with Encore's platform.
package login
import (
"context"
"crypto/rand"
"encoding/base64"
"fmt"
"encr.dev/cli/internal/browser"
"encr.dev/cli/internal/platform"
"encr.dev/internal/conf"
"encr.dev/internal/env"
)
func DecideFlow() (*conf.Config, error) {
if env.IsSSH() || !browser.CanOpen() {
return DeviceAuth()
}
return Interactive()
}
func WithAuthKey(authKey string) (*conf.Config, error) {
params := &platform.ExchangeAuthKeyParams{
AuthKey: authKey,
}
resp, err := platform.ExchangeAuthKey(context.Background(), params)
if err != nil {
return nil, err
} else if resp.Token == nil {
return nil, fmt.Errorf("invalid response: missing token")
}
tok := resp.Token
conf := &conf.Config{Token: *tok, Actor: resp.Actor, AppSlug: resp.AppSlug}
return conf, nil
}
func genRandData() (string, error) {
data := make([]byte, 32)
_, err := rand.Read(data[:])
if err != nil {
return "", err
}
return base64.RawURLEncoding.EncodeToString(data), nil
}