Skip to content

Commit ee7b73e

Browse files
Merge pull request #159 from eywalker/master
Hide password during entry and increment version to 3.3.0
2 parents 3a1daa4 + 7dac497 commit ee7b73e

File tree

3 files changed

+80
-5
lines changed

3 files changed

+80
-5
lines changed

+dj/+lib/getpass.m

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
function pass = getpass(prompt)
2+
%GETPASS Open up a dialog box for the user to enter password. Password
3+
%will be hidden as the user enters text. You can pass in an optional
4+
%argument prompt to be used as the dialog box title. Defaults to "Enter
5+
%password".
6+
7+
if nargin < 1
8+
prompt = 'Enter password';
9+
end
10+
11+
screenSize = get(0, 'ScreenSize');
12+
13+
% configure the diaglog box
14+
hfig = figure( ...
15+
'Menubar', 'none', ...
16+
'Units', 'Pixels', ...
17+
'NumberTitle', 'off', ...
18+
'Resize', 'off', ...
19+
'Name', prompt, ...
20+
'Position', [(screenSize(3:4)-[300 75])/2 300 75], ...
21+
'Color', [0.8 0.8 0.8], ...
22+
'WindowStyle', 'modal');
23+
24+
hpass = uicontrol( ...
25+
'Parent', hfig, ...
26+
'Style', 'Text', ...
27+
'Tag', 'password', ...
28+
'Units', 'Pixels', ...
29+
'Position', [51 30 198 18], ...
30+
'FontSize', 15, ...
31+
'BackGroundColor', [1 1 1]);
32+
33+
set(hfig,'KeyPressFcn',{@keypress_cb, hpass}, 'CloseRequestFcn','uiresume')
34+
35+
% wait for password entry
36+
uiwait
37+
pass = get(hpass,'userdata');
38+
% remove the figure to prevent passwork leakage
39+
delete(hfig)
40+
41+
42+
function keypress_cb(hObj, data, hpass)
43+
% Callback function to handle actual key strokes
44+
45+
pass = get(hpass,'userdata');
46+
47+
switch data.Key
48+
case 'backspace'
49+
pass = pass(1:end-1);
50+
case 'return'
51+
uiresume
52+
return
53+
otherwise
54+
% append the typed character
55+
pass = [pass data.Character];
56+
end
57+
set(hpass, 'userdata', pass)
58+
set(hpass, 'String', char('*' * sign(pass)))

+dj/conn.m

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,19 @@
1818
% Once established during the first invocation, the connection object cannot
1919
% be changed. To reset the connection, use 'clear functions' or 'clear classes'.
2020

21-
function connObj = conn(host, user, pass, initQuery, reset, use_tls)
21+
function connObj = conn(host, user, pass, initQuery, reset, use_tls, nogui)
2222
persistent CONN
2323

24-
if nargin < 5
24+
if nargin < 5 || isempty(reset)
2525
reset = false;
2626
end
2727

28+
% get password prompt option
29+
if nargin < 7 || isempty(nogui)
30+
nogui = false;
31+
end
32+
33+
2834
if isa(CONN, 'dj.Connection') && ~reset
2935
assert(nargin==0, ...
3036
'connection already instantiated. To reconnect, clear functions')
@@ -58,7 +64,11 @@
5864
pass = getenv(env.pass);
5965
end
6066
if isempty(pass)
61-
pass = input('Enter datajoint password> ','s');
67+
if nogui
68+
pass = input('Enter datajoint password> ','s');
69+
else
70+
pass = dj.lib.getpass('Enter datajoint password');
71+
end
6272
end
6373

6474
% get initial query (if any) to execute when a connection is (re)established
@@ -70,6 +80,7 @@
7080
if nargin<6 || isempty(use_tls)
7181
use_tls = dj.set('use_tls');
7282
end
83+
7384
if islogical(use_tls) && ~use_tls
7485
use_tls = 'false';
7586
elseif islogical(use_tls) && use_tls
@@ -85,8 +96,14 @@
8596

8697
connObj = CONN;
8798

99+
% if connection fails to establish, remove the persistent connection object
88100
if ~connObj.isConnected
89-
query(connObj, 'status')
101+
try
102+
query(connObj, 'status')
103+
catch e
104+
CONN = [];
105+
rethrow(e)
106+
end
90107
end
91108

92109
if nargout==0

+dj/version.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function varargout = version
22
% report DataJoint version
33

4-
v = struct('major',3,'minor',2,'bugfix',2);
4+
v = struct('major',3,'minor',3,'bugfix',0);
55

66
if nargout
77
varargout{1}=v;

0 commit comments

Comments
 (0)