-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogin.go
More file actions
68 lines (59 loc) · 1.88 KB
/
login.go
File metadata and controls
68 lines (59 loc) · 1.88 KB
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
package api
import (
"github.com/julienschmidt/httprouter"
"net/http"
"github.com/devlucky/maporable-api/models"
"encoding/json"
"log"
"fmt"
"github.com/devlucky/maporable-api/config"
"net/url"
"strings"
"golang.org/x/oauth2"
"io/ioutil"
)
func Login(w http.ResponseWriter, r *http.Request, ps httprouter.Params, a *config.Config) {
Url, err := url.Parse(a.FacebookOAuth.Endpoint.AuthURL)
if err != nil {
log.Fatal("Parse: ", err)
}
parameters := url.Values{}
parameters.Add("client_id", a.FacebookOAuth.ClientID)
parameters.Add("scope", strings.Join(a.FacebookOAuth.Scopes, " "))
parameters.Add("redirect_uri", a.FacebookOAuth.RedirectURL)
parameters.Add("response_type", "code")
parameters.Add("state", a.FacebookOAuthState)
Url.RawQuery = parameters.Encode()
http.Redirect(w, r, Url.String(), http.StatusTemporaryRedirect)
}
func LoginWithFacebook(w http.ResponseWriter, r *http.Request, ps httprouter.Params, a *config.Config) {
state := r.FormValue("state")
if state != a.FacebookOAuthState {
log.Printf("invalid oauth state, expected '%s', got '%s'\n", a.FacebookOAuthState, state)
w.WriteHeader(http.StatusBadRequest)
return
}
code := r.FormValue("code")
token, err := a.FacebookOAuth.Exchange(oauth2.NoContext, code)
if err != nil {
fmt.Printf("oauthConf.Exchange() failed with '%s'\n", err)
w.WriteHeader(http.StatusBadRequest)
return
}
resp, err := http.Get("https://graph.facebook.com/me?access_token=" +
url.QueryEscape(token.AccessToken))
if err != nil {
fmt.Printf("Get: %s\n", err)
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
return
}
defer resp.Body.Close()
response, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("ReadAll: %s\n", err)
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
return
}
log.Printf("parseResponseBody: %s\n", string(response))
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
}