/
oauth.go
351 lines (319 loc) · 10.9 KB
/
oauth.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package dragonformula
import (
"appengine"
"appengine/datastore"
"appengine/urlfetch"
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"fmt"
"github.com/clbanning/mxj"
"github.com/go-martini/martini"
"github.com/martini-contrib/render"
"github.com/martini-contrib/sessions"
"github.com/mrjones/oauth"
"net/http"
"net/url"
"sort"
"strconv"
"strings"
"time"
)
// var TWITTER_CONSUMER_KEY = "efelQ7UNWZGge5H1p7OMRl0lR"
// var TWITTER_CALLBACK_URL = "http://stg-aqua-teacher.appspot.com/api/twitter/callback"
// var TWITTER_CONSUMER_SECRET = "51UF3rlpJWLDwVhP8lsr0fawd0y7xksmatAyln6ANtgZz3kv8s"
var TWITTER_CONSUMER_KEY = "tFc1luf519X71ihzPS6Mohibn"
var TWITTER_CALLBACK_URL = "http://aqua-teacher.appspot.com/api/twitter/callback"
var TWITTER_CONSUMER_SECRET = "KD84L4SqwdrxOiw6T2DTw4TuzcefWqdZCRCGXhxWCmj3aDSPFy"
var TWITTER_SERVER = oauth.ServiceProvider{
RequestTokenUrl: "http://api.twitter.com/oauth/request_token",
AuthorizeTokenUrl: "https://api.twitter.com/oauth/authorize",
AccessTokenUrl: "https://api.twitter.com/oauth/access_token",
}
var consumer = oauth.NewConsumer(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, TWITTER_SERVER)
// Twitter ボタンが押された時の処理
func LoginTwitter(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
oauth := NewOAuth1(c, fmt.Sprintf("http://aqua-teacher.appspot.com/"))
result := oauth.RequestToken("https://api.twitter.com/oauth/request_token")
oauth.Authenticate(w, r, "https://api.twitter.com/oauth/authenticate", result["oauth_token"])
}
func LoginUser(r render.Render, req *http.Request, session sessions.Session) {
accessToken := GetAccessToken(session)
if accessToken != nil {
result := GetTwitterUser(req, accessToken)
if result == nil {
r.JSON(400, "")
}
r.JSON(200, result)
return
} else {
r.JSON(400, "")
}
}
func GetTwitterUser(req *http.Request, accessToken *oauth.AccessToken) mxj.Map {
c := appengine.NewContext(req)
consumer.HttpClient = urlfetch.Client(c)
response, _ := consumer.Get("https://api.twitter.com/1.1/account/verify_credentials.json", nil, accessToken)
result := make([]byte, 1024*1024)
response.Body.Read(result)
resultString := string(result)
trimStr := strings.Trim(resultString, "\x00")
if trimStr == "" {
return nil
}
accountInfo, _ := mxj.NewMapJson([]byte(trimStr))
return accountInfo
}
/**
* Twitterからのコールバックを受け取って、セッションに詰める。ユーザの登録・更新を行う。
**/
func CallbackTwitter(r render.Render, w http.ResponseWriter, req *http.Request, session sessions.Session) {
c := appengine.NewContext(req)
token := req.FormValue("oauth_token")
verifier := req.FormValue("oauth_verifier")
oauth := NewOAuth1(c, TWITTER_CALLBACK_URL)
accessToken := oauth.ExchangeToken(token, verifier, "https://api.twitter.com/oauth/access_token")
if accessToken != nil { // ログイン成功
// セッション開始
session.Set("accessToken", accessToken.Token)
session.Set("accessTokenSecret", accessToken.Secret)
c.Infof("toke")
c.Infof(accessToken.Token)
c.Infof("secret")
c.Infof(accessToken.Secret)
consumer.HttpClient = urlfetch.Client(c)
response, _ := consumer.Get("https://api.twitter.com/1.1/account/verify_credentials.json", nil, accessToken)
result := make([]byte, 1024*1024)
response.Body.Read(result)
resultString := string(result)
trimStr := strings.Trim(resultString, "\x00")
accountInfo, _ := mxj.NewMapJson([]byte(trimStr))
// User登録
if accountInfo["screen_name"] != nil {
screenName := accountInfo["screen_name"].(string)
user := &User{}
user.Id = screenName
user.Key = screenName
user.Token = accessToken.Token
key := datastore.NewKey(c, "User", screenName, 0, nil)
key, err := datastore.Put(c, key, user)
if err != nil {
c.Criticalf("%s", err)
} else {
c.Infof("")
}
}
// TODO トップページへリダイレクト
r.Redirect("/")
} else { // ログイン失敗
r.Redirect("/")
}
}
/**
* OAuth1.0aの通信を行うクラス
* @class
* @param {map[string]string} params oauthパラメータの配列
* @param {appengine.Context} context コンテキスト
*/
type OAuth1 struct {
params map[string]string
context appengine.Context
}
/**
* OAuthクラスのインスタンス化
* @function
* @params {appengine.Context} c コンテキスト
* @params{appengine.Context} callback コールバックURL
* @returns {*OAuth} OAuthインスタンス
*/
func NewOAuth1(c appengine.Context, callback string) *OAuth1 {
oauth := new(OAuth1)
oauth.context = c
return oauth
}
/**
* Twitter へリクエストトークンを要求する
* @method
* @memberof OAuth1
* @param {string} targetUrl リクエスト要求先のURL
* @returns {map[string]string} リクエスト結果
*/
func (this *OAuth1) RequestToken(targetUrl string) map[string]string {
params := make(map[string]string, 0)
params["oauth_callback"] = TWITTER_CALLBACK_URL
response := this.Request("POST", targetUrl, make(map[string]string), "", []string{TWITTER_CONSUMER_SECRET, ""})
datas := strings.Split(response, "&")
result := make(map[string]string, len(datas))
for i := 0; i < len(datas); i++ {
data := strings.Split(datas[i], "=")
result[data[0]] = data[1]
}
return result
}
/**
* リクエストを送信してレスポンスを受信する
* メソッドは POST 固定
* @method
* @memberof OAuth1
* @param {string} method POSTかGET
* @param {string} targetUrl 送信先
* @param {string} params パラメータ
* @param {string} body リクエストボディ
* @param {string} secret 暗号鍵(ConsumerSecret と OAuth Token Secret)
* @returns {string} レスポンス
*/
func (this *OAuth1) Request(method string, targetUrl string, params map[string]string, body string, secret []string) string {
oauthParams := make(map[string]string, 0)
for key, val := range params {
oauthParams[key] = val
}
oauthParams["oauth_consumer_key"] = TWITTER_CONSUMER_KEY
oauthParams["oauth_signature_method"] = "HMAC-SHA1"
oauthParams["oauth_version"] = "1.0"
oauthParams["oauth_nonce"] = this.CreateNonce()
oauthParams["oauth_timestamp"] = strconv.Itoa(int(time.Now().Unix()))
oauthParams["oauth_signature"] = this.CreateSignature(method, targetUrl, oauthParams, secret)
for key, val := range params {
oauthParams[key] = val
}
// リクエスト送信
httpParams := make(map[string]string, 1)
httpParams["Authorization"] = this.CreateHeader(oauthParams)
var response *http.Response
if method == "GET" {
query := make(map[string]string, 0)
for key, val := range params {
if key != "oauth_token" {
query[key] = val
}
}
response = Get(this.context, targetUrl, query, httpParams)
} else {
response = Request(this.context, method, targetUrl, httpParams, body)
}
// レスポンスボディの読み取り
result := make([]byte, 1024*1024)
response.Body.Read(result)
resultString := string(result)
resultString = strings.Trim(resultString, "\x00")
return resultString
}
func GetTwitterUserById(r render.Render, params martini.Params, req *http.Request) {
c := appengine.NewContext(req)
oauth := NewOAuth1(c, TWITTER_CALLBACK_URL)
url := "https://api.twitter.com/1.1/users/show.json"
parameters := map[string]string{"screen_name": params["userId"]}
secret := []string{TWITTER_CONSUMER_SECRET, ""}
response := oauth.Request("GET", url, parameters, "", secret)
user, _ := mxj.NewMapJson([]byte(response))
r.JSON(200, user)
}
/**
* oauth_nonce を作成する
* @method
* @memberof OAuth1
* @returns {string} 作成したoauth_nonce
*/
func (this *OAuth1) CreateNonce() string {
nonce := ""
for i := 0; i < 4; i++ {
nonce = strings.Join([]string{nonce, string(GetRandomizedString())}, "")
}
return nonce
}
/**
* Aouthorization ヘッダを作成する
* @method
* @memberof OAuth1
* @param {map[string]string} oauthParams OAuthパラメータ
* @returns {string} ヘッダ
*/
func (this *OAuth1) CreateHeader(oauthParams map[string]string) string {
headerParams := make([]string, 0)
for key, val := range oauthParams {
if key == "screen_name" {
continue
}
key = url.QueryEscape(key)
val = url.QueryEscape(val)
set := fmt.Sprintf(`%s="%s"`, key, val)
headerParams = append(headerParams, set)
}
header := strings.Join(headerParams, ", ")
header = fmt.Sprintf("OAuth %s", header)
return header
}
/**
* oauth_signature を作成する
* @method
* @memberof OAuth1
* @param {string} method メソッド
* @param {string} targetUrl リクエスト送信先のURL
* @param {map[string]string} oauthParams パラメータ
* @param {string} secret 暗号化鍵(Consumer Secret と OAuth Token Secret)
* @returns {string} oauth_signature
*/
func (this *OAuth1) CreateSignature(method string, targetUrl string, oauthParams map[string]string, secret []string) string {
keys := make([]string, 0)
for key := range oauthParams {
keys = append(keys, key)
}
sort.Strings(keys)
params := make([]string, len(keys))
for i := 0; i < len(keys); i++ {
key := keys[i]
val := oauthParams[key]
params[i] = fmt.Sprintf("%s=%s", url.QueryEscape(key), url.QueryEscape(val))
}
paramString := strings.Join(params, "&")
baseString := fmt.Sprintf("%s&%s&%s", method, url.QueryEscape(targetUrl), url.QueryEscape(paramString))
signatureKey := fmt.Sprintf("%s&%s", url.QueryEscape(secret[0]), url.QueryEscape(secret[1]))
hash := hmac.New(sha1.New, []byte(signatureKey))
hash.Write([]byte(baseString))
signature := hash.Sum(nil)
return base64.StdEncoding.EncodeToString(signature)
}
/**
* 認証ページヘリダイレクトする
* @memberof OAuth1
* @method
* @param {http.ResponseWriter} w 応答先
* @param {*http.Request} r リクエスト
* @param {string} targetUrl リダイレクト先
* @param {string} token 未認証リクエストトークン
*/
func (this *OAuth1) Authenticate(w http.ResponseWriter, r *http.Request, targetUrl string, token string) {
to := fmt.Sprintf("?oauth_token=%s", token)
to = strings.Join([]string{targetUrl, to}, "")
http.Redirect(w, r, to, 302)
}
/**
* リクエストトークンをアクセストークンに変換する
* @memberof OAuth1
* @method
* @param {string} token リクエストトークン
* @param {string} verifier 認証データ
* @param {string} targetUrl リクエストの送信先
* @returns {oauth.AccessToken} アクセストークン
*/
func (this *OAuth1) ExchangeToken(token string, verifier string, targetUrl string) *oauth.AccessToken {
requestToken := &oauth.RequestToken{Token: token}
consumer.HttpClient = urlfetch.Client(this.context)
accessToken, err := consumer.AuthorizeToken(requestToken, verifier)
if err != nil {
return nil
}
return accessToken
}
func GetAccessToken(session sessions.Session) *oauth.AccessToken {
token := session.Get("accessToken")
secret := session.Get("accessTokenSecret")
if token == nil || token == "" || secret == nil || secret == "" {
return nil
}
return &oauth.AccessToken{
Token: token.(string),
Secret: secret.(string),
}
}