annotate credentials.m @ 1:c4b57991935a

Tell emacs to use org mode for this file.
author Daniele Nicolodi <daniele@science.unitn.it>
date Sun, 23 May 2010 22:06:57 +0200
parents d5fef23867bb
children b4f2f4c10918
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
1 classdef credentials
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
2 properties
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
3
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
4 hostname = [];
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
5 database = [];
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
6 username = [];
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
7 password = [];
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
8 expiry = 0;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
9
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
10 end % properties
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
11
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
12 methods
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
13
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
14 % contructor
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
15 function obj = credentials(hostname, database, username, password)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
16 switch nargin
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
17 case 1
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
18 obj.hostname = hostname;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
19 case 2
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
20 obj.hostname = hostname;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
21 obj.database = database;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
22 case 3
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
23 obj.hostname = hostname;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
24 obj.database = database;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
25 obj.username = username;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
26 case 4
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
27 obj.hostname = hostname;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
28 obj.database = database;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
29 obj.username = username;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
30 obj.password = password;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
31 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
32 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
33
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
34 % convert to string representation
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
35 function str = char(obj, mode)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
36 if nargin < 2
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
37 mode = '';
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
38 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
39 switch mode
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
40 case 'short'
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
41 % do not show password
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
42 frm = 'mysql://%s/%s username=%s';
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
43 str = sprintf(frm, obj.hostname, obj.database, obj.username);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
44 case 'full'
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
45 % show password
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
46 frm = 'mysql://%s/%s username=%s password=%s';
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
47 str = sprintf(frm, obj.hostname, obj.database, obj.username, obj.password);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
48 otherwise
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
49 % by default only show if a password is known
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
50 passwd = [];
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
51 if ischar(obj.password)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
52 passwd = 'YES';
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
53 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
54 frm = 'mysql://%s/%s username=%s password=%s';
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
55 str = sprintf(frm, obj.hostname, obj.database, obj.username, passwd);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
56 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
57 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
58
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
59 % display
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
60 function disp(obj)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
61 disp([' ' char(obj) char(10)]);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
62 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
63
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
64 % check that a credentials object contails all the required informations
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
65 function rv = complete(obj)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
66 info = {'hostname', 'database', 'username', 'password'};
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
67 for kk = 1:numel(info)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
68 if isempty(obj.(info{kk}))
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
69 rv = false;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
70 return;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
71 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
72 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
73 rv = true;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
74 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
75
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
76 % check if the credentials are expired
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
77 function rv = expired(obj)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
78 rv = false;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
79 if obj.expiry > 0 && double(time()) > obj.expiry
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
80 rv = true;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
81 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
82 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
83
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
84 % check if the credentials object matches the given informations
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
85 function rv = match(obj, hostname, database, username)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
86 if nargin < 4
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
87 username = [];
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
88 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
89
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
90 % default value
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
91 rv = true;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
92
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
93 if ~strcmp(obj.hostname, hostname)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
94 rv = false;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
95 return;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
96 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
97 if ~strcmp(obj.database, database)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
98 rv = false;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
99 return;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
100 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
101 if ischar(username) && ischar(obj.username) && ~strcmp(obj.username, username)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
102 rv = false;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
103 return;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
104 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
105 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
106
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
107 end % methods
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
108
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
109 end