work in progress: send user data to external identity provider (zitadel)
This commit is contained in:
		
							parent
							
								
									626ff6e673
								
							
						
					
					
						commit
						2698a578df
					
				
					 5 changed files with 46 additions and 7 deletions
				
			
		|  | @ -34,6 +34,7 @@ oidc_params = dict( | ||||||
|     op_config_url=oidc_provider + '/.well-known/openid-configuration', |     op_config_url=oidc_provider + '/.well-known/openid-configuration', | ||||||
|     op_uris=None, |     op_uris=None, | ||||||
|     op_keys=None, |     op_keys=None, | ||||||
|  |     op_project_scope='urn:zitadel:iam:org:project:id:zitadel:aud', | ||||||
|     callback_url=getenv('OIDC_CALLBACK_URL', base_url + '/auth/callback'), |     callback_url=getenv('OIDC_CALLBACK_URL', base_url + '/auth/callback'), | ||||||
|     client_id = oidc_client_id, |     client_id = oidc_client_id, | ||||||
|     cookie_name=getenv('OIDC_COOKIE_NAME', 'oidc_' + oidc_client_id), |     cookie_name=getenv('OIDC_COOKIE_NAME', 'oidc_' + oidc_client_id), | ||||||
|  | @ -43,7 +44,6 @@ oidc_params = dict( | ||||||
|     private_key_file=getenv('OIDC_SERVICE_USER_PRIVATE_KEY_FILE', '.private-key.json'), |     private_key_file=getenv('OIDC_SERVICE_USER_PRIVATE_KEY_FILE', '.private-key.json'), | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| # access zitadel API | oidc_provider_endpoints = dict( | ||||||
| zitadel_params = dict( |     user='v2/users/human', | ||||||
|     private_key_file=getenv('ZITADEL_SERVICE_USER_PRIVATE_KEY_FILE', '.private-key.json') |  | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  | @ -1,8 +1,43 @@ | ||||||
| # scopes.org.user | # scopes.org.user | ||||||
| 
 | 
 | ||||||
| """Basic user (principal) definitions + access to auth service (zitadel).""" | """Basic user account (principal) definitions + access to identity provider.""" | ||||||
| 
 | 
 | ||||||
| from scopes.web import client | from scopes.web import client | ||||||
| from scopes import util | from scopes import util | ||||||
| 
 | 
 | ||||||
| import config | import config | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | @dataclass | ||||||
|  | class User: | ||||||
|  | 
 | ||||||
|  |     name: str | ||||||
|  |     login: str | ||||||
|  |     email: str | ||||||
|  |     fullName: str | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | class ExtUser: | ||||||
|  |     """All infos for exchanging user data with an external service. | ||||||
|  | 
 | ||||||
|  |        This base class implements the zitadel interface. For other | ||||||
|  |        identity providers sublass accordingly. | ||||||
|  |     """ | ||||||
|  | 
 | ||||||
|  |     provider = 'zitatel' | ||||||
|  |     endpoints = dict( | ||||||
|  |             users='v2/users', | ||||||
|  |     ) | ||||||
|  | 
 | ||||||
|  |     def __init__(self, user, organization, userId=None, userIdPrefix=''): | ||||||
|  |         self.user = user | ||||||
|  | 
 | ||||||
|  |     def asDict(self): | ||||||
|  |         return dict(username=self.user.name) | ||||||
|  | 
 | ||||||
|  |     def send(self): | ||||||
|  |         clt = client.ApiClient(config.oidc_provider) | ||||||
|  |         data = self.asDict() | ||||||
|  |         res = clt.post(config.oidc_provider_endpoints['users'], data) | ||||||
|  | 
 | ||||||
|  |    grants: List[str] | ||||||
|  |  | ||||||
|  | @ -55,3 +55,7 @@ oidc_params = dict( | ||||||
|     cookie_crypt=getenv('OIDC_COOKIE_CRYPT', None), |     cookie_crypt=getenv('OIDC_COOKIE_CRYPT', None), | ||||||
|     private_key_file=getenv('OIDC_SERVICE_USER_PRIVATE_KEY_FILE', '.private-key.json'), |     private_key_file=getenv('OIDC_SERVICE_USER_PRIVATE_KEY_FILE', '.private-key.json'), | ||||||
| ) | ) | ||||||
|  | 
 | ||||||
|  | oidc_provider_endpoints = dict( | ||||||
|  |     user='v2/users/human', | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | @ -265,7 +265,7 @@ def authenticateClient(paramsName='oidc_params'): | ||||||
|                          headers=dict(alg='RS256', kid=keyId)) |                          headers=dict(alg='RS256', kid=keyId)) | ||||||
|     data = dict( |     data = dict( | ||||||
|             grant_type='urn:ietf:params:oauth:grant-type:jwt-bearer', |             grant_type='urn:ietf:params:oauth:grant-type:jwt-bearer', | ||||||
|             scope='openid urn:zitadel:iam:org:project:id:zitadel:aud', |             scope=' '.join(('openid', params['op_project_scope'])) | ||||||
|             assertion=jwToken, |             assertion=jwToken, | ||||||
|     ) |     ) | ||||||
|     headers = {'Content-Type': 'application/x-www-form-urlencoded'} |     headers = {'Content-Type': 'application/x-www-form-urlencoded'} | ||||||
|  | @ -276,7 +276,7 @@ def authenticateClient(paramsName='oidc_params'): | ||||||
|         logger.error('authenticateClient: %s', resp.text) |         logger.error('authenticateClient: %s', resp.text) | ||||||
|         return None |         return None | ||||||
|     tdata = resp.json() |     tdata = resp.json() | ||||||
|     print(tdata) |     #print(tdata) | ||||||
|     return tdata['access_token'] |     return tdata['access_token'] | ||||||
| 
 | 
 | ||||||
| def loadPrivateKeyData(fn='.private-key.json'): | def loadPrivateKeyData(fn='.private-key.json'): | ||||||
|  |  | ||||||
|  | @ -21,7 +21,7 @@ class ApiClient: | ||||||
|     def post(self, endpoint, data): |     def post(self, endpoint, data): | ||||||
|         headers = self.authentication() |         headers = self.authentication() | ||||||
|         # self.makeUrl(endpoint) |         # self.makeUrl(endpoint) | ||||||
|         url = '/'.join(self.bareUrl, endpoint) |         url = '/'.join(self.baseUrl, endpoint) | ||||||
|         resp = requests.post(url, data=data, headers=headers) |         resp = requests.post(url, data=data, headers=headers) | ||||||
|         # check: resp.status_code |         # check: resp.status_code | ||||||
|         data = resp.json() |         data = resp.json() | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue