<commit_before>from datetime import datetime
from enum import Enum

from oscrypto import asymmetric


class OCSPResponderStatus(Enum):
    good = 'good'
    questionable = 'questionable'
    bad = 'bad'
    unknown = 'unknown'


class Authority(object):
    name: str = None
    number_of_certs: int = None
    rank: int = None  # update this when rankings change. Don't delete formerly-high-ranked authorities as that would mess up relations to old test results


class OCSPResponder(object):
    # NOTE: there may be a case where one OCSP URL is used by more than one authority. URL cannot be considered unique
    url: str = None
    authority: Authority = None  # this should be a relation
    number_of_certs: int = None
    subject_cert: Certificate = None  # cached, a relation
    current: bool = None  # whether there are any valid certs that use this URL, can be null if we haven't checked yet
    # if current is False, display the row with strikethrough/muted colors/something


class Certificate(object):
    subject_cert: bytes = None
    issuer_cert: bytes = None
    retrieved_on: datetime = None  # expire the cached certificate when this date is more than 7 days ago
    ocsp_url: OCSPResponder = None  # a relation
    authority: Authority = None  # a relation, maybe not necessary since we can walk up via the associated OCSPResponder

    @property
    def expires_on(self) -> datetime:  # expire the cached certificate when this date is in the past
        certificate = asymmetric.load_certificate(self.subject_cert)
        return certificate.asn1['tbs_certificate']['validity']['not_after'].native


class TestResult(object):
    authority: Authority = None  # a relation
    url: OCSPResponder = None  # a relation, this name might not need to be "url"?
    timestamp: datetime = None  # when the test was run
    location: str = None  # where we got this result from
    ping: bool = None  # did the server respond to a ping?
    ocsp: bool = None  # did a valid OCSP request get a good response?

    @property
    def status(self) -> OCSPResponderStatus:  # relates to the glyphicon displayed
        if self.ocsp and self.ping:
            return OCSPResponderStatus.good
        elif self.ocsp and not self.ping:
            return OCSPResponderStatus.questionable
        elif not self.ocsp:
            return OCSPResponderStatus.bad
        else:
            return OCSPResponderStatus.unknown  # I don't think we'd ever get here
<commit_msg>Make concept into models<commit_after># -*- coding: utf-8 -*-

from datetime import datetime
from enum import Enum

from oscrypto import asymmetric
from sqlalchemy import (
    Binary,
    Column,
    ForeignKey,
    Boolean,
    UniqueConstraint,
    Integer,
    String,
    DateTime,
    Text
)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, backref

Base = declarative_base()


class OCSPResponderStatus(Enum):
    good = 'good'
    questionable = 'questionable'
    bad = 'bad'
    unknown = 'unknown'


class Authority(Base):
    """Represents the authority that issues certificates"""
    __tablename__ = 'authority'

    id = Column(Integer, primary_key=True)

    name = Column(String(255), nullable=False, index=True, doc='The name of the authority')

    number_of_certs = Column(Integer)  # TODO document this

    rank = Column(Integer, doc=("Update this when rankings change. Don't delete formerly-high-ranked "
                                "authorities as that would mess up relations to old test results"))

    endpoints = relationship(
        'Endpoint',
        secondary=Responder,
        primaryjoin=(id == Responder.authority_id),
        secondaryjoin=(id == Responder.endpoint_id),
        backref=backref('authorities')
    )


class Endpoint(Base):
    """Represents the URL at which an OCSP is present"""
    __tablename__ = 'endpoint'

    id = Column(Integer, primary_key=True)

    url = Column(Text, nullable=False, doc='The URL of the OCSP endpoint')


class Responder(Base):
    """Represents the unique pair of authority/endpoint"""
    __tablename__ = 'responder'

    id = Column(Integer, primary_key=True)

    authority_id = Column(Integer, ForeignKey('authority.id'), nullable=False, doc='The authority')
    endpoint_id = Column(Integer, ForeignKey('endpoint.id'), nullable=False, doc='The endpoint')

    __table_args__ = (
        UniqueConstraint(authority_id, endpoint_id),
    )


class Certificate(Base):
    """Represents a certificate and its origin"""
    __tablename__ = 'certificate'

    id = Column(Integer, primary_key=True)

    responder_id = Column(Integer, ForeignKey('responder.id'))
    responder = relationship('Responder')

    subject = Column(Binary, nullable=False, doc="")
    issuer = Column(Binary, nullable=False, doc="")
    retrieved = Column(DateTime, nullable=False,
                       doc="expire the cached certificate when this date is more than 7 days ago")

    @property
    def expires_on(self) -> datetime:  # expire the cached certificate when this date is in the past
        certificate = asymmetric.load_certificate(self.subject_cert)
        return certificate.asn1['tbs_certificate']['validity']['not_after'].native


class User(Base):
    """References a user"""
    __tablename__ = 'user'

    id = Column(Integer, primary_key=True)

    location = Column(Text, doc='The location to be displayed')


class Result(Base):
    """The information about the result from a ping"""
    __tablename__ = 'result'

    id = Column(Integer, primary_key=True)

    responder_id = Column(Integer, ForeignKey('responder.id'))
    responder = relationship('Responder')

    user_id = Column(Integer, ForeignKey('user.id'), nullable=False, doc='The user that ran the test')
    user = relationship('User')

    retrieved = Column(DateTime, default=datetime.utcnow, doc="When was the test run")
    ping = Column(Boolean, default=False, nullable=False, doc='did the server respond to a ping?')
    ocsp = Column(Boolean, default=False, nullable=False, doc='did a valid OCSP request get a good response?')

    @property
    def authority(self):
        return self.responder.authority

    @property
    def endpoint(self):
        return self.responder.endpoint

    @property
    def status(self) -> OCSPResponderStatus:  # relates to the glyphicon displayed
        if self.ocsp and self.ping:
            return OCSPResponderStatus.good
        elif self.ocsp and not self.ping:
            return OCSPResponderStatus.questionable
        elif not self.ocsp:
            return OCSPResponderStatus.bad
        else:
            return OCSPResponderStatus.unknown  # I don't think we'd ever get here

{'fraction_non_alphanumeric': 0.08643815201192251, 'fraction_numerical': 0.0011922503725782414, 'mean_word_length': 3.3408796895213455, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '211323', 'n_tokens_mistral': 1954, 'n_tokens_neox': 1839, 'n_words': 639}