Skip to content

Commit 3cce6e2

Browse files
committed
Use keyring to store passwords
Reference: https://pypi.org/project/keyring
1 parent 13f6fff commit 3cce6e2

File tree

3 files changed

+79
-11
lines changed

3 files changed

+79
-11
lines changed

README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,27 @@
44

55
Solve LeetCode problems in Vim!
66

7-
**Supported sorting by columns (Hooray🎉🎉🎉) Now you can solve problems by frequency!**
8-
97
This Vim plugin is inspired by [skygragon/leetcode-cli][leetcode-cli].
108

9+
**Important Update! Please setup keyring for password safety:**
10+
11+
1. Install keyring with `pip3 install keyring --user`
12+
2. Remove `g:leetcode_password` from your configuration.
13+
3. The first time you sign in, **leetcode.vim** will prompt for the password
14+
and store it in keyring.
15+
1116
## Installation
1217

1318
1. Vim with `+python3` feature is **required**. Install the **pynvim** package
1419
for Neovim:
1520
```sh
1621
pip3 install pynvim --user
1722
```
18-
2. Install the plugin:
23+
2. Install **keyring**:
24+
```sh
25+
pip3 install keyring --user
26+
```
27+
3. Install the plugin:
1928
```vim
2029
Plug 'ianding1/leetcode.vim'
2130
```
@@ -64,7 +73,11 @@ Default value is `''`.
6473

6574
### `g:leetcode_password`
6675

67-
Set to the LeetCode password for auto login.
76+
**Deprecated in favor of keyring.** Set to the LeetCode password for auto login.
77+
78+
If you have installed keyring, then just leave this option blank.
79+
**leetcode.vim** will prompt for the password the first time you sign in, and
80+
store the password in keyring.
6881

6982
**WARNING: the password is stored in plain text.**
7083

autoload/leetcode.vim

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,33 @@ else:
2121
os.environ['LEETCODE_BASE_URL'] = 'https://leetcode.com'
2222

2323
import leetcode
24+
25+
try:
26+
import keyring
27+
has_keyring = True
28+
except ImportError:
29+
has_keyring = False
2430
EOF
2531

2632
let s:inited = py3eval('leetcode.inited')
2733

34+
let s:has_keyring = py3eval('has_keyring')
35+
2836
if g:leetcode_debug
2937
python3 leetcode.enable_logging()
3038
endif
3139

32-
function! leetcode#SignIn(ask) abort
40+
function! s:DoSignIn(username, password) abort
41+
let expr = printf('leetcode.signin("%s", "%s")', a:username, a:password)
42+
let success = py3eval(expr)
43+
44+
if success
45+
echo 'Signed in as ' . a:username
46+
endif
47+
return success
48+
endfunction
49+
50+
function! s:LegacySignIn(ask) abort
3351
if !s:inited
3452
return v:false
3553
endif
@@ -45,13 +63,43 @@ function! leetcode#SignIn(ask) abort
4563
let password = g:leetcode_password
4664
endif
4765

48-
let expr = printf('leetcode.signin("%s", "%s")', username, password)
49-
let success = py3eval(expr)
66+
return s:DoSignIn(username, password)
67+
endfunction
5068

51-
if a:ask && success
52-
echo 'succesfully signed in as '.username
69+
function! s:KeyringSignIn(ask) abort
70+
if !s:inited
71+
return v:false
72+
endif
73+
74+
if g:leetcode_username != ''
75+
let saved_password = py3eval(
76+
\ printf('keyring.get_password("leetcode.vim", "%s")',
77+
\ g:leetcode_username))
78+
else
79+
let saved_password = v:null
80+
endif
81+
82+
if a:ask || saved_password == v:null
83+
let username = input('Username: ', g:leetcode_username)
84+
let password = inputsecret('Password: ')
85+
let g:leetcode_username = username
86+
call py3eval(printf('keyring.set_password("leetcode.vim", "%s", "%s")',
87+
\ username, password))
88+
redraw
89+
else
90+
let username = g:leetcode_username
91+
let password = saved_password
92+
endif
93+
94+
return s:DoSignIn(username, password)
95+
endfunction
96+
97+
function! leetcode#SignIn(ask) abort
98+
if s:has_keyring
99+
return s:KeyringSignIn(a:ask)
100+
else
101+
return s:LegacySignIn(a:ask)
53102
endif
54-
return success
55103
endfunction
56104

57105
function! s:CheckSignIn() abort
@@ -75,6 +123,7 @@ function! s:SetupProblemListBuffer() abort
75123
setlocal norelativenumber
76124
setlocal nospell
77125
setlocal bufhidden=hide
126+
setlocal nowrap
78127
nnoremap <silent> <buffer> <return> :call <SID>HandleProblemListCR()<cr>
79128
nnoremap <silent> <buffer> s :call <SID>HandleProblemListS()<cr>
80129
nnoremap <silent> <buffer> r :call <SID>HandleProblemListR()<cr>
@@ -947,6 +996,7 @@ function! s:ListSubmissions(slug, refresh) abort
947996
setlocal nospell
948997
setlocal nonumber
949998
setlocal norelativenumber
999+
setlocal nowrap
9501000
nnoremap <silent> <buffer> <return> :call <SID>HandleSubmissionsCR()<cr>
9511001
nnoremap <silent> <buffer> r :call <SID>HandleSubmissionsRefresh()<cr>
9521002

doc/leetcode.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ g:leetcode_username Set it to the username/email to allow autologin.
5656
Default: ''
5757

5858
*g:leetcode_password*
59-
g:leetcode_password Set it to the password to allow autologin.
59+
g:leetcode_password Deprecated in favor of keyring. Set it to the password to
60+
allow autologin.
61+
62+
If you have installed keyring, then just leave this option
63+
blank. leetcode.vim will prompt for the password the first
64+
time you sign in, and store the password in keyring.
6065

6166
Default: ''
6267

0 commit comments

Comments
 (0)