/
hello.go
155 lines (139 loc) · 3.78 KB
/
hello.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
package hello
import (
"fmt"
"html/template"
"net/http"
"os"
"sync"
"github.com/ChimeraCoder/anaconda"
"github.com/mrjones/oauth"
"golang.org/x/net/context"
"google.golang.org/appengine"
"google.golang.org/appengine/log"
"google.golang.org/appengine/urlfetch"
)
var (
TwitterConsumerKey string
TwitterConsumerSecret string
AuthCallbackUrl string
)
func init() {
TwitterConsumerKey = os.Getenv("TWITTER_CONSUMER_KEY")
TwitterConsumerSecret = os.Getenv("TWITTER_CONSUMER_SECRET")
AuthCallbackUrlBase := os.Getenv("TWITTER_CALLBACK_URL_BASE")
AuthCallbackUrlRel := "/auth/callback"
AuthCallbackUrl = fmt.Sprint(AuthCallbackUrlBase, AuthCallbackUrlRel)
http.HandleFunc("/", rootHandler)
http.HandleFunc("/auth", authHandler)
http.HandleFunc(AuthCallbackUrlRel, authCallbackHandler)
http.HandleFunc("/omikuji", omikujiHandler)
}
var rootTemplate = template.Must(template.New("root").Parse(`
<!Doctype HTML>
<html>
<head>
<meta charset=utf-8>
<title></title>
</head>
<body>
<a href="/auth">ツイッターログイン</a>
<a href="/omikuji">おみくじ</a>
</body>
</html>
`))
func rootHandler(w http.ResponseWriter, r *http.Request) {
rootTemplate.Execute(w, nil)
}
func init() {
anaconda.SetConsumerKey(TwitterConsumerKey)
anaconda.SetConsumerSecret(TwitterConsumerSecret)
}
var omikujiTemplate = template.Must(template.New("omikuji").Parse(`
<!Doctype HTML>
<html>
<head>
<meta charset=utf-8>
<title></title>
</head>
<body>
大吉です!
</body>
</html>
`))
func omikujiHandler(w http.ResponseWriter, r *http.Request) {
omikujiTemplate.Execute(w, nil)
}
var (
RequestTokenMu sync.Mutex
RequestTokens []oauth.RequestToken
)
func AuthRequest(rtoken oauth.RequestToken) {
RequestTokenMu.Lock()
defer RequestTokenMu.Unlock()
RequestTokens = append(RequestTokens, rtoken)
}
func FindRequest(ctx context.Context, token string) *oauth.RequestToken {
RequestTokenMu.Lock()
defer RequestTokenMu.Unlock()
for _, rtoken := range RequestTokens {
log.Debugf(ctx, "token:%v", rtoken.Token)
if rtoken.Token == token {
r := rtoken
return &r
}
}
return nil
}
func NewTwitterClient(ctx context.Context) *oauth.Consumer {
httpClient := urlfetch.Client(ctx)
c := oauth.NewCustomHttpClientConsumer(
TwitterConsumerKey, TwitterConsumerSecret,
oauth.ServiceProvider{
RequestTokenUrl: "https://api.twitter.com/oauth/request_token",
AuthorizeTokenUrl: "https://api.twitter.com/oauth/authorize",
AccessTokenUrl: "https://api.twitter.com/oauth/access_token",
}, httpClient)
return c
}
func authHandler(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
c := NewTwitterClient(ctx)
requestToken, u, err := c.GetRequestTokenAndUrl(AuthCallbackUrl)
log.Debugf(ctx, "requestToken:%v,%v callbackUrl:%v", requestToken.Token, requestToken.Secret, AuthCallbackUrl)
if err != nil {
fmt.Fprint(w, err)
w.WriteHeader(500)
return
}
AuthRequest(*requestToken)
http.Redirect(w, r, u, http.StatusFound)
}
func authCallbackHandler(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
c := NewTwitterClient(ctx)
token := r.FormValue("oauth_token")
verificationCode := r.FormValue("oauth_verifier")
log.Debugf(ctx, "oauth_token:%v oauth_verifier:%v", token, verificationCode)
requestToken := FindRequest(ctx, token)
if requestToken == nil {
fmt.Fprint(w, "token not found")
w.WriteHeader(500)
return
}
accessToken, err := c.AuthorizeToken(requestToken, verificationCode)
if err != nil {
fmt.Fprint(w, err)
w.WriteHeader(500)
return
}
fmt.Fprintln(w, accessToken.AdditionalData)
api := anaconda.NewTwitterApi(accessToken.Token, accessToken.Secret)
api.HttpClient = urlfetch.Client(ctx)
ok, err := api.VerifyCredentials()
if err != nil {
fmt.Fprint(w, err)
w.WriteHeader(500)
return
}
fmt.Fprintln(w, ok)
}