Skip to content

refactor: Move auth extractors into authentication module - #1547

Merged
tpoliaw merged 1 commit into
mainfrom
auth-checks
Jun 23, 2026
Merged

tpoliaw merged 1 commit into
mainfrom
auth-checks

Conversation

@tpoliaw

@tpoliaw tpoliaw commented May 28, 2026

Copy link
Copy Markdown
Contributor

Splitting up the auth extractor method allows other extractors to access the raw token as well as easy access to the validated and decoded access token.

@tpoliaw
tpoliaw requested a review from a team as a code owner May 28, 2026 17:30
@codecov

codecov Bot commented May 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.70%. Comparing base (6d08154) to head (7fc8096).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1547      +/-   ##
==========================================
+ Coverage   95.69%   95.70%   +0.01%     
==========================================
  Files          43       43              
  Lines        3226     3240      +14     
==========================================
+ Hits         3087     3101      +14     
  Misses        139      139              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tpoliaw
tpoliaw changed the base branch from opa-client to main May 29, 2026 10:18
@tpoliaw
tpoliaw requested a review from ZohebShaikh June 2, 2026 16:03

@ZohebShaikh ZohebShaikh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Generally I think why write extra code when it is already been written by someone else ?

Comment on lines +282 to +287
"""Get bearer token value from authorization header"""
auth = req.headers.get("Authorization")
scheme, param = get_authorization_scheme_param(auth)
if scheme.casefold() != "bearer":
return None
return param.strip()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a copy of this

    authorization = request.headers.get("Authorization")
    scheme, param = get_authorization_scheme_param(authorization)
    if not authorization or scheme.lower() != "bearer":
        if self.auto_error:
            raise self.make_not_authenticated_error()
        else:
            return None  # pragma: nocover
    return param

Why not use the function already in fastAPI ?

After this PR you only use it at 1 more place.
A better thing to do would be to create oauth_scheme in main

oauth_scheme = OAuth2AuthorizationCodeBearer(
    authorizationUrl="",
    tokenUrl="",
    refreshUrl=None,
)

and do depend(oauth_scheme) to get the unchecked_access_token

To a certain extend I get that you want authN related stuff to be in the authentication.py file

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Why not use the function already in fastAPI ?

Because passing in null/placeholder values that aren't needed makes the code more confusing. It also inserts auth details into the API schema because this dependency is always on the endpoints even though is optional.

After this PR you only use it at 1 more place

For now yes. This was paving the way for the authz changes that need access to the unvalidated access_token

A better thing to do would be to create oauth_scheme in main

I think if it's in main we end up with circular imports. main imports authz imports main. Having all access token handling in main makes it end up doing everything. As you said, having authn stuff in the authn module makes it clearer.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks like a preference in doing things a certain way so will not block the merge, but having a link to the fastapi where it is being copied from and with some explanation will be a good thing

@ZohebShaikh
ZohebShaikh self-requested a review June 23, 2026 10:16
And split the auth check into two so that other methods can access the
raw bearer token if required.
@tpoliaw
tpoliaw added this pull request to the merge queue Jun 23, 2026
Merged via the queue into main with commit 38f0d89 Jun 23, 2026
19 checks passed
@tpoliaw
tpoliaw deleted the auth-checks branch June 23, 2026 14:10
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