def test_bearer_body(): client = Client("A") client.client_secret = "boarding pass" request_args = {"access_token": "Sesame"} cis = ResourceRequest() http_args = BearerBody(client).construct(cis, request_args) assert cis["access_token"] == "Sesame" print http_args assert http_args is None # ---------- resp = AuthorizationResponse(code="code", state="state") grant = Grant() grant.add_code(resp) atr = AccessTokenResponse(access_token="2YotnFZFEjr1zCsicMWpAA", token_type="example", refresh_token="tGzv3JOkF0XG5Qx2TlKWIA", example_parameter="example_value", scope=["inner", "outer"]) grant.add_token(atr) client.grant["state"] = grant cis = ResourceRequest() http_args = BearerBody(client).construct(cis, {}, state="state", scope="inner") assert cis["access_token"] == "2YotnFZFEjr1zCsicMWpAA" print http_args assert http_args is None
def test_client_secret_post(): client = Client("A") client.client_secret = "boarding pass" cis = AccessTokenRequest(code="foo", redirect_uri="http://example.com") csp = ClientSecretPost(client) http_args = csp.construct(cis) print cis assert cis["client_id"] == "A" assert cis["client_secret"] == "boarding pass" print http_args assert http_args is None cis = AccessTokenRequest(code="foo", redirect_uri="http://example.com") request_args = {} http_args = csp.construct(cis, request_args, http_args={"client_secret": "another"}) print cis assert cis["client_id"] == "A" assert cis["client_secret"] == "another" print http_args assert http_args == {}
def test_bearer_header_with_http_args(): client = Client("A") client.client_secret = "boarding pass" request_args = {"access_token": "Sesame"} cis = ResourceRequest() bh = BearerHeader(client) http_args = bh.construct(cis, request_args, http_args={"foo": "bar"}) print cis print http_args assert _eq(http_args.keys(), ["foo", "headers"]) assert http_args["headers"] == {"Authorization": "Bearer Sesame"} # ----------------- request_args = {"access_token": "Sesame"} bh = BearerHeader(client) http_args = bh.construct(cis, request_args, http_args={"headers": {"x-foo": "bar"}}) print cis print http_args assert _eq(http_args.keys(), ["headers"]) assert _eq(http_args["headers"].keys(), ["Authorization", "x-foo"]) assert http_args["headers"]["Authorization"] == "Bearer Sesame"
def test_bearer_header_with_http_args(): client = Client("A") client.client_secret = "boarding pass" request_args = {"access_token": "Sesame"} cis = ResourceRequest() bh = BearerHeader(client) http_args = bh.construct(cis, request_args, http_args={"foo": "bar"}) print cis print http_args assert _eq(http_args.keys(), ["foo", "headers"]) assert http_args["headers"] == {"Authorization": "Bearer Sesame"} # ----------------- request_args = {"access_token": "Sesame"} bh = BearerHeader(client) http_args = bh.construct(cis, request_args, http_args={"headers": { "x-foo": "bar" }}) print cis print http_args assert _eq(http_args.keys(), ["headers"]) assert _eq(http_args["headers"].keys(), ["Authorization", "x-foo"]) assert http_args["headers"]["Authorization"] == "Bearer Sesame"
def test_client_secret_basic(): client = Client("A") client.client_secret = "boarding pass" cis = AccessTokenRequest(code="foo", redirect_uri="http://example.com") http_args = oauth2.client_secret_basic(client, cis) assert http_args == {"auth": ("A", "boarding pass")}
def test_client_secret_basic(): client = Client("A") client.client_secret = "boarding pass" cis = AccessTokenRequest(code="foo", redirect_uri="http://example.com") csb = ClientSecretBasic(client) http_args = csb.construct(cis) assert http_args == {"headers": {'Authorization': 'Basic %s' % base64.b64encode('A:boarding pass')}}
def test_bearer_header_2(): client = Client("A") client.client_secret = "boarding pass" cis = ResourceRequest(access_token="Sesame") http_args = oauth2.bearer_header(client, cis) print cis assert "access_token" not in cis print http_args assert http_args == {"headers": {"Authorization": "Bearer Sesame"}}
def test_bearer_header_2(): client = Client("A") client.client_secret = "boarding pass" bh = BearerHeader(client) cis = ResourceRequest(access_token="Sesame") http_args = bh.construct(cis) print cis assert "access_token" not in cis print http_args assert http_args == {"headers": {"Authorization": "Bearer Sesame"}}
def test_bearer_header(): client = Client("A") client.client_secret = "boarding pass" request_args = {"access_token": "Sesame"} cis = ResourceRequest() bh = BearerHeader(client) http_args = bh.construct(cis, request_args) print cis print http_args assert http_args == {"headers": {"Authorization": "Bearer Sesame"}}
def test_bearer_body_get_token(): client = Client("A") client.client_secret = "boarding pass" client.state = "state" resp1 = AuthorizationResponse(code="auth_grant", state="state") client.parse_response(AuthorizationResponse, resp1.to_urlencoded(), "urlencoded") resp2 = AccessTokenResponse(access_token="token1", token_type="Bearer", expires_in=0, state="state") client.parse_response(AccessTokenResponse, resp2.to_urlencoded(), "urlencoded") cis = ResourceRequest() oauth2.bearer_body(client, cis) assert "access_token" in cis assert cis["access_token"] == "token1"
def test_bearer_header_3(): client = Client("A") client.client_secret = "boarding pass" client.state = "state" resp1 = AuthorizationResponse(code="auth_grant", state="state") client.parse_response(AuthorizationResponse, resp1.to_urlencoded(), "urlencoded") resp2 = AccessTokenResponse(access_token="token1", token_type="Bearer", expires_in=0, state="state") client.parse_response(AccessTokenResponse, resp2.to_urlencoded(), "urlencoded") cis = ResourceRequest() http_args = oauth2.bearer_header(client, cis) print cis assert "access_token" not in cis print http_args assert http_args == {"headers": {"Authorization": "Bearer token1"}}
def test_bearer_body_get_token(): client = Client("A") client.client_secret = "boarding pass" resp1 = AuthorizationResponse(code="auth_grant", state="state") client.parse_response(AuthorizationResponse, resp1.to_urlencoded(), "urlencoded") resp2 = AccessTokenResponse(access_token="token1", token_type="Bearer", expires_in=0, state="state") client.parse_response(AccessTokenResponse, resp2.to_urlencoded(), "urlencoded") cis = ResourceRequest() _ = BearerBody(client).construct(cis, state="state") assert "access_token" in cis assert cis["access_token"] == "token1"
def test_bearer_header_3(): client = Client("A") client.client_secret = "boarding pass" resp1 = AuthorizationResponse(code="auth_grant", state="state") client.parse_response(AuthorizationResponse, resp1.to_urlencoded(), "urlencoded") resp2 = AccessTokenResponse(access_token="token1", token_type="Bearer", expires_in=0, state="state") client.parse_response(AccessTokenResponse, resp2.to_urlencoded(), "urlencoded") cis = ResourceRequest() http_args = BearerHeader(client).construct(cis, state="state") print cis assert "access_token" not in cis print http_args assert http_args == {"headers": {"Authorization": "Bearer token1"}}
def test_client_secret_jwt(): cli = Client("Foo") cli.token_endpoint = "https://example.com/token" cli.client_secret = "foobar" csj = ClientSecretJWT(cli) cis = AccessTokenRequest() http_args = csj.construct(cis, algorithm="HS256") print http_args assert cis["client_assertion_type"] == JWT_BEARER assert "client_assertion" in cis cas = cis["client_assertion"] _jwt = JWT().unpack(cas) jso = json.loads(_jwt.part[1]) assert _eq(jso.keys(), ["aud", "iss", "sub", "jti", "exp", "iat"]) print _jwt.headers assert _jwt.headers == {'alg': 'HS256'} _rj = JWS() info = _rj.verify_compact(cas, [SYMKey(key=cli.client_secret)]) assert _eq(info.keys(), ["aud", "iss", "sub", "jti", "exp", "iat"])
def test_client_secret_jwt(): cli = Client("Foo") cli.token_endpoint = "https://example.com/token" cli.client_secret = "foobar" csj = ClientSecretJWT(cli) cis = AccessTokenRequest() http_args = csj.construct(cis, algorithm="HS256") print http_args assert cis["client_assertion_type"] == JWT_BEARER assert "client_assertion" in cis cas = cis["client_assertion"] header, claim, crypto, header_b64, claim_b64 = jwkest.unpack(cas) jso = json.loads(claim) assert _eq(jso.keys(), ["aud", "iss", "sub", "jti", "exp", "iat"]) print header assert header == {'alg': 'HS256'} _rj = JWS() info = _rj.verify_compact(cas, [SYM_key(key=cli.client_secret)]) _dict = json.loads(info) assert _eq(_dict.keys(), ["aud", "iss", "sub", "jti", "exp", "iat"])
def client(): cli = Client("A") cli.client_secret = "boarding pass" return cli
def client(): cli = Client(**CLIENT_CONF) cli.client_secret = "boarding pass" return cli
def client(): cli = Client(client_id="A", config={"issuer": "https://example.com/as"}) cli.client_secret = "boarding pass" return cli