1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
# Copyright (c) 2010-2024 openpyxl
from openpyxl.descriptors.serialisable import Serialisable
from openpyxl.descriptors import (
Alias,
Typed,
String,
Float,
Integer,
Bool,
NoneSet,
Set,
)
from openpyxl.descriptors.excel import (
ExtensionList,
HexBinary,
Guid,
Relation,
Base64Binary,
)
from openpyxl.utils.protection import hash_password
class WorkbookProtection(Serialisable):
_workbook_password, _revisions_password = None, None
tagname = "workbookPr"
workbook_password = Alias("workbookPassword")
workbookPasswordCharacterSet = String(allow_none=True)
revision_password = Alias("revisionsPassword")
revisionsPasswordCharacterSet = String(allow_none=True)
lockStructure = Bool(allow_none=True)
lock_structure = Alias("lockStructure")
lockWindows = Bool(allow_none=True)
lock_windows = Alias("lockWindows")
lockRevision = Bool(allow_none=True)
lock_revision = Alias("lockRevision")
revisionsAlgorithmName = String(allow_none=True)
revisionsHashValue = Base64Binary(allow_none=True)
revisionsSaltValue = Base64Binary(allow_none=True)
revisionsSpinCount = Integer(allow_none=True)
workbookAlgorithmName = String(allow_none=True)
workbookHashValue = Base64Binary(allow_none=True)
workbookSaltValue = Base64Binary(allow_none=True)
workbookSpinCount = Integer(allow_none=True)
__attrs__ = ('workbookPassword', 'workbookPasswordCharacterSet', 'revisionsPassword',
'revisionsPasswordCharacterSet', 'lockStructure', 'lockWindows', 'lockRevision',
'revisionsAlgorithmName', 'revisionsHashValue', 'revisionsSaltValue',
'revisionsSpinCount', 'workbookAlgorithmName', 'workbookHashValue',
'workbookSaltValue', 'workbookSpinCount')
def __init__(self,
workbookPassword=None,
workbookPasswordCharacterSet=None,
revisionsPassword=None,
revisionsPasswordCharacterSet=None,
lockStructure=None,
lockWindows=None,
lockRevision=None,
revisionsAlgorithmName=None,
revisionsHashValue=None,
revisionsSaltValue=None,
revisionsSpinCount=None,
workbookAlgorithmName=None,
workbookHashValue=None,
workbookSaltValue=None,
workbookSpinCount=None,
):
if workbookPassword is not None:
self.workbookPassword = workbookPassword
self.workbookPasswordCharacterSet = workbookPasswordCharacterSet
if revisionsPassword is not None:
self.revisionsPassword = revisionsPassword
self.revisionsPasswordCharacterSet = revisionsPasswordCharacterSet
self.lockStructure = lockStructure
self.lockWindows = lockWindows
self.lockRevision = lockRevision
self.revisionsAlgorithmName = revisionsAlgorithmName
self.revisionsHashValue = revisionsHashValue
self.revisionsSaltValue = revisionsSaltValue
self.revisionsSpinCount = revisionsSpinCount
self.workbookAlgorithmName = workbookAlgorithmName
self.workbookHashValue = workbookHashValue
self.workbookSaltValue = workbookSaltValue
self.workbookSpinCount = workbookSpinCount
def set_workbook_password(self, value='', already_hashed=False):
"""Set a password on this workbook."""
if not already_hashed:
value = hash_password(value)
self._workbook_password = value
@property
def workbookPassword(self):
"""Return the workbook password value, regardless of hash."""
return self._workbook_password
@workbookPassword.setter
def workbookPassword(self, value):
"""Set a workbook password directly, forcing a hash step."""
self.set_workbook_password(value)
def set_revisions_password(self, value='', already_hashed=False):
"""Set a revision password on this workbook."""
if not already_hashed:
value = hash_password(value)
self._revisions_password = value
@property
def revisionsPassword(self):
"""Return the revisions password value, regardless of hash."""
return self._revisions_password
@revisionsPassword.setter
def revisionsPassword(self, value):
"""Set a revisions password directly, forcing a hash step."""
self.set_revisions_password(value)
@classmethod
def from_tree(cls, node):
"""Don't hash passwords when deserialising from XML"""
self = super().from_tree(node)
if self.workbookPassword:
self.set_workbook_password(node.get('workbookPassword'), already_hashed=True)
if self.revisionsPassword:
self.set_revisions_password(node.get('revisionsPassword'), already_hashed=True)
return self
# Backwards compatibility
DocumentSecurity = WorkbookProtection
class FileSharing(Serialisable):
tagname = "fileSharing"
readOnlyRecommended = Bool(allow_none=True)
userName = String(allow_none=True)
reservationPassword = HexBinary(allow_none=True)
algorithmName = String(allow_none=True)
hashValue = Base64Binary(allow_none=True)
saltValue = Base64Binary(allow_none=True)
spinCount = Integer(allow_none=True)
def __init__(self,
readOnlyRecommended=None,
userName=None,
reservationPassword=None,
algorithmName=None,
hashValue=None,
saltValue=None,
spinCount=None,
):
self.readOnlyRecommended = readOnlyRecommended
self.userName = userName
self.reservationPassword = reservationPassword
self.algorithmName = algorithmName
self.hashValue = hashValue
self.saltValue = saltValue
self.spinCount = spinCount
|