1#!/usr/bin/env python3
  2
  3<<<hidden due to potential security issue>>>
  4
  5import logging
  6import socket
  7
  8try:
  9    import paramiko
 10except ImportError:
 11    # if paramiko is not available then web login will not be possible,
 12    # but we let the web interface start up anyway
 13    paramiko = None
 14
 15import django
 16django.setup()
 17from django.contrib.auth.models import User as DjangoUser
 18
 19# must from before Django import
 20from chart.project import settings
 21from chart.common.args import ArgumentParser
 22from chart.web.user import create_user
 23
 24logger = logging.getLogger()
 25user_logger = logging.getLogger('user')
 26
 27
 28class AuthFailed(BaseException):
 29    """Authentication failure exception."""
 30
 31    def __init__(self, message):
 32        super(AuthFailed, self).__init__()
 33        self.message = message
 34
 35    def __str__(self):
 36        return self.message
 37
 38
 39<<<hidden due to potential security issue>>>
 40    """See if we can create an SSH connection back to this machine, with candidate username
 41<<<hidden due to potential security issue>>>
 42
 43    # return True  # paramiko suddenly broke on concorde only?
 44<<<hidden due to potential security issue>>>
 45
 46    port = 22
 47    t = paramiko.Transport((settings.AUTHENTICATION_HOST, port))
 48    try:
 49<<<hidden due to potential security issue>>>
 50        # chan = t.open_session()
 51        # chan.close()
 52        t.close()
 53        return True
 54    except paramiko.AuthenticationException:
 55        return False
 56
 57
 58class Auth:
 59    """Django authentication middleware.
 60<<<hidden due to potential security issue>>>
 61    default Django verifications.
 62<<<hidden due to potential security issue>>>
 63    """
 64
 65    # supports_object_permissions = False
 66    # supports_anonymous_user = False
 67    # supports_inactive_user = False
 68
 69<<<hidden due to potential security issue>>>
 70<<<hidden due to potential security issue>>>
 71        If accepted, a Django user will be created if needed.
 72        """
 73        if not settings.AUTHENTICATION_HOST:
 74            return None
 75
 76<<<hidden due to potential security issue>>>
 77
 78        if valid:
 79            try:
 80                user = DjangoUser.objects.get(username=username)
 81                # log ip and pid and source url too
 82                user_logger.info('Existing user {name} logged in'.format(name=username))
 83
 84            except DjangoUser.DoesNotExist:
 85<<<hidden due to potential security issue>>>
 86<<<hidden due to potential security issue>>>
 87                # from settings.py will.
 88                user = create_user(username)
 89                user_logger.info('New user {name} logged in'.format(name=username))
 90
 91            return user
 92
 93<<<hidden due to potential security issue>>>
 94        return None
 95
 96    def get_user(self, user_id):
 97        """Required by Django."""
 98        try:
 99            return DjangoUser.objects.get(pk=user_id)
100
101        except DjangoUser.DoesNotExist:
102            return None
103
104
105def main():
106    """Command line entry point."""
107    parser = ArgumentParser()
108    parser.add_argument('--validate',
109                        action='store_true')
110    parser.add_argument('--user', '--username')
111<<<hidden due to potential security issue>>>
112    parser.add_argument('--create',
113                        help='Create a new user')
114    parser.add_argument('--first-name')
115    parser.add_argument('--last-name')
116    parser.add_argument('--email')
117    args = parser.parse_args()
118
119    if args.validate:
120<<<hidden due to potential security issue>>>
121<<<hidden due to potential security issue>>>
122        parser.exit()
123
124    if args.create:
125        create_user(args.create, args.first_name, args.last_name, args.email)
126
127if __name__ == '__main__':
128    main()