-
Notifications
You must be signed in to change notification settings - Fork 0
/
linenotify.go
160 lines (130 loc) · 4.49 KB
/
linenotify.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package linenotify
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os/exec"
"strings"
"runtime"
)
const (
LINE_AUTH_PATH = "https://notify-bot.line.me/oauth/authorize"
LINE_TOKEN_PATH = "https://notify-bot.line.me/oauth/token"
NOTIFY_HOST = "https://notify-api.line.me/api/notify"
)
type Client struct {
id string
secret string
callback *url.URL
token string
done chan error
}
func New(id, secret, callback string) (*Client, error) {
if id == "" || secret == "" {
return nil, fmt.Errorf("client id and secret can not be empty")
}
cb, err := url.Parse(callback)
if err != nil {
return nil, fmt.Errorf("fail to pars callback url with err: %v", err)
}
c := &Client{
id: id,
secret: secret,
callback: cb,
token: "",
done: make(chan error),
}
return c, nil
}
func (c *Client) Login() (string, error) {
if err := openbrowser(c.AuthUrl()); err != nil {
return "", fmt.Errorf("fail to open browser, %v", err)
}
if err := c.waitForCallback(); err != nil {
return "", fmt.Errorf("fail to start server waiting for callback code to exchange access toekn: %v", err)
}
return c.token, nil
}
func (c *Client) AuthUrl() string {
values := url.Values{}
values.Add("client_id", c.id)
values.Add("response_type", "code")
values.Add("scope", "notify")
values.Add("redirect_uri", c.callback.String())
values.Add("state", "123")
query := values.Encode()
url := LINE_AUTH_PATH + "?" + query
return url
}
func (c *Client) makeTokenBody(code string) string {
values := url.Values{}
values.Add("client_id", c.id)
values.Add("Client_secret", c.secret)
values.Add("redirect_uri", c.callback.String())
values.Add("code", code)
values.Add("grant_type", "authorization_code")
body := values.Encode()
return body
}
func (c *Client) GetToken(code string) (string, error) {
body := c.makeTokenBody(code)
req, _ := http.NewRequest("POST", LINE_TOKEN_PATH, strings.NewReader(body))
req.Header.Add("Content-type", "application/x-www-form-urlencoded")
res, err := http.DefaultClient.Do(req)
if err != nil {
return "", fmt.Errorf("fail to get token with err: %v", err)
}
defer res.Body.Close()
data, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", fmt.Errorf("fail to read response body with err %v", err)
}
var buf struct {
Access_token string `json:access_token`
Status int `json:status`
Message string `json:message`
}
if err := json.Unmarshal(data, &buf); err != nil {
return "", fmt.Errorf("fail to json parse body with err %v", err)
}
if buf.Status >= 400 {
return "", fmt.Errorf("fail to get token with status: %v and message: %s", buf.Status, buf.Message)
}
return buf.Access_token, nil
}
func (c *Client) handler(w http.ResponseWriter, r *http.Request) {
code := r.URL.Query().Get("code")
token, err := c.GetToken(code)
if err != nil {
c.done <- err
return
}
w.Write([]byte(token))
c.token = token
c.done <- nil
}
func (c *Client) waitForCallback() error {
server := &http.Server{
Addr: c.callback.Host,
Handler: http.HandlerFunc(c.handler),
}
go server.ListenAndServe()
defer server.Close()
return <-c.done
}
func openbrowser(url string) error {
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
return err
}