Skip to content

Commit 3ac379f

Browse files
Merge pull request #155 from jkakavas/al_updates
Updated account_linking microservice for use with eduTEAMS
2 parents 7a07eb2 + c94dffb commit 3ac379f

File tree

2 files changed

+32
-32
lines changed

2 files changed

+32
-32
lines changed

src/satosa/micro_services/account_linking.py

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ def __init__(self, config, *args, **kwargs):
3737

3838
def _handle_al_response(self, context):
3939
"""
40-
Endpoint for handling account linking service response
40+
Endpoint for handling account linking service response. When getting here
41+
user might have approved or rejected linking their account
4142
4243
:type context: satosa.context.Context
4344
:rtype: satosa.response.Response
@@ -47,7 +48,9 @@ def _handle_al_response(self, context):
4748
"""
4849
saved_state = context.state[self.name]
4950
internal_response = InternalResponse.from_dict(saved_state)
50-
status_code, message = self._get_uuid(context, internal_response.auth_info.issuer, internal_response.user_id)
51+
52+
#user_id here is the linked id , not the facebook one, Figure out what to do
53+
status_code, message = self._get_uuid(context, internal_response.auth_info.issuer, internal_response.attributes['issuer_user_id'])
5154

5255
if status_code == 200:
5356
satosa_logging(logger, logging.INFO, "issuer/id pair is linked in AL service",
@@ -59,7 +62,13 @@ def _handle_al_response(self, context):
5962
del context.state[self.name]
6063
return super().process(context, internal_response)
6164
else:
62-
raise SATOSAAuthenticationError(context.state, "Could not link account for user")
65+
# User selected not to link their accounts, so the internal.response.user_id is based on the
66+
# issuers id/sub which is fine
67+
satosa_logging(logger, logging.INFO, "User selected to not link their identity in AL service",
68+
context.state)
69+
del context.state[self.name]
70+
return super().process(context, internal_response)
71+
6372

6473
def process(self, context, internal_response):
6574
"""
@@ -72,42 +81,32 @@ def process(self, context, internal_response):
7281
:param context:
7382
:param internal_response:
7483
:return: response
84+
:
7585
"""
7686

7787
status_code, message = self._get_uuid(context, internal_response.auth_info.issuer, internal_response.user_id)
7888

89+
data = {
90+
"issuer": internal_response.auth_info.issuer,
91+
"redirect_endpoint": "%s/account_linking%s" % (self.base_url, self.endpoint)
92+
}
93+
94+
# Store the issuer user_id/sub because we'll need it in handle_al_response
95+
internal_response.attributes['issuer_user_id'] = internal_response.user_id
7996
if status_code == 200:
8097
satosa_logging(logger, logging.INFO, "issuer/id pair is linked in AL service",
8198
context.state)
8299
internal_response.user_id = message
100+
data['user_id'] = message
83101
if self.id_to_attr:
84102
internal_response.attributes[id_to_attr] = [message]
85-
try:
86-
del context.state[self.name]
87-
except KeyError:
88-
pass
89-
return super().process(context, internal_response)
90-
91-
return self._approve_new_id(context, internal_response, message)
92-
93-
def _approve_new_id(self, context, internal_response, ticket):
94-
"""
95-
Redirect the user to approve the new id
96-
97-
:type context: satosa.context.Context
98-
:type internal_response: satosa.internal_data.InternalResponse
99-
:type ticket: str
100-
:rtype: satosa.response.Redirect
101-
102-
:param context: The current context
103-
:param internal_response: The internal response
104-
:param ticket: The ticket given by the al service
105-
:return: A redirect to approve the new id linking
106-
"""
107-
satosa_logging(logger, logging.INFO, "A new ID must be linked by the AL service",
108-
context.state)
103+
else:
104+
satosa_logging(logger, logging.INFO, "issuer/id pair is not linked in AL service. Got a ticket",
105+
context.state)
106+
data['ticket'] = message
107+
jws = JWS(json.dumps(data), alg=self.signing_key.alg).sign_compact([self.signing_key])
109108
context.state[self.name] = internal_response.to_dict()
110-
return Redirect("%s/%s" % (self.redirect_url, ticket))
109+
return Redirect("%s/%s" % (self.redirect_url, jws))
111110

112111
def _get_uuid(self, context, issuer, id):
113112
"""
@@ -136,7 +135,7 @@ def _get_uuid(self, context, issuer, id):
136135
try:
137136
request = "{}/get_id?jwt={}".format(self.api_url, jws)
138137
response = requests.get(request)
139-
except requests.ConnectionError as con_exc:
138+
except Exception as con_exc:
140139
msg = "Could not connect to account linking service"
141140
satosa_logging(logger, logging.CRITICAL, msg, context.state, exc_info=True)
142141
raise SATOSAAuthenticationError(context.state, msg) from con_exc

tests/satosa/micro_services/test_account_linking.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,15 @@ def test_account_linking_failed(self, account_linking_config, internal_response,
102102
body=ticket,
103103
content_type="text/html"
104104
)
105-
105+
issuer_user_id = internal_response.user_id
106106
result = self.account_linking.process(context, internal_response)
107107
assert isinstance(result, Redirect)
108108
assert result.message.startswith(account_linking_config["redirect_url"])
109109

110110
# account linking endpoint still does not return an id
111-
with pytest.raises(SATOSAAuthenticationError):
112-
self.account_linking._handle_al_response(context)
111+
internal_response = self.account_linking._handle_al_response(context)
112+
#Verify that we kept the user_id the issuer sent us
113+
assert internal_response.user_id == issuer_user_id
113114

114115
@responses.activate
115116
def test_manage_al_handle_failed_connection(self, account_linking_config, internal_response, context):

0 commit comments

Comments
 (0)