| 
														
															@@ -6,6 +6,7 @@ import requests 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 from flask import current_app, redirect, request 
														 | 
														
														 | 
														
															 from flask import current_app, redirect, request 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 from flask_restful import Resource 
														 | 
														
														 | 
														
															 from flask_restful import Resource 
														 | 
													
												
											
												
													
														| 
														 | 
														
															  
														 | 
														
														 | 
														
															  
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+from configs import dify_config 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 from constants.languages import languages 
														 | 
														
														 | 
														
															 from constants.languages import languages 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 from extensions.ext_database import db 
														 | 
														
														 | 
														
															 from extensions.ext_database import db 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 from libs.helper import get_remote_ip 
														 | 
														
														 | 
														
															 from libs.helper import get_remote_ip 
														 | 
													
												
											
										
											
												
													
														 | 
														
															@@ -18,22 +19,24 @@ from .. import api 
														 | 
													
												
											
												
													
														| 
														 | 
														
															  
														 | 
														
														 | 
														
															  
														 | 
													
												
											
												
													
														| 
														 | 
														
															 def get_oauth_providers(): 
														 | 
														
														 | 
														
															 def get_oauth_providers(): 
														 | 
													
												
											
												
													
														| 
														 | 
														
															     with current_app.app_context(): 
														 | 
														
														 | 
														
															     with current_app.app_context(): 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-        github_oauth = GitHubOAuth(client_id=current_app.config.get('GITHUB_CLIENT_ID'), 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-                                   client_secret=current_app.config.get( 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-                                       'GITHUB_CLIENT_SECRET'), 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-                                   redirect_uri=current_app.config.get( 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-                                       'CONSOLE_API_URL') + '/console/api/oauth/authorize/github') 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															- 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-        google_oauth = GoogleOAuth(client_id=current_app.config.get('GOOGLE_CLIENT_ID'), 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-                                   client_secret=current_app.config.get( 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-                                       'GOOGLE_CLIENT_SECRET'), 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-                                   redirect_uri=current_app.config.get( 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-                                       'CONSOLE_API_URL') + '/console/api/oauth/authorize/google') 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															- 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-        OAUTH_PROVIDERS = { 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-            'github': github_oauth, 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-            'google': google_oauth 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-        } 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+        if not dify_config.GITHUB_CLIENT_ID or not dify_config.GITHUB_CLIENT_SECRET: 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+            github_oauth = None 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+        else: 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+            github_oauth = GitHubOAuth( 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+                client_id=dify_config.GITHUB_CLIENT_ID, 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+                client_secret=dify_config.GITHUB_CLIENT_SECRET, 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+                redirect_uri=dify_config.CONSOLE_API_URL + '/console/api/oauth/authorize/github', 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+            ) 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+        if not dify_config.GOOGLE_CLIENT_ID or not dify_config.GOOGLE_CLIENT_SECRET: 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+            google_oauth = None 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+        else: 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+            google_oauth = GoogleOAuth( 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+                client_id=dify_config.GOOGLE_CLIENT_ID, 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+                client_secret=dify_config.GOOGLE_CLIENT_SECRET, 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+                redirect_uri=dify_config.CONSOLE_API_URL + '/console/api/oauth/authorize/google', 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+            ) 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+ 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+        OAUTH_PROVIDERS = {'github': github_oauth, 'google': google_oauth} 
														 | 
													
												
											
												
													
														| 
														 | 
														
															         return OAUTH_PROVIDERS 
														 | 
														
														 | 
														
															         return OAUTH_PROVIDERS 
														 | 
													
												
											
												
													
														| 
														 | 
														
															  
														 | 
														
														 | 
														
															  
														 | 
													
												
											
												
													
														| 
														 | 
														
															  
														 | 
														
														 | 
														
															  
														 | 
													
												
											
										
											
												
													
														 | 
														
															@@ -63,8 +66,7 @@ class OAuthCallback(Resource): 
														 | 
													
												
											
												
													
														| 
														 | 
														
															             token = oauth_provider.get_access_token(code) 
														 | 
														
														 | 
														
															             token = oauth_provider.get_access_token(code) 
														 | 
													
												
											
												
													
														| 
														 | 
														
															             user_info = oauth_provider.get_user_info(token) 
														 | 
														
														 | 
														
															             user_info = oauth_provider.get_user_info(token) 
														 | 
													
												
											
												
													
														| 
														 | 
														
															         except requests.exceptions.HTTPError as e: 
														 | 
														
														 | 
														
															         except requests.exceptions.HTTPError as e: 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-            logging.exception( 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-                f"An error occurred during the OAuth process with {provider}: {e.response.text}") 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+            logging.exception(f'An error occurred during the OAuth process with {provider}: {e.response.text}') 
														 | 
													
												
											
												
													
														| 
														 | 
														
															             return {'error': 'OAuth process failed'}, 400 
														 | 
														
														 | 
														
															             return {'error': 'OAuth process failed'}, 400 
														 | 
													
												
											
												
													
														| 
														 | 
														
															  
														 | 
														
														 | 
														
															  
														 | 
													
												
											
												
													
														| 
														 | 
														
															         account = _generate_account(provider, user_info) 
														 | 
														
														 | 
														
															         account = _generate_account(provider, user_info) 
														 | 
													
												
											
										
											
												
													
														 | 
														
															@@ -81,7 +83,7 @@ class OAuthCallback(Resource): 
														 | 
													
												
											
												
													
														| 
														 | 
														
															  
														 | 
														
														 | 
														
															  
														 | 
													
												
											
												
													
														| 
														 | 
														
															         token = AccountService.login(account, ip_address=get_remote_ip(request)) 
														 | 
														
														 | 
														
															         token = AccountService.login(account, ip_address=get_remote_ip(request)) 
														 | 
													
												
											
												
													
														| 
														 | 
														
															  
														 | 
														
														 | 
														
															  
														 | 
													
												
											
												
													
														| 
														 | 
														
															-        return redirect(f'{current_app.config.get("CONSOLE_WEB_URL")}?console_token={token}') 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+        return redirect(f'{dify_config.CONSOLE_WEB_URL}?console_token={token}') 
														 | 
													
												
											
												
													
														| 
														 | 
														
															  
														 | 
														
														 | 
														
															  
														 | 
													
												
											
												
													
														| 
														 | 
														
															  
														 | 
														
														 | 
														
															  
														 | 
													
												
											
												
													
														| 
														 | 
														
															 def _get_account_by_openid_or_email(provider: str, user_info: OAuthUserInfo) -> Optional[Account]: 
														 | 
														
														 | 
														
															 def _get_account_by_openid_or_email(provider: str, user_info: OAuthUserInfo) -> Optional[Account]: 
														 | 
													
												
											
										
											
												
													
														 | 
														
															@@ -101,11 +103,7 @@ def _generate_account(provider: str, user_info: OAuthUserInfo): 
														 | 
													
												
											
												
													
														| 
														 | 
														
															         # Create account 
														 | 
														
														 | 
														
															         # Create account 
														 | 
													
												
											
												
													
														| 
														 | 
														
															         account_name = user_info.name if user_info.name else 'Dify' 
														 | 
														
														 | 
														
															         account_name = user_info.name if user_info.name else 'Dify' 
														 | 
													
												
											
												
													
														| 
														 | 
														
															         account = RegisterService.register( 
														 | 
														
														 | 
														
															         account = RegisterService.register( 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-            email=user_info.email, 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-            name=account_name, 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-            password=None, 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-            open_id=user_info.id, 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-            provider=provider 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+            email=user_info.email, name=account_name, password=None, open_id=user_info.id, provider=provider 
														 | 
													
												
											
												
													
														| 
														 | 
														
															         ) 
														 | 
														
														 | 
														
															         ) 
														 | 
													
												
											
												
													
														| 
														 | 
														
															  
														 | 
														
														 | 
														
															  
														 | 
													
												
											
												
													
														| 
														 | 
														
															         # Set interface language 
														 | 
														
														 | 
														
															         # Set interface language 
														 |