-
Notifications
You must be signed in to change notification settings - Fork 28
/
organization_application_user.go
235 lines (198 loc) · 7.63 KB
/
organization_application_user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Package aiven provides a client for using the Aiven API.
package aiven
import (
"context"
"time"
)
type (
// OrganizationApplicationUserHandler is the client which interacts with the Organization Application Users API on
// Aiven.
OrganizationApplicationUserHandler struct {
// client is the API client to use.
client *Client
}
// ApplicationUserInfo is a struct that represents a user in an application.
ApplicationUserInfo struct {
// UserID is the unique identifier of the user.
UserID string `json:"user_id"`
// UserEmail is the email of the user.
UserEmail string `json:"user_email"`
// Name is the name of the user.
Name string `json:"name"`
}
// ApplicationUserTokenInfo is a struct that represents a user's token in an application.
ApplicationUserTokenInfo struct {
// CurrentlyActive is true if API request was made with this access token.
CurrentlyActive bool `json:"currently_active"`
// CreateTime is the time when the token was created.
CreateTime *time.Time `json:"create_time"`
// CreatedManually is true for tokens explicitly created via the access_tokens API, false for tokens created
// via login.
CreatedManually bool `json:"created_manually"`
// Description is the description of the token.
Description *string `json:"description,omitempty"`
// ExpiryTime is the timestamp when the access token will expire unless extended, if ever.
ExpiryTime *time.Time `json:"expiry_time,omitempty"`
// ExtendWhenUsed is true to extend token expiration time when token is used. Only applicable if
// max_age_seconds is specified.
ExtendWhenUsed *bool `json:"extend_when_used,omitempty"`
// LastIP is the IP address of the last request made with this token.
LastIP string `json:"last_ip"`
// LastUsedTime is the timestamp when the access token was last used, if ever.
LastUsedTime *time.Time `json:"last_used_time,omitempty"`
// LastUserAgent is the user agent of the last request made with this token.
LastUserAgent *string `json:"last_user_agent,omitempty"`
// LastUserAgentHumanReadable is the user agent of the last request made with this token in
// human-readable format.
LastUserAgentHumanReadable *string `json:"last_user_agent_human_readable,omitempty"`
// MaxAgeSeconds is the time the token remains valid since creation (or since last use if extend_when_used
// is true).
MaxAgeSeconds int `json:"max_age_seconds"`
// TokenPrefix is the prefix of the token.
TokenPrefix string `json:"token_prefix"`
// Scopes is the scopes this token is restricted to if specified.
Scopes *[]string `json:"scopes,omitempty"`
}
// ApplicationUserTokenList is a struct that represents a list of user's tokens in an application.
ApplicationUserTokenList struct {
// Tokens is a list of user's tokens in an application.
Tokens []ApplicationUserTokenInfo `json:"tokens"`
}
// ApplicationUserListResponse is a response from Aiven for a list of application users.
ApplicationUserListResponse struct {
APIResponse
// Users is a list of application users.
Users []ApplicationUserInfo `json:"application_users"`
}
// ApplicationUserCreateRequest is a request to create a user in an application.
ApplicationUserCreateRequest struct {
// Name is the name of the user.
Name string `json:"name"`
}
// ApplicationUserCreateResponse is a response from Aiven for a user creation request.
ApplicationUserCreateResponse struct {
APIResponse
ApplicationUserInfo
}
// ApplicationUserTokenListResponse is a response from Aiven for a list of user's tokens in an application.
ApplicationUserTokenListResponse struct {
APIResponse
ApplicationUserTokenList
}
// ApplicationUserTokenCreateRequest is a request to create a token for a user in an application.
ApplicationUserTokenCreateRequest struct {
// Description is the description of the token.
Description *string `json:"description,omitempty"`
// MaxAgeSeconds is the time the token remains valid since creation (or since last use if extend_when_used
// is true).
MaxAgeSeconds *int `json:"max_age_seconds,omitempty"`
// ExtendWhenUsed is true to extend token expiration time when token is used. Only applicable if
// max_age_seconds is specified.
ExtendWhenUsed *bool `json:"extend_when_used,omitempty"`
// Scopes is the scopes this token is restricted to if specified.
Scopes *[]string `json:"scopes,omitempty"`
}
// ApplicationUserTokenCreateResponse is a response from Aiven for a token creation request.
ApplicationUserTokenCreateResponse struct {
APIResponse
// FullToken is the full token.
FullToken string `json:"full_token"`
// TokenPrefix is the prefix of the token.
TokenPrefix string `json:"token_prefix"`
}
)
// List returns a list of all application users.
//
// GET /organization/{organization_id}/application-users
func (h *OrganizationApplicationUserHandler) List(
ctx context.Context,
orgID string,
) (*ApplicationUserListResponse, error) {
path := buildPath("organization", orgID, "application-users")
bts, err := h.client.doGetRequest(ctx, path, nil)
if err != nil {
return nil, err
}
var r ApplicationUserListResponse
return &r, checkAPIResponse(bts, &r)
}
// Create creates a new application user.
//
// POST /organization/{organization_id}/application-users
func (h *OrganizationApplicationUserHandler) Create(
ctx context.Context,
orgID string,
req ApplicationUserCreateRequest,
) (*ApplicationUserCreateResponse, error) {
path := buildPath("organization", orgID, "application-users")
bts, err := h.client.doPostRequest(ctx, path, req)
if err != nil {
return nil, err
}
var r ApplicationUserCreateResponse
return &r, checkAPIResponse(bts, &r)
}
// Delete deletes an application user.
//
// DELETE /organization/{organization_id}/application-users/{user_id}
func (h *OrganizationApplicationUserHandler) Delete(
ctx context.Context,
orgID string,
userID string,
) error {
path := buildPath("organization", orgID, "application-users", userID)
bts, err := h.client.doDeleteRequest(ctx, path, nil)
if err != nil {
return err
}
return checkAPIResponse(bts, nil)
}
// ListTokens returns a list of all application user tokens.
//
// GET /organization/{organization_id}/application-users/{user_id}/access-tokens
func (h *OrganizationApplicationUserHandler) ListTokens(
ctx context.Context,
orgID string,
userID string,
) (*ApplicationUserTokenListResponse, error) {
path := buildPath("organization", orgID, "application-users", userID, "access-tokens")
bts, err := h.client.doGetRequest(ctx, path, nil)
if err != nil {
return nil, err
}
var r ApplicationUserTokenListResponse
return &r, checkAPIResponse(bts, &r)
}
// CreateToken creates a new application user token.
//
// POST /organization/{organization_id}/application-users/{user_id}/access-tokens
func (h *OrganizationApplicationUserHandler) CreateToken(
ctx context.Context,
orgID string,
userID string,
req ApplicationUserTokenCreateRequest,
) (*ApplicationUserTokenCreateResponse, error) {
path := buildPath("organization", orgID, "application-users", userID, "access-tokens")
bts, err := h.client.doPostRequest(ctx, path, req)
if err != nil {
return nil, err
}
var r ApplicationUserTokenCreateResponse
return &r, checkAPIResponse(bts, &r)
}
// DeleteToken deletes an application user token.
//
// DELETE /organization/{organization_id}/application-users/{user_id}/access-tokens/{token_prefix}
func (h *OrganizationApplicationUserHandler) DeleteToken(
ctx context.Context,
orgID string,
userID string,
tokenPrefix string,
) error {
path := buildPath("organization", orgID, "application-users", userID, "access-tokens", tokenPrefix)
bts, err := h.client.doDeleteRequest(ctx, path, nil)
if err != nil {
return err
}
return checkAPIResponse(bts, nil)
}