Beispiel #1
0
    def post(self):
        print 'Opening request received'
        checkAccessResult = security.checkUserAccessPrivleges(g.user)
        print "Check user privileges for opening request: " + checkAccessResult
        if (checkAccessResult == "Access granted."):
            if datetime.datetime.now() > g.user.lastAccessDateTime + datetime.timedelta(minutes=config.NODE_LOG_MERGE):
                g.user.lastAccessDateTime = datetime.datetime.now()
                logentry = Action(datetime.datetime.utcnow(), config.NODE_NAME, g.user.firstName + ' ' + g.user.lastName,
                               g.user.email, 'Opening request ( ' + str(1) + ' attempts)', 'Opening request', 'L2', 0, 'Web based', Action.ACTION_OPENING_REQUEST, 1)
                print "Log-entry created"

                try:
                    db.session.add(logentry)
                    db.session.commit()
                except:
                    db.session.rollback()
                    return '', 401

            else:
                lastlogEntry = Action.query.filter_by(logType='Opening request', userMail=g.user.email).order_by(Action.date.desc()).first()
                if lastlogEntry is not None:
                    print str(lastlogEntry.synced)
                    if lastlogEntry.synced is 0:
                        print "is not None / False"
                        lastlogEntry.date = datetime.datetime.utcnow()
                        lastlogEntry.actionParameter += 1
                        lastlogEntry.logText = 'Opening request ( ' + str(lastlogEntry.actionParameter) + ' attempts)';
                    else:
                        print "is not None / True"
                        lastlogEntry.synced = 0
                        lastlogEntry.date = datetime.datetime.utcnow()
                        lastlogEntry.actionParameter = 1
                        lastlogEntry.logText = 'Opening request ( ' + str(lastlogEntry.actionParameter) + ' attempts)';
                    print str(lastlogEntry.synced)

                else:
                    print "is None"

                print "Log-entry is in merge-range ts = " + str(datetime.datetime.now()) + " last = " + str(g.user.lastAccessDateTime) + " merge = " + str(config.NODE_LOG_MERGE) + " minutes"

                try:
                    db.session.commit()
                except:
                    db.session.rollback()
                    return '', 401

            backgroundWorker.requestOpening = True

            return 'Access granted', 201
        else:
            print "Check user privileges for opening request: " + checkAccessResult
            return checkAccessResult, 201
        return '', 201
Beispiel #2
0
    def post(self):
        print 'Opening request received'
        checkAccessResult = security.checkUserAccessPrivleges(datetime.datetime.now(),g.user)
        print "Check user privileges for opening request: " + checkAccessResult
        if (checkAccessResult == "Access granted."):
            if datetime.datetime.now() > g.user.lastAccessDateTime + datetime.timedelta(minutes=config.NODE_LOG_MERGE):
                g.user.lastAccessDateTime = datetime.datetime.now()
                logentry = Action(datetime.datetime.utcnow(), config.NODE_NAME, g.user.firstName + ' ' + g.user.lastName,
                               g.user.email, 'Opening request ( ' + str(1) + ' attempts)', 'Opening request', 'L2', 0, 'Web based', Action.ACTION_OPENING_REQUEST, 1)
                print "Log-entry created"

                try:
                    db.session.add(logentry)
                    db.session.commit()
                except:
                    db.session.rollback()
                    return '', 401

            else:
                lastlogEntry = Action.query.filter_by(logType='Opening request', userMail=g.user.email).order_by(Action.date.desc()).first()
                if lastlogEntry is not None:
                    print str(lastlogEntry.synced)
                    if lastlogEntry.synced is 0:
                        print "is not None / False"
                        lastlogEntry.date = datetime.datetime.utcnow()
                        lastlogEntry.actionParameter += 1
                        lastlogEntry.logText = 'Opening request ( ' + str(lastlogEntry.actionParameter) + ' attempts)';
                    else:
                        print "is not None / True"
                        lastlogEntry.synced = 0
                        lastlogEntry.date = datetime.datetime.utcnow()
                        lastlogEntry.actionParameter = 1
                        lastlogEntry.logText = 'Opening request ( ' + str(lastlogEntry.actionParameter) + ' attempts)';
                    print str(lastlogEntry.synced)

                else:
                    print "is None"

                print "Log-entry is in merge-range ts = " + str(datetime.datetime.now()) + " last = " + str(g.user.lastAccessDateTime) + " merge = " + str(config.NODE_LOG_MERGE) + " minutes"

                try:
                    db.session.commit()
                except:
                    db.session.rollback()
                    return '', 401

            backgroundWorker.requestOpening = True

            return 'Access granted', 201
        else:
            print "Check user privileges for opening request: " + checkAccessResult
            return checkAccessResult, 201
        return '', 201
Beispiel #3
0
    def checkRFIDTag(self):

        #if self.first == True:
        #    self.first = False
        #    raise ValueError('A very specific bad thing happened')

        while (self.lock == True):
            print "still locked (checkRFIDTag)"
            time.sleep(0.2)

        try:
            self.lock = True

            (status,
             TagType) = RFIDReader.MFRC522_Request(RFIDReader.PICC_REQIDL)

            for i in range(0, 2):
                (status, uid) = RFIDReader.MFRC522_Anticoll()
                if status == RFIDReader.MI_OK:
                    break
                else:
                    time.sleep(0.2)

            self.resetTagInfo()

            # If we have the UID, continue
            if status == RFIDReader.MI_OK:

                # Print UID
                self.tagInfo.detected = True
                self.tagInfo.tagId = str(uid[0]) + "." + str(
                    uid[1]) + "." + str(uid[2]) + "." + str(uid[3])
                self.tagInfo.userInfo = ""

                user = User.query.filter_by(cardID=self.tagInfo.tagId).first()

                if user is None:
                    self.ledState = self.LED_STATE_ACCESS_DENIED
                    self.lock = False
                    return

                self.tagInfo.userInfo = user.email
                # print user.email

                # This is the default key for authentication
                defaultkey = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]
                userkey = []
                usersecret = []

                userkeyString = user.cardAuthKeyA
                for x in userkeyString.split('-'):
                    userkey.append(int(x, 16))

                # print "Userkey: " + str(userkey)

                usersecretString = user.cardSecret
                for x in usersecretString.split('-'):
                    usersecret.append(int(x, 16))

                # print "Usersecret: " + str(usersecret)

                SecretBlockAddr = user.cardAuthSector * 4 + user.cardAuthBlock
                TrailerBlockAddr = user.cardAuthSector * 4 + 3

                # Select the scanned tag
                RFIDReader.MFRC522_SelectTag(uid)

                # Authenticate
                status = RFIDReader.MFRC522_Auth(RFIDReader.PICC_AUTHENT1A,
                                                 SecretBlockAddr, userkey, uid)

                # Check if authenticated
                if status == RFIDReader.MI_OK:

                    readSecret = RFIDReader.MFRC522_Read(SecretBlockAddr)
                    # print readSecret
                    readSecretString = ''
                    i = 0

                    if not readSecret:
                        print "Read secret string is empty."
                        self.lock = False
                        RFIDReader.MFRC522_StopCrypto1()
                        return False

                    for x in readSecret:
                        if i != 0:
                            readSecretString = readSecretString + '-'
                        i = i + 1
                        readSecretString = readSecretString + format(x, '02X')

                    # print readSecretString

                    if readSecretString == user.cardSecret:
                        print "correct secret"
                        if security.checkUserAccessPrivleges(
                                datetime.datetime.now(),
                                user) == "Access granted.":
                            if datetime.datetime.now(
                            ) > user.lastAccessDateTime + datetime.timedelta(
                                    minutes=ConfigManager.NODE_LOG_MERGE):
                                user.lastAccessDateTime = datetime.datetime.now(
                                )

                                logentry = Action(
                                    datetime.datetime.utcnow(),
                                    ConfigManager.NODE_NAME,
                                    user.firstName + ' ' + user.lastName,
                                    user.email, 'Opening request (' + str(1) +
                                    ' attempts)', 'Opening request', 'L2', 1,
                                    'Card based',
                                    Action.ACTION_OPENING_REQUEST, 1)
                                print "Log-entry created"
                                try:
                                    db.session.add(logentry)
                                    db.session.commit()
                                except:
                                    self.ledState = self.LED_STATE_ACCESS_DENIED
                                    db.session.rollback()
                                    raise

                            else:
                                lastlogEntry = Action.query.filter_by(
                                    logType='Opening request',
                                    userMail=user.email).order_by(
                                        Action.date.desc()).first()
                                if lastlogEntry is not None:
                                    if lastlogEntry.synced is 0:
                                        lastlogEntry.date = datetime.datetime.utcnow(
                                        )
                                        lastlogEntry.actionParameter += 1
                                        lastlogEntry.logText = 'Opening request (' + str(
                                            lastlogEntry.actionParameter
                                        ) + ' attempts)'
                                    else:
                                        lastlogEntry.synced = 0
                                        lastlogEntry.date = datetime.datetime.utcnow(
                                        )
                                        lastlogEntry.actionParameter = 1
                                        lastlogEntry.logText = 'Opening request (' + str(
                                            lastlogEntry.actionParameter
                                        ) + ' attempts)'
                                print "Log-entry is in merge-range ts = " + str(
                                    datetime.datetime.now()
                                ) + " last = " + str(
                                    user.lastAccessDateTime
                                ) + " merge = " + str(
                                    ConfigManager.NODE_LOG_MERGE) + " minutes"
                                try:
                                    db.session.commit()
                                except:
                                    self.ledState = self.LED_STATE_ACCESS_DENIED
                                    db.session.rollback()
                                    raise

                            self.requestOpening = True
                            self.ledState = self.LED_STATE_ACCESS_GRANTED
                        else:
                            self.ledState = self.LED_STATE_ACCESS_DENIED
                            print "no user-access privilege"
                    else:
                        self.tagInfo.userInfo = user.email + '(inv. sec.)'
                        print "no user-access privilege"
                        self.ledState = self.LED_STATE_ACCESS_DENIED

                    RFIDReader.MFRC522_StopCrypto1()
                    self.lock = False
                    return True
                else:
                    self.tagInfo.userInfo = user.email + '(inv. key.)'
                    print "Authentication error"
                    self.ledState = self.LED_STATE_ACCESS_DENIED
                    self.lock = False
                    return False
            else:
                self.lock = False
                return False
        except:
            self.lock = False
            print "unexpected error in checkRFIDTag"
            self.ledState = self.LED_STATE_ACCESS_DENIED
            raise
Beispiel #4
0
    def checkRFIDTag(self):

        # if self.first == True:
        #    self.first = False
        #    raise ValueError('A very specific bad thing happened')

        while self.lock == True:
            print "still locked (checkRFIDTag)"
            time.sleep(0.2)

        try:
            self.lock = True

            (status, TagType) = RFIDReader.MFRC522_Request(RFIDReader.PICC_REQIDL)

            for i in range(0, 2):
                (status, uid) = RFIDReader.MFRC522_Anticoll()
                if status == RFIDReader.MI_OK:
                    break
                else:
                    time.sleep(0.2)

            self.resetTagInfo()

            # If we have the UID, continue
            if status == RFIDReader.MI_OK:

                # Print UID
                self.tagInfo.tagId = str(uid[0]) + "." + str(uid[1]) + "." + str(uid[2]) + "." + str(uid[3])
                self.tagInfo.userInfo = ""

                user = User.query.filter_by(cardID=self.tagInfo.tagId).first()

                if user is None:
                    self.ledState = self.LED_STATE_ACCESS_DENIED
                    self.lock = False
                    return

                self.tagInfo.userInfo = user.email
                # print user.email

                # This is the default key for authentication
                defaultkey = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]
                userkey = []
                usersecret = []

                userkeyString = user.cardAuthKeyA
                for x in userkeyString.split("-"):
                    userkey.append(int(x, 16))

                # print "Userkey: " + str(userkey)

                usersecretString = user.cardSecret
                for x in usersecretString.split("-"):
                    usersecret.append(int(x, 16))

                # print "Usersecret: " + str(usersecret)

                SecretBlockAddr = user.cardAuthSector * 4 + user.cardAuthBlock
                TrailerBlockAddr = user.cardAuthSector * 4 + 3

                # Select the scanned tag
                RFIDReader.MFRC522_SelectTag(uid)

                # Authenticate
                status = RFIDReader.MFRC522_Auth(RFIDReader.PICC_AUTHENT1A, SecretBlockAddr, userkey, uid)

                # Check if authenticated
                if status == RFIDReader.MI_OK:

                    readSecret = RFIDReader.MFRC522_Read(SecretBlockAddr)
                    # print readSecret
                    readSecretString = ""
                    i = 0

                    if not readSecret:
                        print "Read secret string is empty."
                        self.lock = False
                        RFIDReader.MFRC522_StopCrypto1()
                        return False

                    for x in readSecret:
                        if i != 0:
                            readSecretString = readSecretString + "-"
                        i = i + 1
                        readSecretString = readSecretString + format(x, "02X")

                    # print readSecretString

                    if readSecretString == user.cardSecret:
                        print "correct secret"
                        if security.checkUserAccessPrivleges(datetime.datetime.now(), user) == "Access granted.":
                            if datetime.datetime.now() > user.lastAccessDateTime + datetime.timedelta(
                                minutes=config.NODE_LOG_MERGE
                            ):
                                user.lastAccessDateTime = datetime.datetime.now()

                                logentry = Action(
                                    datetime.datetime.utcnow(),
                                    config.NODE_NAME,
                                    user.firstName + " " + user.lastName,
                                    user.email,
                                    "Opening request (" + str(1) + " attempts)",
                                    "Opening request",
                                    "L2",
                                    1,
                                    "Card based",
                                    Action.ACTION_OPENING_REQUEST,
                                    1,
                                )
                                print "Log-entry created"
                                try:
                                    db.session.add(logentry)
                                    db.session.commit()
                                except:
                                    self.ledState = self.LED_STATE_ACCESS_DENIED
                                    db.session.rollback()
                                    raise

                            else:
                                lastlogEntry = (
                                    Action.query.filter_by(logType="Opening request", userMail=user.email)
                                    .order_by(Action.date.desc())
                                    .first()
                                )
                                if lastlogEntry is not None:
                                    if lastlogEntry.synced is 0:
                                        lastlogEntry.date = datetime.datetime.utcnow()
                                        lastlogEntry.actionParameter += 1
                                        lastlogEntry.logText = (
                                            "Opening request (" + str(lastlogEntry.actionParameter) + " attempts)"
                                        )
                                    else:
                                        lastlogEntry.synced = 0
                                        lastlogEntry.date = datetime.datetime.utcnow()
                                        lastlogEntry.actionParameter = 1
                                        lastlogEntry.logText = (
                                            "Opening request (" + str(lastlogEntry.actionParameter) + " attempts)"
                                        )
                                print "Log-entry is in merge-range ts = " + str(
                                    datetime.datetime.now()
                                ) + " last = " + str(user.lastAccessDateTime) + " merge = " + str(
                                    config.NODE_LOG_MERGE
                                ) + " minutes"
                                try:
                                    db.session.commit()
                                except:
                                    self.ledState = self.LED_STATE_ACCESS_DENIED
                                    db.session.rollback()
                                    raise

                            self.requestOpening = True
                            self.ledState = self.LED_STATE_ACCESS_GRANTED
                        else:
                            self.ledState = self.LED_STATE_ACCESS_DENIED
                            print "no user-access privilege"
                    else:
                        self.tagInfo.userInfo = user.email + "(inv. sec.)"
                        print "no user-access privilege"
                        self.ledState = self.LED_STATE_ACCESS_DENIED

                    RFIDReader.MFRC522_StopCrypto1()
                    self.lock = False
                    return True
                else:
                    self.tagInfo.userInfo = user.email + "(inv. key.)"
                    print "Authentication error"
                    self.ledState = self.LED_STATE_ACCESS_DENIED
                    self.lock = False
                    return False
            else:
                self.lock = False
                return False
        except:
            self.lock = False
            print "unexpected error in checkRFIDTag"
            self.ledState = self.LED_STATE_ACCESS_DENIED
            raise