Skip to content
This repository has been archived by the owner on Jul 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #194 from UPC/feature_api_rest_identitat
Browse files Browse the repository at this point in the history
Canvis per donar suport a la nova API REST

Closes: #188
  • Loading branch information
alexm authored Feb 17, 2020
2 parents 93465df + d792c0e commit c7b232a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 21 deletions.
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ mock>=1.3.0
suds
discover
freezegun
requests

1 change: 1 addition & 0 deletions settings_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"password_soa": "xxx",
"username_gn6": "xxx",
"password_gn6": "xxx",
"identitat_digital_apikey": "xxx",

# Instància de GN6 on voleu crear els tiquets
"domini": "999",
Expand Down
57 changes: 36 additions & 21 deletions soa/identitat.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import settings
import re
from soa.service import SOAService
import requests


class GestioIdentitat(SOAService):
class GestioIdentitat:

def __init__(self):
self.url = "https://bus-soa.upc.edu/GestioIdentitat/Personesv6?wsdl"
self.url = "https://identitatdigital.upc.edu/gcontrol/rest"
self.token = self.get_token()
self.identitat_local = GestioIdentitatLocal()
SOAService.__init__(self)

def get_token(self):
try:
resposta = requests.post(
self.url+"/acls/processos",
data={'idProces': settings.get("identitat_digital_apikey")})
token = resposta.json()['tokenAcl']
return token
except Exception:
return None

def canonicalitzar_mail(self, mail):
if mail is None:
Expand Down Expand Up @@ -36,30 +46,35 @@ def obtenir_uid_remot(self, mail):
# si la part esquerra del mail correspon a un usuari UPC real
if "@upc.edu" in mail:
cn = mail.split("@")[0]
dades_persona = self.client.service.obtenirDadesPersona(
commonName=cn)
if dades_persona.ok:
return cn
try:
cn = mail.split("@")[0]
persona = requests.get(
self.url+"/externs/persones/"+cn+"/cn",
headers={'TOKEN': self.token}).json()
return persona['commonName']
except Exception:
None

# Si no hi ha correspondencia directa amb un usuari UPC
# busquem a partir del mail qui pot ser
resultat = self.client.service.llistaPersones(email=mail)
if len(resultat.llistaPersones.persona) == 1:
cns = requests.get(self.url+"/externs/identitats/cn?email=" + mail,
headers={'TOKEN': self.token}).json()
if len(cns) == 1:
# Quan tenim un resultat, es aquest
return resultat.llistaPersones.persona[0].cn
return cns[0]
else:
# Si tenim mes d'un, busquem el que te el mail que busquem
# com a preferent o be retornem el primer
for persona in resultat.llistaPersones.persona:
dades_persona = self.client.service.obtenirDadesPersona(
commonName=persona.cn)
emailPreferent = getattr(
dades_persona,
'emailPreferent',
None)
if (self.canonicalitzar_mail(emailPreferent) == mail):
return persona.cn

for cn in cns:
try:
persona = requests.get(
self.url+"/externs/persones/"+cn+"/cn",
headers={'TOKEN': self.token}).json()
email_preferent = persona['emailPreferent']
if (self.canonicalitzar_mail(email_preferent) == mail):
return persona['commonName']
except Exception:
None
return None
except Exception:
return None
Expand Down

0 comments on commit c7b232a

Please sign in to comment.