-
Notifications
You must be signed in to change notification settings - Fork 0
/
authfacebook.go
68 lines (62 loc) · 1.82 KB
/
authfacebook.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
package app
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"github.com/hairizuanbinnoorazman/techmeetup/logger"
)
type FacebookAuthorize struct {
logger logger.Logger
clientID string
redirectURI string
}
func (f FacebookAuthorize) ServeHTTP(w http.ResponseWriter, r *http.Request) {
authorizeURL, _ := url.ParseRequestURI("https://www.facebook.com/v8.0/dialog/oauth")
query := authorizeURL.Query()
query.Add("client_id", f.clientID)
query.Add("redirect_uri", f.redirectURI)
authorizeURL.RawQuery = query.Encode()
http.Redirect(w, r, authorizeURL.String(), http.StatusPermanentRedirect)
}
type FacebookAccess struct {
client *http.Client
logger logger.Logger
clientID string
clientSecret string
redirectURI string
}
func (f FacebookAccess) ServeHTTP(w http.ResponseWriter, r *http.Request) {
code := r.URL.Query().Get("code")
if code == "" {
w.WriteHeader(http.StatusUnauthorized)
return
}
accessURL, _ := url.ParseRequestURI("https://graph.facebook.com/v8.0/oauth/access_token")
accessReqBody := url.Values{}
accessReqBody["client_id"] = []string{f.clientID}
accessReqBody["client_secret"] = []string{f.clientSecret}
accessReqBody["redirect_uri"] = []string{f.redirectURI}
accessReqBody["code"] = []string{code}
resp, err := f.client.PostForm(accessURL.String(), accessReqBody)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
type authAccessResp struct {
AccessToken string `json:"access_token"`
RefereshToken string `json:"refresh_token"`
}
rawAuthAccessResp, err := ioutil.ReadAll(resp.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
var a authAccessResp
err = json.Unmarshal(rawAuthAccessResp, &a)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
f.logger.Infof("Response: %v", a)
}