-
Notifications
You must be signed in to change notification settings - Fork 13
Extract orgID from claim #321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+147
−16
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
075c74b
Add fetch for tenant config and write context file
timothyF95 37851ef
Merge branch 'main' into add-context-file-fetch
timothyF95 a5ab4d0
Update based on new api changes
timothyF95 86f55d7
Merge branch 'main' into add-context-file-fetch
timothyF95 1caba32
lint
timothyF95 98a5b95
Merge branch 'main' into add-context-file-fetch
timothyF95 ee6fec2
Adjust for latest api changes
timothyF95 fec7d66
add support for api key
timothyF95 f3bb708
Clean up
timothyF95 4acdad8
Extract orgID from claim
timothyF95 dc9e013
Merge branch 'main' into extract-orgid-from-claim
timothyF95 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -105,42 +105,66 @@ func SaveCredentials(tokenSet *CreLoginTokenSet) error { | |
| return nil | ||
| } | ||
|
|
||
| // GetDeploymentAccessStatus returns the deployment access status for the organization. | ||
| // This can be used to check and display whether the user has deployment access. | ||
| func (c *Credentials) GetDeploymentAccessStatus() (*DeploymentAccess, error) { | ||
| // API keys can only be generated on ungated organizations, so they always have access | ||
| if c.AuthType == AuthTypeApiKey { | ||
| return &DeploymentAccess{ | ||
| HasAccess: true, | ||
| Status: DeploymentAccessStatusFullAccess, | ||
| }, nil | ||
| } | ||
|
|
||
| // For JWT bearer tokens, we need to parse the token and check the organization_status claim | ||
| // decodeJWTClaims extracts the claims map from the access token JWT payload. | ||
| func (c *Credentials) decodeJWTClaims() (map[string]interface{}, error) { | ||
| if c.Tokens == nil || c.Tokens.AccessToken == "" { | ||
| return nil, fmt.Errorf("no access token available") | ||
| } | ||
|
|
||
| // Parse the JWT to extract claims | ||
| parts := strings.Split(c.Tokens.AccessToken, ".") | ||
| if len(parts) < 2 { | ||
| return nil, fmt.Errorf("invalid JWT token format") | ||
| } | ||
|
|
||
| // Decode the payload (second part of the JWT) | ||
| payload, err := base64.RawURLEncoding.DecodeString(parts[1]) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to decode JWT payload: %w", err) | ||
| } | ||
|
|
||
| // Parse claims into a map | ||
| var claims map[string]interface{} | ||
| if err := json.Unmarshal(payload, &claims); err != nil { | ||
| return nil, fmt.Errorf("failed to unmarshal JWT claims: %w", err) | ||
| } | ||
|
|
||
| // Log all claims for debugging | ||
| c.log.Debug().Interface("claims", claims).Msg("JWT claims decoded") | ||
| return claims, nil | ||
| } | ||
|
|
||
| // GetOrgID returns the organization ID from the access token. | ||
| func (c *Credentials) GetOrgID() (string, error) { | ||
| if c.AuthType == AuthTypeApiKey { | ||
| return "", fmt.Errorf("org_id is not available for API key authentication") | ||
| } | ||
|
|
||
| claims, err := c.decodeJWTClaims() | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| orgID, ok := claims["org_id"].(string) | ||
| if !ok || orgID == "" { | ||
| return "", fmt.Errorf("org_id claim not found in access token") | ||
| } | ||
|
|
||
| return orgID, nil | ||
| } | ||
|
|
||
| // GetDeploymentAccessStatus returns the deployment access status for the organization. | ||
| // This can be used to check and display whether the user has deployment access. | ||
| func (c *Credentials) GetDeploymentAccessStatus() (*DeploymentAccess, error) { | ||
| // API keys can only be generated on ungated organizations, so they always have access | ||
| if c.AuthType == AuthTypeApiKey { | ||
| return &DeploymentAccess{ | ||
| HasAccess: true, | ||
| Status: DeploymentAccessStatusFullAccess, | ||
| }, nil | ||
| } | ||
|
|
||
| // For JWT bearer tokens, we need to parse the token and check the organization_status claim | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And where do we check organization_status?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment preexists, it has just been moved |
||
| claims, err := c.decodeJWTClaims() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // Dynamically find the organization_status claim by looking for any key ending with "organization_status" | ||
| var orgStatus string | ||
|
|
||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably not needed now, but we will likely have to add support for machine-to-machine authentication via API key in the future
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, i looked into this but it's not relevant to the current feature and requires some more thought as the orgID cannot be extracted from the API_KEY
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case, you need to call the
verifyApiKeyquery on GQL and the orgID will be in the response payload. But you can add this in the follow-up ticket.