forked from mattn/webapi-vim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoauth.vim
More file actions
162 lines (155 loc) · 6.2 KB
/
oauth.vim
File metadata and controls
162 lines (155 loc) · 6.2 KB
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
" oauth
" Last Change: 2010-09-10
" Maintainer: Yasuhiro Matsumoto <[email protected]>
" License: This file is placed in the public domain.
" Reference:
" http://tools.ietf.org/rfc/rfc5849.txt
let s:save_cpo = &cpo
set cpo&vim
function! webapi#oauth#request_token(url, ctx, ...) abort
let params = a:0 > 0 ? a:000[0] : {}
let query = {}
let time_stamp = localtime()
let nonce = time_stamp . " " . time_stamp
let nonce = webapi#sha1#sha1(nonce)[0:28]
let query["oauth_consumer_key"] = a:ctx.consumer_key
let query["oauth_nonce"] = nonce
let query["oauth_request_method"] = "POST"
let query["oauth_signature_method"] = "HMAC-SHA1"
let query["oauth_timestamp"] = time_stamp
let query["oauth_version"] = "1.0"
for key in keys(params)
let query[key] = params[key]
endfor
let query_string = "POST&"
let query_string .= webapi#http#encodeURI(a:url)
let query_string .= "&"
let query_string .= webapi#http#encodeURI(webapi#http#encodeURI(query))
let hmacsha1 = webapi#hmac#sha1(webapi#http#encodeURI(a:ctx.consumer_secret) . "&", query_string)
let query["oauth_signature"] = webapi#base64#b64encodebin(hmacsha1)
let res = webapi#http#post(a:url, query, {})
let a:ctx.request_token = webapi#http#decodeURI(substitute(filter(split(res.content, "&"), "v:val =~ '^oauth_token='")[0], '^[^=]*=', '', ''))
let a:ctx.request_token_secret = webapi#http#decodeURI(substitute(filter(split(res.content, "&"), "v:val =~ '^oauth_token_secret='")[0], '^[^=]*=', '', ''))
return a:ctx
endfunction
function! webapi#oauth#access_token(url, ctx, ...) abort
let params = a:0 > 0 ? a:000[0] : {}
let query = {}
let time_stamp = localtime()
let nonce = time_stamp . " " . time_stamp
let nonce = webapi#sha1#sha1(nonce)[0:28]
let query["oauth_consumer_key"] = a:ctx.consumer_key
let query["oauth_nonce"] = nonce
let query["oauth_request_method"] = "POST"
let query["oauth_signature_method"] = "HMAC-SHA1"
let query["oauth_timestamp"] = time_stamp
let query["oauth_token"] = a:ctx.request_token
let query["oauth_token_secret"] = a:ctx.request_token_secret
let query["oauth_version"] = "1.0"
for key in keys(params)
let query[key] = params[key]
endfor
let query_string = "POST&"
let query_string .= webapi#http#encodeURI(a:url)
let query_string .= "&"
let query_string .= webapi#http#encodeURI(webapi#http#encodeURI(query))
let hmacsha1 = webapi#hmac#sha1(webapi#http#encodeURI(a:ctx.consumer_secret) . "&" . webapi#http#encodeURI(a:ctx.request_token_secret), query_string)
let query["oauth_signature"] = webapi#base64#b64encodebin(hmacsha1)
let res = webapi#http#post(a:url, query, {})
let a:ctx.access_token = webapi#http#decodeURI(substitute(filter(split(res.content, "&"), "v:val =~ '^oauth_token='")[0], '^[^=]*=', '', ''))
let a:ctx.access_token_secret = webapi#http#decodeURI(substitute(filter(split(res.content, "&"), "v:val =~ '^oauth_token_secret='")[0], '^[^=]*=', '', ''))
return a:ctx
endfunction
function! webapi#oauth#get(url, ctx, ...) abort
let params = a:0 > 0 ? a:000[0] : {}
let getdata = a:0 > 1 ? a:000[1] : {}
let headdata = a:0 > 2 ? a:000[2] : {}
let query = {}
let time_stamp = localtime()
let nonce = time_stamp . " " . time_stamp
let nonce = webapi#sha1#sha1(nonce)[0:28]
let query["oauth_consumer_key"] = a:ctx.consumer_key
let query["oauth_nonce"] = nonce
let query["oauth_request_method"] = "GET"
let query["oauth_signature_method"] = "HMAC-SHA1"
let query["oauth_timestamp"] = time_stamp
let query["oauth_token"] = a:ctx.access_token
let query["oauth_version"] = "1.0"
if type(params) == 4
for key in keys(params)
let query[key] = params[key]
endfor
endif
if type(getdata) == 4
for key in keys(getdata)
let query[key] = getdata[key]
endfor
endif
let query_string = query["oauth_request_method"] . "&"
let query_string .= webapi#http#encodeURI(a:url)
let query_string .= "&"
let query_string .= webapi#http#encodeURI(webapi#http#encodeURI(query))
let hmacsha1 = webapi#hmac#sha1(webapi#http#encodeURI(a:ctx.consumer_secret) . "&" . webapi#http#encodeURI(a:ctx.access_token_secret), query_string)
let query["oauth_signature"] = webapi#base64#b64encodebin(hmacsha1)
if type(getdata) == 4
for key in keys(getdata)
call remove(query, key)
endfor
endif
let auth = 'OAuth '
for key in sort(keys(query))
let auth .= key . '="' . webapi#http#encodeURI(query[key]) . '", '
endfor
let auth = auth[:-3]
let headdata["Authorization"] = auth
let res = webapi#http#get(a:url, getdata, headdata)
return res
endfunction
function! webapi#oauth#post(url, ctx, ...) abort
let params = a:0 > 0 ? a:000[0] : {}
let postdata = a:0 > 1 ? a:000[1] : {}
let headdata = a:0 > 2 ? a:000[2] : {}
let query = {}
let time_stamp = localtime()
let nonce = time_stamp . " " . time_stamp
let nonce = webapi#sha1#sha1(nonce)[0:28]
let query["oauth_consumer_key"] = a:ctx.consumer_key
let query["oauth_nonce"] = nonce
let query["oauth_request_method"] = "POST"
let query["oauth_signature_method"] = "HMAC-SHA1"
let query["oauth_timestamp"] = time_stamp
let query["oauth_token"] = a:ctx.access_token
let query["oauth_version"] = "1.0"
if type(params) == 4
for key in keys(params)
let query[key] = params[key]
endfor
endif
if type(postdata) == 4
for key in keys(postdata)
let query[key] = postdata[key]
endfor
endif
let query_string = query["oauth_request_method"] . "&"
let query_string .= webapi#http#encodeURI(a:url)
let query_string .= "&"
let query_string .= webapi#http#encodeURI(webapi#http#encodeURI(query))
let hmacsha1 = webapi#hmac#sha1(webapi#http#encodeURI(a:ctx.consumer_secret) . "&" . webapi#http#encodeURI(a:ctx.access_token_secret), query_string)
let query["oauth_signature"] = webapi#base64#b64encodebin(hmacsha1)
if type(postdata) == 4
for key in keys(postdata)
call remove(query, key)
endfor
endif
let auth = 'OAuth '
for key in sort(keys(query))
let auth .= webapi#http#escape(key) . '="' . webapi#http#escape(query[key]) . '",'
endfor
let auth = auth[:-2]
let headdata["Authorization"] = auth
let res = webapi#http#post(a:url, postdata, headdata)
return res
endfunction
let &cpo = s:save_cpo
unlet s:save_cpo
" vim:set et: