def create_token(api, settings, email, password): while True: try: if get_user_config(settings).get('ssh_auth') and password is None: key_path = get_user_config(settings).get('ssh_path', get_default_ssh_key_path()) fingerprint = get_public_key_fingerprint(key_path) if not fingerprint: raise PublicKeyException('WrongPublicKey') ssh_token = api.create_ssh_token() signature = sign_token(key_path, fingerprint, ssh_token) return api.create_token_ssh_auth(email, ssh_token, signature, fingerprint) else: return api.create_token_basic_auth(email, password) except (cclib.APIException, SignatureException, PublicKeyException) as e: if password is not None: sys.exit(messages['NotAuthorized']) print >> sys.stderr, str(e) + " " + messages['NotAuthorizedPublicKey'] password = get_password_env(settings) if not password: password = get_password()
def test_sign_token_with_private_key(self, os, RSAKey, **kwargs): os.path.exist.return_value = True private_key = Mock() prefix = 'ssh-rsa' content = "BBBBBB" private_key.sign_ssh_data.return_value = struct.pack('>I', len(prefix)) + prefix + struct.pack('>I', len(content)) + content RSAKey.from_private_key_file.return_value = private_key result = sign_token('/path/to/pkey.pub', 'fingerprint', 'somedata') self.assertEqual(content.encode('base64').strip(), result)
def test_sign_token_with_private_key(self, os, RSAKey, **kwargs): os.path.exist.return_value = True private_key = Mock() prefix = 'ssh-rsa' content = "BBBBBB" private_key.sign_ssh_data.return_value = struct.pack( '>I', len(prefix)) + prefix + struct.pack('>I', len(content)) + content RSAKey.from_private_key_file.return_value = private_key result = sign_token('/path/to/pkey.pub', 'fingerprint', 'somedata') self.assertEqual(content.encode('base64').strip(), result)
def test_sign_token_with_agent(self, os, paramiko, **kwargs): os.path.exist.return_value = False private_key = Mock() private_key.get_fingerprint.return_value = binascii.a2b_hex('4a:4c:5b:3e:47:21:d5:7f:c9:8c:d9:2e:a7:22:73:65'.replace(':', '')) prefix = 'ssh-rsa' content = "BBBBBB" private_key.sign_ssh_data.return_value = struct.pack('>I', len(prefix)) + prefix + struct.pack('>I', len(content)) + content agent = paramiko.agent.Agent.return_value agent.get_keys.return_value = [private_key] result = sign_token(None, '4a:4c:5b:3e:47:21:d5:7f:c9:8c:d9:2e:a7:22:73:65', 'somedata') self.assertEqual(content.encode('base64').strip(), result)
def test_sign_token_with_agent(self, os, paramiko, **kwargs): os.path.exist.return_value = False private_key = Mock() private_key.get_fingerprint.return_value = binascii.a2b_hex( '4a:4c:5b:3e:47:21:d5:7f:c9:8c:d9:2e:a7:22:73:65'.replace(':', '')) prefix = 'ssh-rsa' content = "BBBBBB" private_key.sign_ssh_data.return_value = struct.pack( '>I', len(prefix)) + prefix + struct.pack('>I', len(content)) + content agent = paramiko.agent.Agent.return_value agent.get_keys.return_value = [private_key] result = sign_token(None, '4a:4c:5b:3e:47:21:d5:7f:c9:8c:d9:2e:a7:22:73:65', 'somedata') self.assertEqual(content.encode('base64').strip(), result)