0001 function [user,passwd,enc_key] = ensemble_get_credentials(auth_path,cred_type)
0002
0003
0004
0005
0006
0007
0008
0009 user = '';
0010 passwd = '';
0011 enc_key = '';
0012
0013 if nargin < 1
0014 auth_path = './';
0015 end
0016
0017 if ~exist('cred_type','var')
0018 cred_type = 'subject';
0019 elseif ~ismember(cred_type, {'subject','researcher'})
0020 fprintf('%s: Unknown login type: %s\n', mfilename, cred_type);
0021 return
0022 end
0023
0024
0025 if ~exist(auth_path,'dir')
0026 fprintf('%s: Directory does not exist: %s\n', mfilename, auth_path);
0027 return
0028 end
0029
0030
0031 fname = fullfile(auth_path,sprintf('%s_database_credentials.txt', cred_type));
0032 if ~exist(fname,'file')
0033 fprintf('%s: Credentials file does not exist or is not readable.\n', mfilename);
0034 return
0035 end
0036
0037 fid = fopen(fname,'rt');
0038 if fid==-1
0039 warning(sprintf('Failed to open credentials file: %s\n', fname));
0040 return
0041 end
0042
0043 while ~feof(fid)
0044 str = fgetl(fid);
0045 [tag,val] = strtok(str,':');
0046
0047 switch tag
0048 case 'user'
0049 user = strtrim(val(2:end));
0050
0051 case 'passwd'
0052 passwd = strtrim(val(2:end));
0053
0054 case 'enc_key_file'
0055 fstub = strtrim(val(2:end));
0056 if isempty(fstub)
0057 continue
0058 end
0059 enc_fname = fullfile(auth_path,fstub);
0060 encfid = fopen(enc_fname,'rt');
0061 if encfid < 3
0062 enc_key = '';
0063 fprintf('Could not open encryption key file\n');
0064 continue
0065 else
0066 enc_key = fgetl(encfid);
0067 fclose(encfid);
0068 end
0069
0070 end
0071
0072 end
0073
0074 fclose(fid);
0075