Skip to content

Fix api access to archi app#522

Open
hassan11196 wants to merge 3 commits intoarchi-physics:mainfrom
hassan11196:fix-api-access
Open

Fix api access to archi app#522
hassan11196 wants to merge 3 commits intoarchi-physics:mainfrom
hassan11196:fix-api-access

Conversation

@hassan11196
Copy link
Copy Markdown
Collaborator

Summary

  • Add Bearer token authentication support for programmatic API access via Authorization: Bearer <token> header
  • Auto-upsert user into users table on Bearer auth to prevent FK violations on conversation_metadata
  • Fix AttributeError on current_model_used by referencing self.chat.current_model_used instead of self.current_model_used
  • Return 401 JSON for /api/ routes instead of redirecting to /login

@hassan11196 hassan11196 changed the title Fix api access Fix api access to archi app Mar 15, 2026
@swinney swinney requested a review from Copilot March 16, 2026 18:41
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the chat app’s authentication flow to support programmatic access via Authorization: Bearer <token> while keeping existing session-based SSO behavior, and fixes a response payload attribute reference.

Changes:

  • Added Bearer token authentication that decodes JWT claims, derives roles, and populates the Flask session (including user upsert into users).
  • Updated auth decorators to return JSON 401 for /api/ routes instead of redirecting to /login.
  • Fixed model_used reporting by referencing self.chat.current_model_used.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +2535 to +2538
claims = decode_jwt_claims(token_string)
if not claims:
return False

Comment on lines +2569 to +2577
# Populate the session so all downstream code works unchanged
self._set_user_session(
email=email,
name=name,
username=username,
user_id=user_id,
auth_method='bearer',
roles=user_roles
)
Comment on lines 2608 to +2629
@@ -2542,16 +2617,18 @@ def decorated_function(*args, **kwargs):
method='web',
details=f"path={request.path}, method={request.method}"
)
# For API requests return 401 instead of redirect
if request.path.startswith('/api/'):
return jsonify({'error': 'Unauthorized', 'message': 'Authentication required'}), 401
# Redirect to login page which will trigger SSO
return redirect(url_for('login'))

# Return 401 Unauthorized response for API requests
return jsonify({'error': 'Unauthorized', 'message': 'Authentication required'}), 401
if request.path.startswith('/api/'):
return jsonify({'error': 'Unauthorized', 'message': 'Authentication required'}), 401
else:
else:
Comment on lines +2530 to +2534
auth_header = request.headers.get('Authorization', '')
if not auth_header.startswith('Bearer '):
return False

token_string = auth_header[7:]
Comment on lines +2555 to +2565
# Upsert user into the users table so that conversation_metadata
# can reference user_id via the FK constraint.
if user_id:
try:
user_service = UserService(pg_config=self.pg_config)
user_service.get_or_create_user(
user_id=user_id,
auth_provider='sso',
display_name=name,
email=email,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants