annotate credentials.m @ 3:b4f2f4c10918

Add help comments.
author Daniele Nicolodi <daniele@science.unitn.it>
date Sun, 23 May 2010 22:12:05 +0200
parents d5fef23867bb
children
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
3
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
2
0
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
3 properties
3
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
4
0
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
5 hostname = [];
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
6 database = [];
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
7 username = [];
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
8 password = [];
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
9 expiry = 0;
3
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
10
0
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
11 end % properties
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
12
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
13 methods
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
14
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
15 function obj = credentials(hostname, database, username, password)
3
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
16 % CREDENTIALS Constructor for credentials objects.
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
17 %
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
18 % Those are simple container objects to hold credentials required for
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
19 % establishing a connection to a database server, in addition to an
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
20 % expiry time.
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
21 %
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
22 % CREDENTIALS(hostname, database, username, password) The constructor can
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
23 % be called with any number of arguments. The default value for the object
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
24 % properties is the empty vector.
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
25
0
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
26 switch nargin
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
27 case 1
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
28 obj.hostname = hostname;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
29 case 2
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
30 obj.hostname = hostname;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
31 obj.database = database;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
32 case 3
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
33 obj.hostname = hostname;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
34 obj.database = database;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
35 obj.username = username;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
36 case 4
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
37 obj.hostname = hostname;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
38 obj.database = database;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
39 obj.username = username;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
40 obj.password = password;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
41 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
42 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
43
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
44 function str = char(obj, mode)
3
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
45 % CHAR Convert a credentials object to string representation.
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
46 %
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
47 % It takes an optional second argument that defines the representation to
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
48 % use. The default is to replace the password, if present, with the YES
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
49 % string, other possible modes are SHORT where password is omitted, or FULL
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
50 % where the password is shown at it is.
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
51
0
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
52 if nargin < 2
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
53 mode = '';
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
54 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
55 switch mode
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
56 case 'short'
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
57 % do not show password
3
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
58 frm = 'mysql://%s/%s username=%s';
0
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
59 str = sprintf(frm, obj.hostname, obj.database, obj.username);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
60 case 'full'
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
61 % show password
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
62 frm = 'mysql://%s/%s username=%s password=%s';
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
63 str = sprintf(frm, obj.hostname, obj.database, obj.username, obj.password);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
64 otherwise
3
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
65 % by default only show if a password is known
0
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
66 passwd = [];
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
67 if ischar(obj.password)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
68 passwd = 'YES';
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
69 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
70 frm = 'mysql://%s/%s username=%s password=%s';
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
71 str = sprintf(frm, obj.hostname, obj.database, obj.username, passwd);
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 end
3
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
74
0
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
75 function disp(obj)
3
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
76 % DISP Overloaded display method for credentials objects.
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
77 %
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
78 % Uses the default string representation of the char() method where the
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
79 % password, if present, is replaced with the string YES.
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
80
0
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
81 disp([' ' char(obj) char(10)]);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
82 end
3
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
83
0
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
84 function rv = complete(obj)
3
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
85 % COMPLETE Checks if the credentials are complete.
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
86 %
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
87 % Credentials object are complete when they contains all the required
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
88 % information to connect to a database. Namely the HOSTNAME, DATABASE
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
89 % and USERNAME properties should not be empty, the PASSWORD property is
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
90 % allowed to be an empty string '' but not [].
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
91
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
92 info = {'hostname', 'database', 'username'};
0
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
93 for kk = 1:numel(info)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
94 if isempty(obj.(info{kk}))
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
95 rv = false;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
96 return;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
97 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
98 end
3
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
99 if ~ischar(obj.password)
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
100 rv = false;
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
101 return;
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
102 end
0
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
103 rv = true;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
104 end
3
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
105
0
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
106 function rv = expired(obj)
3
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
107 % EXPIRED Checks if the credentials are expired.
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
108 %
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
109 % Credential objects expire when their expiry time is smaller than the
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
110 % current time in seconds since the epoch, as obtained by the time()
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
111 % function. Credentials with zero or negative expiry time never expire.
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
112
0
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
113 rv = false;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
114 if obj.expiry > 0 && double(time()) > obj.expiry
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
115 rv = true;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
116 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
117 end
3
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
118
0
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
119 function rv = match(obj, hostname, database, username)
3
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
120 % MATCH Check if the credentials object matches the given information.
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
121 %
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
122 % MATCH(obj, hostname, database) Returns true when HOSTANAME and DATABASE
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
123 % parameters match the object properties.
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
124 %
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
125 % MATCH(obj, hostname, database, username) Returns true when HOSTANAME,
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
126 % DATABASE, and USERNAME parameters match the object
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
127 % properties. USERNAME is compared only if both the given one and the
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
128 % object property are not the empty vector.
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
129
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
130 % default arguments
0
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
131 if nargin < 4
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
132 username = [];
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
133 end
3
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
134
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
135 % default return value
0
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
136 rv = true;
3
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
137
0
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
138 if ~strcmp(obj.hostname, hostname)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
139 rv = false;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
140 return;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
141 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
142 if ~strcmp(obj.database, database)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
143 rv = false;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
144 return;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
145 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
146 if ischar(username) && ischar(obj.username) && ~strcmp(obj.username, username)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
147 rv = false;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
148 return;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
149 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
150 end
3
b4f2f4c10918 Add help comments.
Daniele Nicolodi <daniele@science.unitn.it>
parents: 0
diff changeset
151
0
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
152 end % methods
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
153
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
154 end