-
Notifications
You must be signed in to change notification settings - Fork 1
/
fb-routes.go
144 lines (121 loc) · 3.52 KB
/
fb-routes.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
package main
import (
"encoding/json"
"net/http"
)
type fbRedirectResponse struct {
URL string `json:"url"`
}
func fbGetRedirectionURL(hc *fbHostConfig) handlerFuncType {
return func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(fbRedirectResponse{hc.getFbLoginRedirect()})
}
}
type fbSetUserRequest struct {
UserAccessToken string `json:"userAccessToken"`
}
func fbSetUserAccessToken(hc *fbHostConfig) handlerFuncType {
return func(w http.ResponseWriter, r *http.Request) {
var requestData fbSetUserRequest
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&requestData)
defer r.Body.Close()
if err != nil {
throwError(w, "Sent JSON body does is not of correct type", http.StatusBadRequest)
return
}
session, err := store.Get(r, DefaultSession)
if err != nil {
throwError(w, err.Error(), http.StatusInternalServerError)
return
}
currUserIDI := session.Values["userID"]
currUserID, ok := currUserIDI.(string)
if !ok {
throwError(w, "Insufficient rights", http.StatusForbidden)
return
}
err = hc.setUserAccessToken(requestData.UserAccessToken, currUserID)
if err != nil {
throwError(w, "User access does not seem to work: "+err.Error(), http.StatusExpectationFailed)
return
}
json.NewEncoder(w).Encode(hc.access)
}
}
func fbGetPages(hc *fbHostConfig) handlerFuncType {
return func(w http.ResponseWriter, r *http.Request) {
session, err := store.Get(r, DefaultSession)
if err != nil {
throwError(w, err.Error(), http.StatusInternalServerError)
return
}
currUserIDI := session.Values["userID"]
currUserID, ok := currUserIDI.(string)
if !ok || currUserID != hc.access.LocalUserID {
throwError(w, "Insufficient rights", http.StatusForbidden)
return
}
pages, err := hc.getPages()
if err != nil {
throwError(w, err.Error(), http.StatusExpectationFailed)
return
}
json.NewEncoder(w).Encode(pages)
}
}
type accessResponse struct {
LocalUserID string `json:"localUserId"`
PageName string `json:"pageName"`
PageID string `json:"pageId"`
}
func fbGetAccess(hc *fbHostConfig) handlerFuncType {
return func(w http.ResponseWriter, r *http.Request) {
res := accessResponse{
hc.access.LocalUserID,
hc.access.PageName,
hc.access.PageID,
}
json.NewEncoder(w).Encode(res)
}
}
type fbSetPageRequest struct {
ID string `json:"id"`
Name string `json:"name"`
AccessToken string `json:"accessToken"`
}
func fbSetPage(hc *fbHostConfig) handlerFuncType {
return func(w http.ResponseWriter, r *http.Request) {
session, err := store.Get(r, DefaultSession)
if err != nil {
throwError(w, err.Error(), http.StatusInternalServerError)
return
}
currUserIDI := session.Values["userID"]
currUserID, ok := currUserIDI.(string)
if !ok || currUserID != hc.access.LocalUserID {
throwError(w, "Insufficient rights", http.StatusForbidden)
return
}
var requestData fbSetPageRequest
decoder := json.NewDecoder(r.Body)
err = decoder.Decode(&requestData)
defer r.Body.Close()
if err != nil {
throwError(w, "Sent JSON body does is not of correct type", http.StatusBadRequest)
return
}
err = hc.setPage(requestData.ID, requestData.Name, requestData.AccessToken)
if err != nil {
throwError(w, "Page access does not seem to work: "+err.Error(), http.StatusExpectationFailed)
return
}
json.NewEncoder(w).Encode(hc.access)
}
}
func fbClear(hc *fbHostConfig) handlerFuncType {
return func(w http.ResponseWriter, r *http.Request) {
hc.clear()
json.NewEncoder(w).Encode(struct{}{})
}
}