-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.go
109 lines (94 loc) · 2.59 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
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
package main
import (
"crypto/rand"
"crypto/rsa"
"encoding/base64"
"fmt"
"io/ioutil"
"math/big"
"net/http"
"net/http/cookiejar"
"net/url"
"strconv"
"strings"
)
const (
rStudioPubKeyURL = "http://localhost:8787/auth-public-key"
rStudioLoginURL = "http://localhost:8787/auth-do-sign-in"
userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0"
)
func getPubkey() (*rsa.PublicKey, error) {
req, err := http.NewRequest("GET", rStudioPubKeyURL, nil)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("Failed to get pubkey from %s", rStudioPubKeyURL)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
pubkey, err := parsePubkey(string(body))
if err != nil {
return nil, err
}
return pubkey, nil
}
// parsePubkey parses the string returned by RStudio server
// The string is like this: 010001:DB1E3A8360F...
func parsePubkey(pubkey string) (*rsa.PublicKey, error) {
s := strings.Split(pubkey, ":")
if len(s) != 2 {
return nil, fmt.Errorf("The format of the pubkey is wrong: %s", pubkey)
}
publicExponent, err := strconv.ParseInt(s[0], 16, 0)
if err != nil {
return nil, err
}
modulus := new(big.Int)
_, success := modulus.SetString(s[1], 16)
if !success {
return nil, fmt.Errorf("Failed to parse modulus: %s", s[1])
}
return &rsa.PublicKey{N: modulus, E: int(publicExponent)}, nil
}
func getLoginSessionCookie(username, password string, pubkey *rsa.PublicKey) (*http.Cookie, error) {
form := url.Values{}
form.Add("persist", "0")
form.Add("appUri", "")
form.Add("clientPath", "")
v, err := rsa.EncryptPKCS1v15(rand.Reader, pubkey, []byte(username+"\n"+password))
if err != nil {
return nil, err
}
form.Add("v", string(base64.StdEncoding.EncodeToString(v)))
req, err := http.NewRequest("POST", rStudioLoginURL, strings.NewReader(form.Encode()))
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", userAgent)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
jar, _ := cookiejar.New(nil)
client := http.Client{Jar: jar}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
localhostURL, _ := url.Parse("http://localhost")
cookies := jar.Cookies(localhostURL)
if len(cookies) != 1 {
return nil, fmt.Errorf("Unexpected Cookie: %#v", cookies)
}
loginSessionCookie := cookies[0]
if loginSessionCookie.Value == "" {
return nil, fmt.Errorf("Failed to login!")
}
return loginSessionCookie, nil
}