2015-03-06 10:23:13 +00:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2015-03-06 13:23:01 +00:00
|
|
|
"log"
|
2015-03-06 10:23:13 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2015-03-06 13:23:01 +00:00
|
|
|
"time"
|
2015-03-06 10:23:13 +00:00
|
|
|
|
|
|
|
"github.com/localhots/empact/config"
|
|
|
|
"github.com/localhots/empact/db"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Authenticate(code string) (token, login string, err error) {
|
2015-03-21 14:19:17 +00:00
|
|
|
defer report(time.Now(), "Authenticate")
|
2015-03-06 10:23:13 +00:00
|
|
|
if token, err = FetchAccessToken(code); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-03-06 13:23:01 +00:00
|
|
|
log.Printf("Got token %q for code %q\n", token, code)
|
2015-03-06 10:23:13 +00:00
|
|
|
|
|
|
|
var user *db.User
|
|
|
|
if user, err = FetchUserInfoWithToken(token); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
login = user.Login
|
2015-03-06 13:23:01 +00:00
|
|
|
log.Println("Saving user", user)
|
2015-03-21 14:18:41 +00:00
|
|
|
db.Queue(func() { user.Save() })
|
2015-03-06 10:23:13 +00:00
|
|
|
|
|
|
|
tok := &db.Token{
|
|
|
|
User: login,
|
|
|
|
Token: token,
|
|
|
|
}
|
2015-03-06 13:23:01 +00:00
|
|
|
log.Println("Saving token", tok)
|
2015-03-21 14:18:41 +00:00
|
|
|
db.Queue(func() { tok.Save() })
|
2015-03-06 10:23:13 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func FetchAccessToken(code string) (token string, err error) {
|
2015-03-21 14:19:17 +00:00
|
|
|
defer report(time.Now(), "FetchAccessToken")
|
2015-03-06 10:23:13 +00:00
|
|
|
payload := url.Values{}
|
|
|
|
payload.Set("client_id", config.C().ClientID)
|
|
|
|
payload.Set("client_secret", config.C().ClientSecret)
|
|
|
|
payload.Set("code", code)
|
|
|
|
payload.Set("redirect_uri", config.C().RedirectURI)
|
|
|
|
|
2015-03-06 13:23:01 +00:00
|
|
|
log.Printf("Requesting token for code %q", code)
|
2015-03-06 10:23:13 +00:00
|
|
|
buf := bytes.NewBuffer([]byte(payload.Encode()))
|
|
|
|
var resp *http.Response
|
|
|
|
if resp, err = http.Post(config.C().AccessTokenURL, "application/x-www-form-urlencoded", buf); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
var body []byte
|
|
|
|
if body, err = ioutil.ReadAll(resp.Body); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pairs, _ := url.ParseQuery(string(body))
|
|
|
|
if token = pairs.Get("access_token"); token == "" {
|
|
|
|
err = fmt.Errorf("Failed to fetch access token usign code %q: %s", code, pairs.Get("error_description"))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func FetchUserInfoWithToken(token string) (u *db.User, err error) {
|
2015-03-21 14:19:17 +00:00
|
|
|
defer report(time.Now(), "FetchUserInfoWithToken")
|
2015-03-06 10:23:13 +00:00
|
|
|
var resp *http.Response
|
|
|
|
if resp, err = http.Get("https://api.github.com/user?access_token=" + token); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
var body []byte
|
|
|
|
if body, err = ioutil.ReadAll(resp.Body); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
u = &db.User{}
|
|
|
|
err = json.Unmarshal(body, &u)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|