Home > database > ensemble_get_credentials.m

ensemble_get_credentials

PURPOSE ^

[user,passwd,enc_key] = ensemble_get_credentials(auth_path);

SYNOPSIS ^

function [user,passwd,enc_key] = ensemble_get_credentials(auth_path,cred_type)

DESCRIPTION ^

 [user,passwd,enc_key] = ensemble_get_credentials(auth_path);

 Looks for a file called <cred_type>_database_credentials.txt in the directory
 specified in authpath, where cred_type is either researcher or subject

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [user,passwd,enc_key] = ensemble_get_credentials(auth_path,cred_type)
0002 % [user,passwd,enc_key] = ensemble_get_credentials(auth_path);
0003 %
0004 % Looks for a file called <cred_type>_database_credentials.txt in the directory
0005 % specified in authpath, where cred_type is either researcher or subject
0006 
0007 % 06/15/10 PJ - fixed original version
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 % Make sure the directory exists
0025 if ~exist(auth_path,'dir')
0026   fprintf('%s: Directory does not exist: %s\n', mfilename, auth_path);
0027   return
0028 end
0029 
0030 % Make sure the credentials file exists and is readable
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

Generated on Tue 16-Jul-2019 04:00:51 by m2html © 2003