connect to exchange server
This commit is contained in:
@@ -23,6 +23,7 @@ import json
|
||||
import locale
|
||||
import hashlib
|
||||
import imaplib
|
||||
import msal
|
||||
import email
|
||||
|
||||
from ..models.default import *
|
||||
@@ -72,34 +73,55 @@ def to_percent(x):
|
||||
"""Takes a float and returns a string"""
|
||||
return ("%.2f " % x).replace('.', ',') + "%"
|
||||
|
||||
def generate_auth_string(user, token):
|
||||
return f"user={user}\x01auth=Bearer {token}\x01\x01"
|
||||
|
||||
def mailbox_connect(request, societe):
|
||||
# connecter au serveur IMAP de la societe
|
||||
if societe == 'PE':
|
||||
mbx_name = 'peinture-dumas@entreprise-dumas.com'
|
||||
mbx_pwd = 'S@sdumas69'
|
||||
elif societe == 'ME':
|
||||
mbx_name = 'menuiserie-dumas@entreprise-dumas.com'
|
||||
mbx_pwd = 'S@sdumas69'
|
||||
elif societe == 'PL':
|
||||
mbx_name = 'versanit-dumas@entreprise-dumas.com'
|
||||
mbx_pwd = 'S@sdumas69'
|
||||
elif societe == 'PO':
|
||||
mbx_name = 'polynet-dumas@entreprise-dumas.com'
|
||||
mbx_pwd = 'S@sdumas69'
|
||||
else:
|
||||
request.session.flash("Cette société est inconnue ou non traitée : %s" % societe, 'danger')
|
||||
return None
|
||||
|
||||
|
||||
conf = {
|
||||
"authority": "https://login.microsoftonline.com/47f02128-acf0-47a7-a3bf-27a3619d4a4f",
|
||||
"client_id": "4fdf2634-a260-4576-a442-684998f0187b", #AppID
|
||||
"scope": ['https://outlook.office365.com/.default'],
|
||||
"secret": "dZD8Q~GmG_PFi.t_uRiyBEzwLeMZeemQGHUFta~J", #Key-Value
|
||||
"secret-id": "5cae3249-0fc0-43e2-bc0e-d78f41964970", #Key-ID
|
||||
}
|
||||
|
||||
app = msal.ConfidentialClientApplication(conf['client_id'], authority=conf['authority'],
|
||||
client_credential=conf['secret'])
|
||||
|
||||
result = app.acquire_token_silent(conf['scope'], account=None)
|
||||
|
||||
if not result:
|
||||
result = app.acquire_token_for_client(scopes=conf['scope'])
|
||||
|
||||
if "access_token" not in result:
|
||||
request.session.flash("ERREUR de connexion au serveur Exchange : %s" % result.get("error"), 'danger')
|
||||
return None
|
||||
|
||||
imap = imaplib.IMAP4('outlook.office365.com')
|
||||
imap.starttls()
|
||||
# import pdb;pdb.set_trace()
|
||||
conn = imaplib.IMAP4_SSL('imap.entreprise-dumas.com')
|
||||
try:
|
||||
# se connecter à la mailbox
|
||||
conn.login(mbx_name, mbx_pwd)
|
||||
imap.authenticate("XOAUTH2", lambda x: generate_auth_string(mbx_name, result['access_token']).encode("utf-8"))
|
||||
except imaplib.IMAP4.error:
|
||||
request.session.flash("ERREUR connexion au compte %s" % mbx_name, 'danger')
|
||||
return None
|
||||
|
||||
return conn
|
||||
# returne la connection
|
||||
return imap
|
||||
|
||||
@view_config(route_name='home', renderer='../templates/default/home.pt', permission='view')
|
||||
def home(request):
|
||||
|
||||
Reference in New Issue
Block a user