Sitelet https://github.com/CPqD/trd-sdk-python/pull/7/files
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ para as chamadas de callback via Webhooks.

## Requisitos e instalação

Testado com Python 3.7. Para dependências, ver _requirements.txt_.
Testado com Python 3.12. Para dependências, ver _requirements.txt_.
Para instalação automática do SDK e dependências via `pip`, execute a linha abaixo:

```shell
$ pip install git+https://github.com/CPqD/trd-sdk-python.git@v1.1.0
$ pip install git+https://github.com/CPqD/trd-sdk-python.git@v1.2.0
```

#### Servidor WSGI para _callbacks_ via Webhooks
Expand Down Expand Up @@ -171,7 +171,9 @@ client = TranscriptionClient(
sl_username="<Usuário da licença>",
sl_password="<Senha da licença>",
sl_host="<Servidor de Autenticação>",
sl_port="<Porta de Autenticação>"
sl_port="<Porta de Autenticação>",
sl_protocol="https", # Opcional: Padrão é "https"
sl_token=None # Opcional: Insira um token válido caso não queira utilizar as credenciais acima para gerar um novo
)
```

Expand Down
66 changes: 41 additions & 25 deletions cpqdtrd/cert.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,56 @@
# -*- coding: utf-8 -*-
from OpenSSL import crypto
from cryptography import x509
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.x509.oid import NameOID
import ipaddress
from datetime import datetime, timedelta, timezone
import re


def create_self_signed_cert(hostname, cert_path, pkey_path):
"""Generate a certificate and private key, and returns the public key as str."""
# create a key pair
k = crypto.PKey()
k.generate_key(crypto.TYPE_RSA, 2048)

# create a self-signed cert
cert = crypto.X509()
cert.get_subject().C = "BR"
cert.get_subject().ST = "Sao Paulo"
cert.get_subject().L = "Campinas"
cert.get_subject().O = hostname
cert.get_subject().OU = hostname
cert.set_serial_number(1000)
cert.gmtime_adj_notBefore(0)
cert.gmtime_adj_notAfter(10 * 365 * 24 * 60 * 60)
cert.set_issuer(cert.get_subject())
key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
now = datetime.now(timezone.utc)
subject = issuer = x509.Name(
[
x509.NameAttribute(NameOID.COUNTRY_NAME, "BR"),
x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, "Sao Paulo"),
x509.NameAttribute(NameOID.LOCALITY_NAME, "Campinas"),
x509.NameAttribute(NameOID.ORGANIZATION_NAME, hostname),
x509.NameAttribute(NameOID.ORGANIZATIONAL_UNIT_NAME, hostname),
]
)

if re.compile(r"[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}").match(hostname):
alt_name = "IP:" + hostname
subject_alt_name = x509.IPAddress(ipaddress.ip_address(hostname))
else:
alt_name = "DNS:" + hostname
cert.add_extensions(
[crypto.X509Extension(b"subjectAltName", False, alt_name.encode())]
)
subject_alt_name = x509.DNSName(hostname)

cert.set_pubkey(k)
cert.sign(k, "sha512")
cert = (
x509.CertificateBuilder()
.subject_name(subject)
.issuer_name(issuer)
.public_key(key.public_key())
.serial_number(1000)
.not_valid_before(now)
.not_valid_after(now + timedelta(days=3650))
.add_extension(
x509.SubjectAlternativeName([subject_alt_name]),
critical=False,
)
.sign(private_key=key, algorithm=hashes.SHA512())
)

with open(cert_path, "wb") as f:
f.write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert))
f.write(cert.public_bytes(serialization.Encoding.PEM))
with open(pkey_path, "wb") as f:
f.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, k))
f.write(
key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption(),
)
)

return
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ pymongo>=3.7.2
tqdm
requests
flask
gevent
pyopenssl
gevent>= 23.9.1
cryptography
soundfile
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name="cpqdtrd",
version="1.1.0",
version="1.2.0",
description="CPqD Dialog Transcription Client",
install_requires=install_requires,
author="Akira Miasato",
Expand Down