fix: KeyError: 'token' in PrivateKey Authentication Mode#510
Open
agburch wants to merge 1 commit intookta:masterfrom
Open
fix: KeyError: 'token' in PrivateKey Authentication Mode#510agburch wants to merge 1 commit intookta:masterfrom
agburch wants to merge 1 commit intookta:masterfrom
Conversation
Contributor
Author
|
@BinoyOza-okta - could you take a look at this? Seems like a trivial fix, but this issue popped up recently (since v3.0.0) and broke some of our pipelines |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix KeyError: 'token' in PrivateKey Authentication Mode
Summary
This PR fixes a critical bug that prevents the Okta SDK from initializing when using PrivateKey (OAuth 2.0) authentication mode. The issue causes a
KeyError: 'token'exception during client initialization, making it impossible to use the OAuth 2.0 client credentials flow.Issue Fixed
KeyError: 'token' in ApiClient initialization
File:
okta/api_client.py(line 91)Problem:
When using
authorizationMode: 'PrivateKey', the SDK crashes during client initialization because it tries to access a requiredtokenfield that doesn't exist in the configuration.Root Cause:
PrivateKey authentication mode uses a private key to dynamically generate OAuth tokens via the client credentials flow. No pre-existing token is needed or expected in the configuration. The code incorrectly assumed a
tokenfield would always be present, regardless of authentication mode.Impact:
KeyError: 'token'during client initializationError Stack Trace
Testing
Before Fix
After Fix
Configuration Examples
PrivateKey Authentication (OAuth 2.0) - Now Works
SSWS Authentication (API Token) - Continues to Work
Both authentication modes now work correctly.
How Authentication Modes Work
The SDK supports two authentication modes:
tokenfield directly for authenticationprivateKeyto dynamically generate OAuth tokensThe fix ensures that:
authorizationModeis"PrivateKey", notokenis required (usesprivateKeyinstead)authorizationModeis"SSWS", thetokenfield is still used as beforeConfigurationclass properly handlesaccess_token=Nonefor PrivateKey modeCode Review Notes
Why
.get()withNoneis safe:Looking at
okta/configuration.py, theConfigurationclass already handlesaccess_token=None:And in
auth_settings():The SDK was already designed to handle
access_token=None, butapi_client.pywas incorrectly requiring thetokenfield to exist in the configuration dictionary.Related Documentation
Additional Notes
This bug likely went undetected because:
The fix uses a defensive programming practice (
dict.get()with a default value) that should have been used from the start, especially since the SDK already supports multiple authentication modes.