source: TI12-security/tags/start/X500DN.py @ 380

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/tags/start/X500DN.py@1316
Revision 380, 1.8 KB checked in by lawrence, 15 years ago (diff)

Creating NDG security directory

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#Copyright (C) 2004 CCLRC & NERC
2#    This software may be distributed under the terms of the
3#    Q Public License, version 1.0 or later
4#
5# Version 0.1 BNL November 12, 2004
6#
7from UserDict import UserDict
8
9class X500DN(UserDict):
10  ''' An X500 Distinguished Name from RFC2253 (supplemented with
11  Email address) expressed as a python dictionary '''
12  def __init__(self,argdict=None):
13    self.longNames={'CN':'commonName',
14                   'OU':'OrganisationalUnitName',
15                   'O':'Organisation',
16                   'C':'CountryName',
17                   'EMAIL':'Email Address',
18                   'L':'localityName',
19                   'ST':'stateOrProvinceName',
20                   'STREET':'streetAddress',
21                   'UID':'userid'}
22    UserDict.__init__(self,self.longNames)
23 
24    self._clear()
25    if argdict is not None:
26      for i in argdict: self.data[i]=argdict[i]
27
28  def _clear(self):
29    for i in self.keys(): self.data[i]=''
30
31  def __delitem__(self,key):
32    raise 'Keys cannot be deleted from the X500DN '
33
34  def __setitem__(self,key,item):
35    if key not in self.data.keys():
36      raise 'Key '+key+' not known in X500DN '
37    self.data[key]=item
38
39  def serialise(self):
40    ''' Return X500 string '''
41    keys=self.data.keys()
42    keys.sort()
43    s=''
44    for i in keys:
45      if self.data[i]!='':
46        s+=i+' = '+self.data[i]+','
47    if len(s)!=0:s=s[:-1] # trim last comma
48    return s
49
50  def deserialise(self,s):
51    list=s.split(',')
52    self._clear()
53    for item in list:
54      key,value=item.split('=')
55      self.data[key]=value
56
57if __name__=='__main__':
58  print 'Demonstrating the use of X500DN'
59  y=X500DN()
60  print y
61  print y.longNames
62  print y.keys()
63 
64  y['CN']='Bryan'
65
66  print 'show the serialisation'
67  print y.serialise()
68  y.deserialise('DN = Another Bryan, C = Another place')
69  print y
70
71  y['ABC']='bnl' #should break
72 
Note: See TracBrowser for help on using the repository browser.