aboutsummaryrefslogtreecommitdiff
path: root/oauth2client/_helpers.py
diff options
context:
space:
mode:
authorDanny Hermes <daniel.j.hermes@gmail.com>2015-07-06 20:46:36 -0700
committerDanny Hermes <daniel.j.hermes@gmail.com>2015-07-13 13:46:53 -0700
commit3b3aeb65d704c0eec3471f12eae32d07dceaf288 (patch)
tree7494b5c2e13da10c480dc8be18d4cece79d1c6e9 /oauth2client/_helpers.py
parent76b3c40068db9923930db0e6047a9e5124ae68b9 (diff)
downloadoauth2client-3b3aeb65d704c0eec3471f12eae32d07dceaf288.tar.gz
Factoring out conditional code from `crypt.py`.
Until now, code that depended on PyCrypto or OpenSSL was defined conditionally (e.g. indented) in `crypt.py`. Rather than grouping all these together, we factor out the library specific behavior into standalone modules (but make the modules private / protected). In addition, added a `_helpers.py` module with common behavior that was previously defined in multiple places. Finally, beefed up some test cases so that the three newly added modules had 100% test coverage. Towards #212.
Diffstat (limited to 'oauth2client/_helpers.py')
-rw-r--r--oauth2client/_helpers.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/oauth2client/_helpers.py b/oauth2client/_helpers.py
new file mode 100644
index 0000000..974d210
--- /dev/null
+++ b/oauth2client/_helpers.py
@@ -0,0 +1,53 @@
+# Copyright 2015 Google Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+"""Helper functions for commonly used utilities."""
+
+import base64
+import json
+import six
+
+
+def _parse_pem_key(raw_key_input):
+ """Identify and extract PEM keys.
+
+ Determines whether the given key is in the format of PEM key, and extracts
+ the relevant part of the key if it is.
+
+ Args:
+ raw_key_input: The contents of a private key file (either PEM or PKCS12).
+
+ Returns:
+ string, The actual key if the contents are from a PEM file, or else None.
+ """
+ offset = raw_key_input.find(b'-----BEGIN ')
+ if offset != -1:
+ return raw_key_input[offset:]
+
+
+def _json_encode(data):
+ return json.dumps(data, separators=(',', ':'))
+
+
+def _urlsafe_b64encode(raw_bytes):
+ if isinstance(raw_bytes, six.text_type):
+ raw_bytes = raw_bytes.encode('utf-8')
+ return base64.urlsafe_b64encode(raw_bytes).decode('ascii').rstrip('=')
+
+
+def _urlsafe_b64decode(b64string):
+ # Guard against unicode strings, which base64 can't handle.
+ if isinstance(b64string, six.text_type):
+ b64string = b64string.encode('ascii')
+ padded = b64string + b'=' * (4 - len(b64string) % 4)
+ return base64.urlsafe_b64decode(padded)