-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthentication.py
More file actions
127 lines (84 loc) · 3.3 KB
/
authentication.py
File metadata and controls
127 lines (84 loc) · 3.3 KB
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
"""
Authentication module for communicating with a Midas server
"""
from zope.interface import implements
from twisted.internet.protocol import Protocol
from twisted.conch.interfaces import IConchUser
from twisted.conch.avatar import ConchUser
from twisted.conch.ssh.session import SSHSession, SSHSessionProcessProtocol, wrapProtocol
from twisted.conch.ssh.factory import SSHFactory
from twisted.conch.ssh.userauth import SSHUserAuthServer
from twisted.cred.checkers import ICredentialsChecker
from twisted.cred.credentials import IUsernamePassword
from twisted.python import log
from sftpServer import MidasFileTransferServer
from twisted.cred import portal, checkers, credentials, error
from twisted.internet import defer
import pydas
class EchoProtocol(Protocol):
def connectionMade(self):
self.transport.write("Echo protocol connected\r\n")
def dataReceived(self, bytes):
self.transport.write("echo: " + repr(bytes) + "\r\n")
def connectionLost(self, reason):
print 'Connection lost', reason
def eofReceived(self):
print 'eofReceived'
def closed(self):
print 'closed'
def closedReceived(self):
print 'closeReceived'
class SCPProtocol(Protocol):
def connectionMade(self):
print 'connection made'
self.transport.write('some data')
self.transport.loseConnection()
def dataReceived(self, bytes):
print 'dataReceived: %r' % bytes
def connectionLost(self, reason):
print 'connectionLost', reason
def nothing():
pass
class SimpleSession(SSHSession):
name = 'session'
def request_pty_req(self, data):
return True
def request_shell(self, data):
protocol = EchoProtocol()
transport = SSHSessionProcessProtocol(self)
protocol.makeConnection(transport)
transport.makeConnection(wrapProtocol(protocol))
self.client = transport
return True
def request_exec(self, data):
print 'request_exec', data
protocol = SCPProtocol()
transport = SSHSessionProcessProtocol(self)
protocol.makeConnection(transport)
transport.makeConnection(wrapProtocol(protocol))
self.client = transport
return True
class MidasConchUser(ConchUser):
def __init__(self, avatarId):
ConchUser.__init__(self)
self.email, self.pydas, self.url = avatarId
self.channelLookup['session'] = SSHSession
self.subsystemLookup.update(
{'sftp': MidasFileTransferServer})
class MidasRealm:
implements(portal.IRealm)
def requestAvatar(self, avatarId, mind, *interfaces):
user = MidasConchUser(avatarId)
return IConchUser, user, nothing
class MidasChecker(object):
implements(ICredentialsChecker)
credentialInterfaces = (IUsernamePassword,)
def __init__(self, url):
self.url = url
def requestAvatarId(self, credentials):
try:
pydas.login(email=credentials.username, password=credentials.password, application='Midasftp Server', url=self.url)
except pydas.exceptions.PydasException as detail:
print "Caught PydasException: ", detail
return defer.fail(error.LoginFailed("Invalid email or password"))
return defer.succeed((credentials.username, pydas, self.url))