def test_srv2(): cc = ClaimsClient(client_id="client_1") cc.client_secret = "hemlig" req = cc.construct_UserClaimsRequest( request_args={ "sub": "diana", "claims_names": ["gender", "birthdate"] }) srv = ClaimsServer("pyoicserv", SessionDB(), CDB, USERINFO, verify_client, keyjar=KEYJAR, dist_claims_mode=ClaimsMode(USER2MODE)) srv.keyjar[""] = keybundle_from_local_file("rsa.key", "rsa", ["ver", "sig"]) assert srv resp = srv.claims_endpoint(req.to_urlencoded(), "") print resp.message ucr = UserClaimsResponse().deserialize(resp.message, "json") ucr.verify(keyjar=srv.keyjar) print ucr assert _eq(ucr["claims_names"], ["gender", "birthdate"]) assert "jwt" in ucr
def test_srv2(): cc = ClaimsClient(client_id="client_1") cc.client_secret = "hemlig" req = cc.construct_UserClaimsRequest( request_args={"sub": "diana", "claims_names": ["gender", "birthdate"]}) srv = ClaimsServer("pyoicserv", SessionDB("https://example.com"), CDB, USERINFO, verify_client, keyjar=KEYJAR, dist_claims_mode=ClaimsMode(USER2MODE)) srv.keyjar[""] = keybundle_from_local_file("%s/rsa.key" % BASE_PATH, "rsa", ["ver", "sig"]) assert srv resp = srv.claims_endpoint(req.to_urlencoded(), "") print resp.message ucr = UserClaimsResponse().deserialize(resp.message, "json") ucr.verify(keyjar=srv.keyjar) print ucr assert _eq(ucr["claims_names"], ["gender", "birthdate"]) assert "jwt" in ucr
def test_srv2(): cc = ClaimsClient(client_id="client_1") cc.client_secret="hemlig" req = cc.construct_UserClaimsRequest(request_args={"user_id": "diana", "claims_names":["gender", "birthdate"]}) srv = ClaimsServer("name", None, CDB, FUNCTIONS, USERDB) srv.keystore.set_sign_key(rsa_load("rsa.key"), "rsa") assert srv environ = BASE_ENVIRON.copy() environ["REQUEST_METHOD"] = "POST" txt = req.to_urlencoded() environ["CONTENT_LENGTH"] = len(txt) fil = StringIO.StringIO(buf=txt) environ["wsgi.input"] = fil resp = srv.claims_endpoint(environ, start_response, LOG()) print resp assert len(resp) == 1 ucr = UserClaimsResponse().deserialize(resp[0], "json") ucr.verify(key = srv.keystore.get_keys("sig", owner=None)) print ucr assert _eq(ucr["claims_names"], ["gender", "birthdate"]) assert "jwt" in ucr
def test_srv2(): cc = ClaimsClient(client_id="client_1") cc.client_secret="hemlig" req = cc.construct_UserClaimsRequest(request_args={"sub": "diana", "claims_names":["gender", "birthdate"]}) srv = ClaimsServer("name", None, CDB, FUNCTIONS, USERDB) srv.keyjar[""] = [KeyBundle(source="file://rsa.key", usage=["ver", "sig"])] assert srv environ = BASE_ENVIRON.copy() environ["REQUEST_METHOD"] = "POST" txt = req.to_urlencoded() environ["CONTENT_LENGTH"] = len(txt) fil = StringIO.StringIO(buf=txt) environ["wsgi.input"] = fil resp = srv.claims_endpoint(environ, start_response, LOG()) print resp assert len(resp) == 1 ucr = UserClaimsResponse().deserialize(resp[0], "json") ucr.verify(keyjar = srv.keyjar) print ucr assert _eq(ucr["claims_names"], ["gender", "birthdate"]) assert "jwt" in ucr
def create_claims_server(self, keyjar, session_db): self.srv = ClaimsServer("pyoicserv", session_db, TestClaimsServer.CDB, UserInfo(USERDB), verify_client, keyjar=keyjar, dist_claims_mode=ClaimsMode( TestClaimsServer.USER2MODE))
def create_claims_server(self, keyjar): self.srv = ClaimsServer("pyoicserv", SessionDB("https://example.com"), TestClaimsServer.CDB, UserInfo(USERDB), verify_client, keyjar=keyjar, dist_claims_mode=ClaimsMode( TestClaimsServer.USER2MODE))
class TestClaimsServer(object): USER2MODE = { "diana": "aggregate", "upper": "distribute", "babs": "aggregate" } CDB = {"client_1": {"client_secret": "hemlig"}} @pytest.fixture(autouse=True) def create_claims_server(self, keyjar, session_db): self.srv = ClaimsServer("pyoicserv", session_db, TestClaimsServer.CDB, UserInfo(USERDB), verify_client, keyjar=keyjar, dist_claims_mode=ClaimsMode( TestClaimsServer.USER2MODE)) def test_claims_endpoint(self): cc = ClaimsClient(client_id="client_1") cc.client_secret = "hemlig" req = cc.construct_UserClaimsRequest( request_args={ "sub": "diana", "claims_names": ["gender", "birthdate"] }) resp = self.srv.claims_endpoint(req.to_urlencoded(), "") ucr = UserClaimsResponse().deserialize(resp.message, "json") ucr.verify(keyjar=self.srv.keyjar) assert _eq(ucr["claims_names"], ["gender", "birthdate"]) assert "jwt" in ucr @pytest.fixture(scope="session") def keyjar(self): symkey = KeyBundle([{ "kty": "oct", "key": "abcdefghijklmnop", "use": "ver" }, { "kty": "oct", "key": "abcdefghijklmnop", "use": "sig" }]) base_path = os.path.abspath( os.path.join(os.path.dirname(__file__), "data/keys")) rsakey = keybundle_from_local_file( os.path.abspath(os.path.join(base_path, "rsa.key")), "rsa", ["ver", "sig"]) keyjar = KeyJar() keyjar["client1"] = [symkey, rsakey] keyjar[""] = rsakey return keyjar
class TestClaimsServer(object): USER2MODE = {"diana": "aggregate", "upper": "distribute", "babs": "aggregate"} CDB = { "client_1": {"client_secret": "hemlig"} } @pytest.fixture(autouse=True) def create_claims_server(self, keyjar, session_db): self.srv = ClaimsServer("pyoicserv", session_db, TestClaimsServer.CDB, UserInfo(USERDB), verify_client, keyjar=keyjar, dist_claims_mode=ClaimsMode( TestClaimsServer.USER2MODE)) def test_claims_endpoint(self): cc = ClaimsClient(client_id="client_1") cc.client_secret = "hemlig" req = cc.construct_UserClaimsRequest( request_args={"sub": "diana", "claims_names": ["gender", "birthdate"]}) resp = self.srv.claims_endpoint(req.to_urlencoded(), "") ucr = UserClaimsResponse().deserialize(resp.message, "json") ucr.verify(keyjar=self.srv.keyjar) assert _eq(ucr["claims_names"], ["gender", "birthdate"]) assert "jwt" in ucr @pytest.fixture(scope="session") def keyjar(self): symkey = KeyBundle( [{"kty": "oct", "key": "abcdefghijklmnop", "use": "ver"}, {"kty": "oct", "key": "abcdefghijklmnop", "use": "sig"}]) base_path = os.path.abspath( os.path.join(os.path.dirname(__file__), "data/keys")) rsakey = keybundle_from_local_file( os.path.abspath(os.path.join(base_path, "rsa.key")), "rsa", ["ver", "sig"]) keyjar = KeyJar() keyjar["client1"] = [symkey, rsakey] keyjar[""] = rsakey return keyjar
parser.add_argument('-v', dest='verbose', action='store_true') parser.add_argument('-d', dest='debug', action='store_true') parser.add_argument('-p', dest='port', default=8093, type=int) parser.add_argument(dest="config") args = parser.parse_args() cdb = json.loads(open("claims_client.json").read()) userinfo = UserInfo(USERDB) # in memory session storage config = json.loads(open(args.config).read()) sdb = create_session_db(config["issuer"], config["SESSION_KEY"], password="******") OAS = ClaimsServer(config["issuer"], sdb, cdb, userinfo, verify_client) if "keys" in config: for typ, info in config["keys"].items(): OAS.keyjar.add_kb("", keybundle_from_local_file(info["key"], "rsa", ["ver", "sig"])) try: OAS.jwks_uri.append(info["jwk"]) except KeyError: pass #print URLS if args.debug: OAS.debug = True OAS.endp = ENDPOINTS
parser.add_argument('-v', dest='verbose', action='store_true') parser.add_argument('-d', dest='debug', action='store_true') parser.add_argument('-p', dest='port', default=8093, type=int) parser.add_argument(dest="config") args = parser.parse_args() cdb = json.loads(open("claims_client.json").read()) userinfo = UserInfo(USERDB) # in memory session storage config = json.loads(open(args.config).read()) sdb = create_session_db(config["issuer"], config["SESSION_KEY"], password="******") OAS = ClaimsServer(config["issuer"], sdb, cdb, userinfo, verify_client) if "keys" in config: for typ, info in config["keys"].items(): OAS.keyjar.add_kb("", keybundle_from_local_file(info["key"], "rsa", ["ver", "sig"])) try: OAS.jwks_uri.append(info["jwk"]) except KeyError: pass #print URLS if args.debug: OAS.debug = True OAS.endpoints = ENDPOINTS
def verify_client(env, req, cdb): return True FUNCTIONS = { "verify_client": verify_client, "userinfo": user_info } cc = ClaimsClient(client_id="client_1") cc.client_secret="hemlig" req = cc.construct_UserClaimsRequest(request_args={"user_id": "diana", "claims_names":["gender", "birthdate"]}) srv = ClaimsServer("name", None, CDB, FUNCTIONS, USERDB) srv.keyjar[""] = [KeyChain(source="file://rsa.key", usage=["ver", "sig"])] assert srv environ = BASE_ENVIRON.copy() environ["REQUEST_METHOD"] = "POST" txt = req.to_urlencoded() environ["CONTENT_LENGTH"] = len(txt) fil = StringIO.StringIO(buf=txt) environ["wsgi.input"] = fil resp = srv.claims_endpoint(environ, start_response, LOG()) print resp assert len(resp) == 1