Skip to content

Commit fe9a531

Browse files
committed
Support two factor authentication
1 parent 2c0bcf5 commit fe9a531

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

autoload/gist.vim

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -799,33 +799,51 @@ function! s:GistGetAuthHeader()
799799

800800
redraw
801801
echohl WarningMsg
802-
echo 'Gist.vim requires authorization to use the Github API. These settings are stored in "~/.gist-vim". If you want to revoke, do "rm ~/.gist-vim".'
802+
echo 'Gist.vim requires authorization to use the GitHub API. These settings are stored in "~/.gist-vim". If you want to revoke, do "rm ~/.gist-vim".'
803803
echohl None
804-
let password = inputsecret("Github Password for ".g:github_user.":")
805-
if len(password) > 0
806-
let insecureSecret = printf("basic %s", webapi#base64#b64encode(g:github_user.":".password))
804+
let password = inputsecret("GitHub Password for ".g:github_user.":")
805+
if len(password) == 0
806+
let secret = ''
807+
let v:errmsg = 'Canceled'
808+
return secrert
809+
endif
810+
let insecureSecret = printf("basic %s", webapi#base64#b64encode(g:github_user.":".password))
811+
let res = webapi#http#post(g:github_api_url.'/authorizations', webapi#json#encode({
812+
\ "scopes" : ["gist"],
813+
\ "note" : "Gist.vim on ".hostname(),
814+
\ "note_url" : "http://www.vim.org/scripts/script.php?script_id=2423"
815+
\}), {
816+
\ "Content-Type" : "application/json",
817+
\ "Authorization" : insecureSecret,
818+
\})
819+
let h = filter(res.header, 'v:val =~ "^X-GitHub-OTP: require"')
820+
if len(h)
821+
let otp = inputsecret("OTP:")
822+
if len(otp) == 0
823+
let secret = ''
824+
let v:errmsg = 'Canceled'
825+
return secrert
826+
endif
807827
let res = webapi#http#post(g:github_api_url.'/authorizations', webapi#json#encode({
808828
\ "scopes" : ["gist"],
809829
\ "note" : "Gist.vim on ".hostname(),
810830
\ "note_url" : "http://www.vim.org/scripts/script.php?script_id=2423"
811831
\}), {
812832
\ "Content-Type" : "application/json",
813833
\ "Authorization" : insecureSecret,
834+
\ "X-GitHub-OTP" : otp,
814835
\})
815-
let authorization = webapi#json#decode(res.content)
816-
if has_key(authorization, 'token')
817-
let secret = printf("token %s", authorization.token)
818-
call writefile([secret], s:gist_token_file)
819-
if !(has('win32') || has('win64'))
820-
call system("chmod go= ".s:gist_token_file)
821-
endif
822-
elseif has_key(authorization, 'message')
823-
let secret = ''
824-
let v:errmsg = authorization.message
836+
endif
837+
let authorization = webapi#json#decode(res.content)
838+
if has_key(authorization, 'token')
839+
let secret = printf("token %s", authorization.token)
840+
call writefile([secret], s:gist_token_file)
841+
if !(has('win32') || has('win64'))
842+
call system("chmod go= ".s:gist_token_file)
825843
endif
826-
else
844+
elseif has_key(authorization, 'message')
827845
let secret = ''
828-
let v:errmsg = 'Canceled'
846+
let v:errmsg = authorization.message
829847
endif
830848
return secret
831849
endfunction

0 commit comments

Comments
 (0)