-
Notifications
You must be signed in to change notification settings - Fork 166
feat: Add GetTokens, GetTokensByID, RevokeTokenByID methods #1255
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
Closed
Closed
Changes from 6 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
2d645f9
feat: add access token management APIs
legnoh e0c8835
refactor: wrap mock response in TokenInfos structure for GetTokens test
legnoh 0d568be
fix: update token structure to include Issuer field in GetTokens and …
legnoh bcf85b0
fix: update token tests to use Subject and Issuer fields instead of U…
legnoh 78cc2ce
refactor: remove unused createRefreshableAccessTokenParams function
legnoh f4f5af1
fix: update OrderBy field in GetTokensParams document
legnoh 6f189fb
refactor: remove LastUsed field from GetTokensParams and related logic
legnoh 726af38
fix: update response status check in RevokeTokenByID to handle NoContent
legnoh 4220434
Add retry to POST build scan trigger if needed (#1252)
attiasas 079241b
Xray Remediation Endpoints (#1233)
attiasas bf54a5c
JGC-427 - Skip broken repositories tests (#1256)
ehl-jf 1eb2f9c
Removed repo field in AQL query (#1258)
naveenku-jfrog 73f9f67
support for direct-download cmd (#1199)
reshmifrog 09b78c6
JGC-429 - Bump go 1.25 (#1259)
ehl-jf 9ccf6d8
Fix get details API (#1260)
dortam888 d3570d1
update build-info-go dependency (#1262)
reshmifrog 8194171
changing the build-info-go version
nitinp19 b347e35
Fix an issue that evidence encoded url twice resulting in a wrong url…
dortam888 c687c13
Xray Get Violations API (#1186)
attiasas f03db7b
JGC-000 - Remove auto label (#1266)
RemiBou c2ab78e
Fix application details resolution from api (#1268)
dortam888 e25eff5
On-demand log level to debug and set props recursively for props (#1269)
fluxxBot 9742f99
Git Integration POST Request (#1272)
orto17 67d0ca2
Fix Remediation after API breaking change (#1275)
attiasas 9798380
Fix: Handle Empty/Uninitialized .git Directory Gracefully (#1277)
agrasth 23fc9f0
Fix: Temp Directory Cleanup Continues on Permission Errors (#1276)
agrasth 8f9334e
malicious-code-scanner (#1080)
barv-jfrog 1b73690
Align config profile structure to recent changes in API body (#1274)
eranturgeman cbf1b0d
fixed TestXscSendGitIntegrationEvent (#1281)
orto17 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
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
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 |
|---|---|---|
|
|
@@ -3,12 +3,15 @@ package services | |
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "net/url" | ||
| "strconv" | ||
|
|
||
| "github.com/jfrog/jfrog-client-go/auth" | ||
| "github.com/jfrog/jfrog-client-go/http/jfroghttpclient" | ||
| clientutils "github.com/jfrog/jfrog-client-go/utils" | ||
| "github.com/jfrog/jfrog-client-go/utils/errorutils" | ||
| "github.com/jfrog/jfrog-client-go/utils/io/httputils" | ||
| "net/http" | ||
| ) | ||
|
|
||
| // #nosec G101 -- False positive - no hardcoded credentials. | ||
|
|
@@ -50,6 +53,32 @@ type CreateOidcTokenParams struct { | |
| ApplicationKey string `json:"application_key,omitempty"` | ||
| } | ||
|
|
||
| type GetTokensParams struct { | ||
| Description string `url:"description,omitempty"` | ||
| Username string `url:"username,omitempty"` | ||
| Refreshable *bool `url:"refreshable,omitempty"` | ||
| TokenId string `url:"token_id,omitempty"` | ||
| OrderBy string `url:"order_by,omitempty"` | ||
| DescendingOrder *bool `url:"descending_order,omitempty"` | ||
| LastUsed *int64 `url:"last_used,omitempty"` | ||
| } | ||
|
|
||
| type TokenInfos struct { | ||
| Tokens []TokenInfo `json:"tokens"` | ||
| } | ||
|
|
||
| type TokenInfo struct { | ||
| TokenId string `json:"token_id"` | ||
| Subject string `json:"subject"` | ||
| Expiry int64 `json:"expiry,omitempty"` | ||
| IssuedAt int64 `json:"issued_at"` | ||
| Issuer string `json:"issuer"` | ||
| Description string `json:"description,omitempty"` | ||
| Refreshable bool `json:"refreshable,omitempty"` | ||
| Scope string `json:"scope,omitempty"` | ||
| LastUsed int64 `json:"last_used,omitempty"` | ||
| } | ||
|
|
||
| func NewCreateTokenParams(params CreateTokenParams) CreateTokenParams { | ||
| return CreateTokenParams{CommonTokenParams: params.CommonTokenParams, IncludeReferenceToken: params.IncludeReferenceToken} | ||
| } | ||
|
|
@@ -127,6 +156,87 @@ func (ps *TokenService) ExchangeOidcToken(params CreateOidcTokenParams) (auth.Oi | |
| return tokenInfo, errorutils.CheckError(err) | ||
| } | ||
|
|
||
| func (ps *TokenService) GetTokens(params GetTokensParams) ([]TokenInfo, error) { | ||
| httpDetails := ps.ServiceDetails.CreateHttpClientDetails() | ||
| requestUrl := fmt.Sprintf("%s%s", ps.ServiceDetails.GetUrl(), tokensApi) | ||
|
|
||
| // Build query parameters manually | ||
| queryParams := url.Values{} | ||
| if params.Description != "" { | ||
| queryParams.Add("description", params.Description) | ||
| } | ||
| if params.Username != "" { | ||
| queryParams.Add("username", params.Username) | ||
| } | ||
| if params.Refreshable != nil { | ||
| queryParams.Add("refreshable", strconv.FormatBool(*params.Refreshable)) | ||
| } | ||
| if params.TokenId != "" { | ||
| queryParams.Add("token_id", params.TokenId) | ||
| } | ||
| if params.OrderBy != "" { | ||
| queryParams.Add("order_by", params.OrderBy) | ||
| } | ||
| if params.DescendingOrder != nil { | ||
| queryParams.Add("descending_order", strconv.FormatBool(*params.DescendingOrder)) | ||
| } | ||
| if params.LastUsed != nil { | ||
| queryParams.Add("last_used", strconv.FormatInt(*params.LastUsed, 10)) | ||
| } | ||
|
|
||
| if queryString := queryParams.Encode(); queryString != "" { | ||
| requestUrl += "?" + queryString | ||
| } | ||
|
|
||
| resp, body, _, err := ps.client.SendGet(requestUrl, true, &httpDetails) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| var tokenInfos TokenInfos | ||
| err = json.Unmarshal(body, &tokenInfos) | ||
| return tokenInfos.Tokens, errorutils.CheckError(err) | ||
| } | ||
|
|
||
| func (ps *TokenService) GetTokenByID(tokenId string) (*TokenInfo, error) { | ||
| if tokenId == "" { | ||
| return nil, errorutils.CheckErrorf("token ID cannot be empty") | ||
| } | ||
|
|
||
| httpDetails := ps.ServiceDetails.CreateHttpClientDetails() | ||
| url := fmt.Sprintf("%s%s/%s", ps.ServiceDetails.GetUrl(), tokensApi, tokenId) | ||
|
|
||
| resp, body, _, err := ps.client.SendGet(url, true, &httpDetails) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| var tokenInfo TokenInfo | ||
| err = json.Unmarshal(body, &tokenInfo) | ||
| return &tokenInfo, errorutils.CheckError(err) | ||
| } | ||
|
|
||
| func (ps *TokenService) RevokeTokenByID(tokenId string) error { | ||
| if tokenId == "" { | ||
| return errorutils.CheckErrorf("token ID cannot be empty") | ||
| } | ||
|
|
||
| httpDetails := ps.ServiceDetails.CreateHttpClientDetails() | ||
| url := fmt.Sprintf("%s%s/%s", ps.ServiceDetails.GetUrl(), tokensApi, tokenId) | ||
|
|
||
| resp, body, err := ps.client.SendDelete(url, nil, &httpDetails) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK) | ||
|
||
| } | ||
|
|
||
| func prepareForRefresh(p CreateTokenParams) (*CreateTokenParams, error) { | ||
| // Validate provided parameters | ||
| if p.RefreshToken == "" { | ||
|
|
||
Oops, something went wrong.
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.
last_used should be removed as it is not supported by the backend
Uh oh!
There was an error while loading. Please reload this page.
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.
When I look at the document below, it suggests in a
Notethat thelast_usedparameter is available under certain conditions — is that not correct?https://jfrog.com/help/r/jfrog-rest-apis/get-tokens
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.
The Note holds true for the response payload. But its not supported as query param
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.
Understood. I've made the correction, so please take a look.