-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathmain.go
99 lines (85 loc) · 2.5 KB
/
main.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
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"log"
"net/http"
"net/url"
"os"
"html/template"
)
var clientID string
var clientSecret string
var callbackURL string
var token string
func main() {
http.HandleFunc("/callback", callbackHandler)
http.HandleFunc("/notify", notifyHandler)
http.HandleFunc("/auth", authHandler)
clientID = os.Getenv("ClientID")
clientSecret = os.Getenv("ClientSecret")
callbackURL = os.Getenv("CallbackURL")
port := os.Getenv("PORT")
fmt.Printf("ENV port:%s, cid:%s csecret:%s\n", port, clientID, clientSecret)
addr := fmt.Sprintf(":%s", port)
http.ListenAndServe(addr, nil)
}
func notifyHandler(w http.ResponseWriter, r *http.Request) {
r.ParseForm() // Populates request.Form
msg := r.Form.Get("msg")
fmt.Printf("Get msg=%s\n", msg)
data := url.Values{}
data.Add("message", msg)
byt, err := apiCall("POST", apiNotify, data, token)
fmt.Println("ret:", string(byt), " err:", err)
res := newTokenResponse(byt)
fmt.Println("result:", res)
token = res.AccessToken
w.Write(byt)
}
func callbackHandler(w http.ResponseWriter, r *http.Request) {
r.ParseForm() // Populates request.Form
code := r.Form.Get("code")
state := r.Form.Get("state")
fmt.Printf("Get code=%s, state=%s \n", code, state)
data := url.Values{}
data.Add("grant_type", "authorization_code")
data.Add("code", code)
data.Add("redirect_uri", callbackURL)
data.Add("client_id", clientID)
data.Add("client_secret", clientSecret)
byt, err := apiCall("POST", apiToken, data, "")
fmt.Println("ret:", string(byt), " err:", err)
res := newTokenResponse(byt)
fmt.Println("result:", res)
token = res.AccessToken
w.Write(byt)
}
func authHandler(w http.ResponseWriter, r *http.Request) {
check := func(err error) {
if err != nil {
log.Fatal(err)
}
}
t, err := template.New("webpage").Parse(authTmpl)
check(err)
noItems := struct {
ClientID string
CallbackURL string
}{
ClientID: clientID,
CallbackURL: callbackURL,
}
err = t.Execute(w, noItems)
check(err)
}