2525}
2626
2727
28- def available_scopes (endpoint_context ):
29- _supported = endpoint_context .provider_info .get ("scopes_supported" )
30- if _supported :
31- return [s for s in endpoint_context .scope2claims .keys () if s in _supported ]
32- else :
33- return [s for s in endpoint_context .scope2claims .keys ()]
34-
35-
3628def convert_scopes2claims (scopes , allowed_claims = None , scope2claim_map = None ):
3729 scope2claim_map = scope2claim_map or SCOPE2CLAIMS
3830
@@ -53,26 +45,55 @@ def convert_scopes2claims(scopes, allowed_claims=None, scope2claim_map=None):
5345
5446
5547class Scopes :
56- def __init__ (self ):
57- pass
48+ def __init__ (self , server_get , allowed_scopes = None , scopes_mapping = None ):
49+ self .server_get = server_get
50+ if not scopes_mapping :
51+ scopes_mapping = dict (SCOPE2CLAIMS )
52+ self .scopes_mapping = scopes_mapping
53+ if not allowed_scopes :
54+ allowed_scopes = list (scopes_mapping .keys ())
55+ self .allowed_scopes = allowed_scopes
5856
59- def allowed_scopes (self , client_id , endpoint_context ):
57+ def get_allowed_scopes (self , client_id = None ):
6058 """
6159 Returns the set of scopes that a specific client can use.
6260
6361 :param client_id: The client identifier
64- :param endpoint_context: A EndpointContext instance
6562 :returns: List of scope names. Can be empty.
6663 """
67- _cli = endpoint_context .cdb .get (client_id )
68- if _cli is not None :
69- _scopes = _cli .get ("allowed_scopes" )
70- if _scopes :
71- return _scopes
72- else :
73- return available_scopes (endpoint_context )
74- return []
75-
76- def filter_scopes (self , client_id , endpoint_context , scopes ):
77- allowed_scopes = self .allowed_scopes (client_id , endpoint_context )
64+ allowed_scopes = self .allowed_scopes
65+ if client_id :
66+ client = self .server_get ("endpoint_context" ).cdb .get (client_id )
67+ if client is not None :
68+ if "allowed_scopes" in client :
69+ allowed_scopes = client .get ("allowed_scopes" )
70+ elif "scopes_mapping" in client :
71+ allowed_scopes = list (client .get ("scopes_mapping" ).keys ())
72+
73+ return allowed_scopes
74+
75+ def get_scopes_mapping (self , client_id = None ):
76+ """
77+ Returns the mapping of scopes to claims fora specific client.
78+
79+ :param client_id: The client identifier
80+ :returns: Dict of scopes to claims. Can be empty.
81+ """
82+ scopes_mapping = self .scopes_mapping
83+ if client_id :
84+ client = self .server_get ("endpoint_context" ).cdb .get (client_id )
85+ if client is not None :
86+ scopes_mapping = client .get ("scopes_mapping" , scopes_mapping )
87+ return scopes_mapping
88+
89+ def filter_scopes (self , scopes , client_id = None ):
90+ allowed_scopes = self .get_allowed_scopes (client_id )
7891 return [s for s in scopes if s in allowed_scopes ]
92+
93+ def scopes_to_claims (self , scopes , scopes_mapping = None , client_id = None ):
94+ if not scopes_mapping :
95+ scopes_mapping = self .get_scopes_mapping (client_id )
96+
97+ scopes = self .filter_scopes (scopes , client_id )
98+
99+ return convert_scopes2claims (scopes , scope2claim_map = scopes_mapping )
0 commit comments