/
auth.go
162 lines (139 loc) · 3.84 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
package auth
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"path"
"strings"
oauth2ns "github.com/loomnetwork/oauth2-noserver"
"golang.org/x/oauth2"
"golang.org/x/oauth2/github"
"golang.org/x/oauth2/linkedin"
)
type LoginAuth struct {
Email string `json:"email,omitempty" form:"id"`
ApiKey string `json:"apikey,omitempty" form:"id"`
}
func Login(network string, loomnetworkHost string) string {
n := strings.ToLower(network)
var c *http.Client
if strings.Index(n, "linkedin") >= 0 {
c = loginLinkedIn()
} else if strings.Index(n, "github") >= 0 {
c = loginGithub()
} else {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("Please enter which network you want (linkedin/github): \n")
text, _ := reader.ReadString('\n')
return Login(text, loomnetworkHost)
}
if c == nil {
fmt.Printf("Failed logging into %s\n", network)
}
return validateLoomNetwork(loomnetworkHost, c, n)
//return extractGithubEmail(c, "")
}
type GithubEmail struct {
Email string `json:"email,omitempty"`
Verified bool `json:"verified,omitempty""`
Primary bool `json:"primary,omitempty""`
}
func extractGithubEmail(c *http.Client, auth string) string {
githubEmailURL := "https://api.github.com/user/emails"
req, err := http.NewRequest("GET", githubEmailURL, nil)
if auth != "" {
req.Header.Add("Authorization", auth)
}
resp, err := c.Do(req)
if err != nil {
fmt.Println("error:", err)
return ""
}
defer resp.Body.Close()
if resp.StatusCode != 200 { // OK
fmt.Printf("bad response code %d\n", resp.StatusCode)
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
var gemails []GithubEmail
err = json.Unmarshal(bodyBytes, &gemails)
if err != nil {
fmt.Println("error:", err)
}
if len(gemails) > 0 {
for _, email := range gemails {
if email.Verified == true && email.Primary == true {
return email.Email
}
}
}
return ""
}
func validateLoomNetwork(loomnetworkHost string, c *http.Client, network string) string {
u, err := url.Parse(loomnetworkHost)
u.Path = path.Join(u.Path, fmt.Sprintf("/login_oauth"))
req, err := http.NewRequest("POST", u.String(), nil)
req.Header.Add("Loom-Oauth-Provider", network)
req.Header.Add("accept", "application/json")
resp, err := c.Do(req)
defer resp.Body.Close()
if resp.StatusCode != 200 { // OK
fmt.Printf("bad response code %d\n", resp.StatusCode)
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
//TODO remove this
bodyString := string(bodyBytes)
fmt.Printf(bodyString)
var lauth LoginAuth
err = json.Unmarshal(bodyBytes, &lauth)
if err != nil {
fmt.Println("error:", err)
return ""
}
return lauth.ApiKey
}
func loginLinkedIn() *http.Client {
clientID := "86zs2w1g2j8hfu"
clientSecret := "mBd0gDHQdEwSRgt8"
scopes := []string{"r_emailaddress", "r_basicprofile"}
conf := &oauth2.Config{
ClientID: clientID, // also known as slient key sometimes
ClientSecret: clientSecret, // also known as secret key
Scopes: scopes,
Endpoint: linkedin.Endpoint,
}
r := oauth2ns.Authorize(conf)
// use client.Get / client.Post for further requests, the token will automatically be there
if len(r.Token.AccessToken) > 0 {
return r.Client
}
return nil
}
func loginGithub() *http.Client {
fmt.Printf("Attempting to login to Github\n")
clientID := "57bc10263596c4739845"
clientSecret := "952e625011098759a2f7ebc02e12c01cf1ef2e80"
scopes := []string{"user:email"}
conf := &oauth2.Config{
ClientID: clientID, // also known as slient key sometimes
ClientSecret: clientSecret, // also known as secret key
Scopes: scopes,
Endpoint: github.Endpoint,
}
r := oauth2ns.Authorize(conf)
// use client.Get / client.Post for further requests, the token will automatically be there
if len(r.Token.AccessToken) > 0 {
return r.Client
}
return nil
}