-
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
Changes from 5 commits
2d645f9
e0c8835
0d568be
bcf85b0
78cc2ce
f4f5af1
6f189fb
726af38
4220434
079241b
bf54a5c
1eb2f9c
73f9f67
09b78c6
9ccf6d8
d3570d1
8194171
b347e35
c687c13
f03db7b
c2ab78e
e25eff5
9742f99
67d0ca2
9798380
23fc9f0
8f9334e
1b73690
cbf1b0d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 == "" { | ||
|
|
||
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.
// Order by field (created,token_id,owner,subject,expiry)
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.
You're right — the information was inaccurate. I'll correct it!
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.
I've made the correction!