Skip to content

Commit 8c47d05

Browse files
cparkinscparkins
cparkins
authored and
cparkins
committed
microsoft#138: Add the /tokens endpoints to create/update/revoke/list Personal Access Tokens made available in API v7.2-preview1.
1 parent c9e5fa0 commit 8c47d05

File tree

2 files changed

+362
-0
lines changed

2 files changed

+362
-0
lines changed

azuredevops/v7/tokens/client.go

+189
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
// --------------------------------------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// Licensed under the MIT License.
4+
// --------------------------------------------------------------------------------------------
5+
// Generated file, DO NOT EDIT
6+
// Changes may cause incorrect behavior and will be lost if the code is regenerated.
7+
// --------------------------------------------------------------------------------------------
8+
9+
package tokens
10+
11+
import (
12+
"bytes"
13+
"context"
14+
"encoding/json"
15+
"github.com/google/uuid"
16+
"github.com/microsoft/azure-devops-go-api/azuredevops/v7"
17+
"net/http"
18+
"net/url"
19+
"strconv"
20+
)
21+
22+
var ResourceAreaId, _ = uuid.Parse("55967393-20ef-45c6-a96c-b5d5d5986a9a")
23+
24+
type Client interface {
25+
// [Preview API] Creates a new personal access token (PAT) for the requesting user.
26+
CreatePat(context.Context, CreatePatArgs) (*PatTokenResult, error)
27+
// [Preview API] Gets a single personal access token (PAT).
28+
GetPat(context.Context, GetPatArgs) (*PatTokenResult, error)
29+
// [Preview API] Revokes a personal access token (PAT) by authorizationId.
30+
Revoke(context.Context, RevokeArgs) error
31+
// [Preview API] Updates an existing personal access token (PAT) with the new parameters. To update a token, it must be valid (has not been revoked).
32+
UpdatePat(context.Context, UpdatePatArgs) (*PatTokenResult, error)
33+
// [Preview API] Gets a paged list of personal access tokens (PATs) created in this organization. Subsequent calls to the API require the same filtering options to be supplied.
34+
ListPats(context.Context, ListPatsArgs) (*PagedPatResults, error)
35+
}
36+
37+
type ClientImpl struct {
38+
Client azuredevops.Client
39+
}
40+
41+
func NewClient(ctx context.Context, connection *azuredevops.Connection) (Client, error) {
42+
client, err := connection.GetClientByResourceAreaId(ctx, ResourceAreaId)
43+
if err != nil {
44+
return nil, err
45+
}
46+
return &ClientImpl{
47+
Client: *client,
48+
}, nil
49+
}
50+
51+
// [Preview API] Creates a new personal access token (PAT) for the requesting user.
52+
func (client *ClientImpl) CreatePat(ctx context.Context, args CreatePatArgs) (*PatTokenResult, error) {
53+
if args.Token == nil {
54+
return nil, &azuredevops.ArgumentNilError{ArgumentName: "args.Token"}
55+
}
56+
57+
body, marshalErr := json.Marshal(*args.Token)
58+
if marshalErr != nil {
59+
return nil, marshalErr
60+
}
61+
locationId, _ := uuid.Parse("55967393-20ef-45c6-a96c-b5d5d5986a9a")
62+
resp, err := client.Client.Send(ctx, http.MethodPost, locationId, "7.2-preview.1", nil, nil, bytes.NewReader(body), "application/json", "application/json", nil)
63+
if err != nil {
64+
return nil, err
65+
}
66+
67+
var responseValue PatTokenResult
68+
err = client.Client.UnmarshalBody(resp, &responseValue)
69+
return &responseValue, err
70+
}
71+
72+
type CreatePatArgs struct {
73+
// (required) The request parameters for creating a personal access token (PAT)
74+
Token *PatTokenCreateRequest
75+
}
76+
77+
// [Preview API] Gets a single personal access token (PAT).
78+
func (client *ClientImpl) GetPat(ctx context.Context, args GetPatArgs) (*PatTokenResult, error) {
79+
if args.AuthorizationId == nil {
80+
return nil, &azuredevops.ArgumentNilError{ArgumentName: "args.AuthorizationId"}
81+
}
82+
83+
queryParams := url.Values{}
84+
queryParams.Add("authorizationId", (*args.AuthorizationId).String())
85+
locationId, _ := uuid.Parse("55967393-20ef-45c6-a96c-b5d5d5986a9a")
86+
resp, err := client.Client.Send(ctx, http.MethodGet, locationId, "7.2-preview.1", nil, queryParams, nil, "", "application/json", nil)
87+
if err != nil {
88+
return nil, err
89+
}
90+
91+
var responseValue PatTokenResult
92+
err = client.Client.UnmarshalBody(resp, &responseValue)
93+
return &responseValue, err
94+
}
95+
96+
type GetPatArgs struct {
97+
// (required) The authorizationId identifying a single, unique personal access token (PAT).
98+
AuthorizationId *uuid.UUID
99+
}
100+
101+
// [Preview API] Revokes a personal access token (PAT) by authorizationId.
102+
func (client *ClientImpl) Revoke(ctx context.Context, args RevokeArgs) error {
103+
if args.AuthorizationId == nil {
104+
return &azuredevops.ArgumentNilError{ArgumentName: "args.AuthorizationId"}
105+
}
106+
107+
queryParams := url.Values{}
108+
queryParams.Add("authorizationId", (*args.AuthorizationId).String())
109+
locationId, _ := uuid.Parse("55967393-20ef-45c6-a96c-b5d5d5986a9a")
110+
_, err := client.Client.Send(ctx, http.MethodDelete, locationId, "7.2-preview.1", nil, queryParams, nil, "", "application/json", nil)
111+
if err != nil {
112+
return err
113+
}
114+
115+
return nil
116+
}
117+
118+
type RevokeArgs struct {
119+
// (required) The authorizationId identifying a single, unique personal access token (PAT).
120+
AuthorizationId *uuid.UUID
121+
}
122+
123+
func (client *ClientImpl) UpdatePat(ctx context.Context, args UpdatePatArgs) (*PatTokenResult, error) {
124+
if args.Token == nil {
125+
return nil, &azuredevops.ArgumentNilError{ArgumentName: "args.Body"}
126+
}
127+
128+
body, marshalErr := json.Marshal(*args.Token)
129+
if marshalErr != nil {
130+
return nil, marshalErr
131+
}
132+
locationId, _ := uuid.Parse("55967393-20ef-45c6-a96c-b5d5d5986a9a")
133+
resp, err := client.Client.Send(ctx, http.MethodPut, locationId, "7.2-preview.1", nil, nil, bytes.NewReader(body), "application/json", "application/json", nil)
134+
if err != nil {
135+
return nil, err
136+
}
137+
138+
var responseValue PatTokenResult
139+
err = client.Client.UnmarshalBody(resp, &responseValue)
140+
return &responseValue, err
141+
}
142+
143+
type UpdatePatArgs struct {
144+
// (required) The authorizationId identifying a single, unique personal access token (PAT).
145+
Token *PatTokenUpdateRequest
146+
}
147+
148+
// [Preview API] Lists of all the session token details of the personal access tokens (PATs) for a particular user.
149+
func (client *ClientImpl) ListPats(ctx context.Context, args ListPatsArgs) (*PagedPatResults, error) {
150+
queryParams := url.Values{}
151+
if args.DisplayFilterOption != nil && *args.DisplayFilterOption != "" {
152+
queryParams.Add("displayFilterOption", string(*args.DisplayFilterOption))
153+
}
154+
if args.SortByOption != nil && *args.SortByOption != "" {
155+
queryParams.Add("sortByOption", string(*args.SortByOption))
156+
}
157+
if args.IsSortAscending != nil {
158+
queryParams.Add("isSortAscending", strconv.FormatBool(*args.IsSortAscending))
159+
}
160+
if args.ContinuationToken != nil && *args.ContinuationToken != "" {
161+
queryParams.Add("continuationToken", *args.ContinuationToken)
162+
}
163+
if args.Top != nil {
164+
queryParams.Add("$top", strconv.Itoa(*args.Top))
165+
}
166+
locationId, _ := uuid.Parse("55967393-20ef-45c6-a96c-b5d5d5986a9a")
167+
resp, err := client.Client.Send(ctx, http.MethodGet, locationId, "7.1-preview.1", nil, queryParams, nil, "", "application/json", nil)
168+
if err != nil {
169+
return nil, err
170+
}
171+
172+
var responseValue PagedPatResults
173+
err = client.Client.UnmarshalBody(resp, &responseValue)
174+
return &responseValue, err
175+
}
176+
177+
// Arguments for the ListPersonalAccessTokens function
178+
type ListPatsArgs struct {
179+
// (Optional) Refers to the status of the personal access token (PAT)
180+
DisplayFilterOption *DisplayFilterOption
181+
// (Optional) Which field to sort by
182+
SortByOption *SortByOption
183+
// (Optional) Ascending or descending
184+
IsSortAscending *bool
185+
// (Optional) Where to start returning tokens from
186+
ContinuationToken *string
187+
// (Optional) How many tokens to return, limit of 100
188+
Top *int
189+
}

azuredevops/v7/tokens/models.go

+173
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
package tokens
2+
3+
import (
4+
"github.com/google/uuid"
5+
"github.com/microsoft/azure-devops-go-api/azuredevops/v7"
6+
)
7+
8+
// PatTokenCreateRequest encapsulates the request parameters for creating a new personal access token (PAT)
9+
type PatTokenCreateRequest struct {
10+
// True, if this personal access token (PAT) is for all of the user's accessible organizations. False, if otherwise (e.g. if the token is for a specific organization)
11+
AllOrgs *bool `json:"allOrgs,omitempty"`
12+
// The token name
13+
DisplayName *string `json:"displayName,omitempty"`
14+
// The token scopes for accessing Azure DevOps resources
15+
Scope *string `json:"scope,omitempty"`
16+
// The token expiration date. If the "Enforce maximum personal access token lifespan" policy is enabled and the provided token expiration date is past the maximum allowed lifespan, it will return back a PAT with a validTo date equal to the current date + maximum allowed lifespan.
17+
ValidTo *azuredevops.Time `json:"validTo,omitempty"`
18+
}
19+
20+
// PatTokenResult contains the resulting personal access token (PAT) and the error (if any) that occurred during the operation
21+
type PatTokenResult struct {
22+
// Represents a personal access token (PAT) used to access Azure DevOps resources
23+
PatToken PatToken `json:"patToken,omitempty"`
24+
// The error (if any) that occurred
25+
PatTokenError *PatTokenError `json:"patTokenError,omitempty"`
26+
}
27+
28+
// PatToken represents a personal access token (PAT) used to access Azure DevOps resources
29+
type PatToken struct {
30+
// Unique guid identifier
31+
AuthorizationId *uuid.UUID `json:"authorizationId,omitempty"`
32+
// The token name
33+
DisplayName *string `json:"displayName,omitempty"`
34+
// The token scopes for accessing Azure DevOps resources
35+
Scope *string `json:"scope,omitempty"`
36+
// The organizations for which the token is valid; null if the token applies to all of the user's accessible organizations
37+
TargetAccounts *[]uuid.UUID `json:"targetAccounts,omitempty"`
38+
// The unique token string generated at creation
39+
Token *string `json:"token,omitempty"`
40+
// The token creation date
41+
ValidFrom *azuredevops.Time `json:"validFrom,omitempty"`
42+
// The token expiration date
43+
ValidTo *azuredevops.Time `json:"validTo,omitempty"`
44+
}
45+
46+
// Enumeration of possible errors returned when creating a personal access token (PAT)
47+
type PatTokenError string
48+
49+
type buildPatTokenErrorType struct {
50+
None PatTokenError
51+
DisplayNameRequired PatTokenError
52+
InvalideDisplayName PatTokenError
53+
InvalidValidTo PatTokenError
54+
InvalidScope PatTokenError
55+
UserIdRequired PatTokenError
56+
InvalidUserId PatTokenError
57+
InvalidUserType PatTokenError
58+
AccessDenied PatTokenError
59+
FailedToIssueAccessToken PatTokenError
60+
InvalidClient PatTokenError
61+
InvalidClientType PatTokenError
62+
InvalidClientId PatTokenError
63+
InvalideTargetAccounts PatTokenError
64+
HostAuthorizationNotFound PatTokenError
65+
AuthorizationNotFound PatTokenError
66+
FailedToUpdateAccessToken PatTokenError
67+
SourceNotSupported PatTokenError
68+
InvalidSourceIP PatTokenError
69+
InvalideSource PatTokenError
70+
DuplicateHash PatTokenError
71+
SshPolicyDisabled PatTokenError
72+
InvalidToken PatTokenError
73+
TokenNotFound PatTokenError
74+
InvalidAuthorizationId PatTokenError
75+
FailedToReadTenantPolicy PatTokenError
76+
GlobalPatPolicyViolation PatTokenError
77+
FullScopePatPolicyViolation PatTokenError
78+
PatLifespanPolicyViolation PatTokenError
79+
InvalidTokenType PatTokenError
80+
InvalidAudience PatTokenError
81+
InvalidSubject PatTokenError
82+
DeploymentHostNotSupported PatTokenError
83+
}
84+
85+
var PatTokeErrorValues = buildPatTokenErrorType{
86+
None: "none",
87+
DisplayNameRequired: "displayNameRequired",
88+
InvalideDisplayName: "invalidDisplayName",
89+
InvalidValidTo: "invalidValidTo",
90+
InvalidScope: "invalidScope",
91+
UserIdRequired: "userIdRequired",
92+
InvalidUserId: "invalidUserId",
93+
InvalidUserType: "invalidUserType",
94+
AccessDenied: "accessDenied",
95+
FailedToIssueAccessToken: "failedToIssueAccessToken",
96+
InvalidClient: "invalidClient",
97+
InvalidClientType: "invalidClientType",
98+
InvalidClientId: "invalidClientId",
99+
InvalideTargetAccounts: "invalidTargetAccounts",
100+
HostAuthorizationNotFound: "hostAuthorizationNotFound",
101+
AuthorizationNotFound: "authorizationNotFound",
102+
FailedToUpdateAccessToken: "failedToUpdateAccessToken",
103+
SourceNotSupported: "sourceNotSupported",
104+
InvalidSourceIP: "invalidSourceIP",
105+
InvalideSource: "invalidSource",
106+
DuplicateHash: "duplicateHash",
107+
SshPolicyDisabled: "sshPolicyDisabled",
108+
InvalidToken: "invalidToken",
109+
TokenNotFound: "tokenNotFound",
110+
InvalidAuthorizationId: "invalidAuthorizationId",
111+
FailedToReadTenantPolicy: "failedToReadTenantPolicy",
112+
GlobalPatPolicyViolation: "globalPatPolicyViolation",
113+
FullScopePatPolicyViolation: "fullScopePatPolicyViolation",
114+
PatLifespanPolicyViolation: "patLifespanPolicyViolation",
115+
InvalidTokenType: "invalidTokenType",
116+
InvalidAudience: "invalidAudience",
117+
InvalidSubject: "invalidSubject",
118+
DeploymentHostNotSupported: "deploymentHostNotSupported",
119+
}
120+
121+
// PatTokenUpdateRequest encapsulates the request parameters for updating a personal access token (PAT)
122+
type PatTokenUpdateRequest struct {
123+
// (Optional) True if this personal access token (PAT) is for all of the user's accessible organizations. False if otherwise (e.g. if the token is for a specific organization)
124+
AllOrgs *bool `json:"allOrgs,omitempty"`
125+
// The authorizationId identifying a single, unique personal access token (PAT)
126+
AuthorizationId *uuid.UUID `json:"authorizationId,omitempty"`
127+
// (Optional) The token name
128+
DisplayName *string `json:"displayName,omitempty"`
129+
// (Optional) The token scopes for accessing Azure DevOps resources
130+
Scope *string `json:"scope,omitempty"`
131+
// (Optional) The token expiration date. If the \"Enforce maximum personal access token lifespan\" policy is enabled and the provided token expiration date is past the maximum allowed lifespan, it will return back a PAT with a validTo date equal to the date when the PAT was intially created + maximum allowed lifespan.
132+
ValidTo *azuredevops.Time `json:"validTo,omitempty"`
133+
}
134+
135+
// Enumerates display filter options for Personal Access Tokens (PATs)
136+
type DisplayFilterOption string
137+
138+
type buildDisplayFilterOptionType struct {
139+
Active DisplayFilterOption
140+
Revoked DisplayFilterOption
141+
Expired DisplayFilterOption
142+
All DisplayFilterOption
143+
}
144+
145+
var DisplayFilterOptionValues = buildDisplayFilterOptionType{
146+
Active: "active",
147+
Revoked: "revoked",
148+
Expired: "expired",
149+
All: "all",
150+
}
151+
152+
// Enumerates sort by options for Personal Access Tokens (PATs)
153+
type SortByOption string
154+
155+
type buildSortByOptionType struct {
156+
DisplayName SortByOption
157+
DisplayDate SortByOption
158+
Status SortByOption
159+
}
160+
161+
var SortByOptionValues = buildSortByOptionType{
162+
DisplayName: "displayName",
163+
DisplayDate: "displayDate",
164+
Status: "status",
165+
}
166+
167+
// PagedPatResults returned by the List method; contains a list of personal access tokens (PATs) and the continuation token to get the next page of results
168+
type PagedPatResults struct {
169+
// "Used to access the next page of results in successive API calls to list personal access tokens (PATs)
170+
ContinuationToken *string `json:"continuationToken,omitempty"`
171+
// The list of personal access tokens (PATs)
172+
PatTokens *[]PatToken `json:"patTokens,omitempty"`
173+
}

0 commit comments

Comments
 (0)