def test_construct_with_token(self, services): authz_service = services['authorization'] _state = authz_service.create_state('Issuer') req = AuthorizationRequest(state=_state, response_type='code', redirect_uri='https://example.com', scope=['openid']) authz_service.store_item(req, 'auth_request', _state) # Add a state and bind a code to it resp1 = AuthorizationResponse(code="auth_grant", state=_state) response = services['authorization'].parse_response( resp1.to_urlencoded(), "urlencoded") services['authorization'].update_service_context(response, key=_state) # based on state find the code and then get an access token resp2 = AccessTokenResponse(access_token="token1", token_type="Bearer", expires_in=0, state=_state) response = services['accesstoken'].parse_response( resp2.to_urlencoded(), "urlencoded") services['accesstoken'].update_service_context(response, key=_state) # and finally use the access token, bound to a state, to # construct the authorization header http_args = BearerHeader().construct(ResourceRequest(), services['accesstoken'], key=_state) assert http_args == {"headers": {"Authorization": "Bearer token1"}}
def test_multiple_scope(self): atr = AccessTokenResponse( access_token="2YotnFZFEjr1zCsicMWpAA", token_type="example", expires_in=3600, refresh_token="tGzv3JOkF0XG5Qx2TlKWIA", example_parameter="example_value", scope=["inner", "outer"]) assert _eq(atr["scope"], ["inner", "outer"]) uec = atr.to_urlencoded() assert "inner+outer" in uec
def test_to_urlencoded_extended_omit(self): atr = AccessTokenResponse( access_token="2YotnFZFEjr1zCsicMWpAA", token_type="example", expires_in=3600, refresh_token="tGzv3JOkF0XG5Qx2TlKWIA", example_parameter="example_value", scope=["inner", "outer"], extra=["local", "external"], level=3) uec = atr.to_urlencoded() assert query_string_compare(uec, "scope=inner+outer&level=3&expires_in=3600&token_type=example" "&extra=local&" "extra=external&refresh_token=tGzv3JOkF0XG5Qx2TlKWIA&" "access_token=2YotnFZFEjr1zCsicMWpAA&example_parameter" "=example_value") del atr["extra"] ouec = atr.to_urlencoded() assert query_string_compare(ouec, "access_token=2YotnFZFEjr1zCsicMWpAA&refresh_token=tGzv3JOkF0XG5Qx2TlKWIA&" "level=3&example_parameter=example_value&token_type=example" "&expires_in=3600&" "scope=inner+outer") assert len(uec) == (len(ouec) + len("extra=local") + len("extra=external") + 2) atr2 = AccessTokenResponse().deserialize(uec, "urlencoded") assert _eq(atr2.keys(), ['access_token', 'expires_in', 'token_type', 'scope', 'refresh_token', 'level', 'example_parameter', 'extra']) atr3 = AccessTokenResponse().deserialize(ouec, "urlencoded") assert _eq(atr3.keys(), ['access_token', 'expires_in', 'token_type', 'scope', 'refresh_token', 'level', 'example_parameter'])
def test_construct_with_request(self, services): authz_service = services['authorization'] authz_service.state_db.set('EEEE', State(iss='Issuer').to_json()) resp1 = AuthorizationResponse(code="auth_grant", state="EEEE") response = authz_service.parse_response(resp1.to_urlencoded(), "urlencoded") authz_service.update_service_context(response, key='EEEE') resp2 = AccessTokenResponse(access_token="token1", token_type="Bearer", expires_in=0, state="EEEE") response = services['accesstoken'].parse_response( resp2.to_urlencoded(), "urlencoded") services['accesstoken'].update_service_context(response, key='EEEE') request = ResourceRequest() BearerBody().construct(request, service=authz_service, key="EEEE") assert "access_token" in request assert request["access_token"] == "token1"
def test_construct_with_request(self, entity): authz_service = entity.client_get("service", 'authorization') _cntx = authz_service.client_get("service_context") _key = _cntx.state.create_state(iss='Issuer') resp1 = AuthorizationResponse(code="auth_grant", state=_key) response = authz_service.parse_response(resp1.to_urlencoded(), "urlencoded") authz_service.update_service_context(response, key=_key) resp2 = AccessTokenResponse(access_token="token1", token_type="Bearer", expires_in=0, state=_key) _token_service = entity.client_get("service", 'accesstoken') response = _token_service.parse_response(resp2.to_urlencoded(), "urlencoded") _token_service.update_service_context(response, key=_key) request = ResourceRequest() BearerBody().construct(request, service=authz_service, key=_key) assert "access_token" in request assert request["access_token"] == "token1"