1package linodego 2 3import ( 4 "context" 5 "encoding/json" 6 "fmt" 7) 8 9// OAuthClientStatus constants start with OAuthClient and include Linode API Instance Status values 10type OAuthClientStatus string 11 12// OAuthClientStatus constants reflect the current status of an OAuth Client 13const ( 14 OAuthClientActive OAuthClientStatus = "active" 15 OAuthClientDisabled OAuthClientStatus = "disabled" 16 OAuthClientSuspended OAuthClientStatus = "suspended" 17) 18 19// OAuthClient represents a OAuthClient object 20type OAuthClient struct { 21 // The unique ID of this OAuth Client. 22 ID string `json:"id"` 23 24 // The location a successful log in from https://login.linode.com should be redirected to for this client. The receiver of this redirect should be ready to accept an OAuth exchange code and finish the OAuth exchange. 25 RedirectURI string `json:"redirect_uri"` 26 27 // The name of this application. This will be presented to users when they are asked to grant it access to their Account. 28 Label string `json:"label"` 29 30 // Current status of the OAuth Client, Enum: "active" "disabled" "suspended" 31 Status OAuthClientStatus `json:"status"` 32 33 // The OAuth Client secret, used in the OAuth exchange. This is returned as <REDACTED> except when an OAuth Client is created or its secret is reset. This is a secret, and should not be shared or disclosed publicly. 34 Secret string `json:"secret"` 35 36 // If this OAuth Client is public or private. 37 Public bool `json:"public"` 38 39 // The URL where this client's thumbnail may be viewed, or nil if this client does not have a thumbnail set. 40 ThumbnailURL *string `json:"thumbnail_url"` 41} 42 43// OAuthClientCreateOptions fields are those accepted by CreateOAuthClient 44type OAuthClientCreateOptions struct { 45 // The location a successful log in from https://login.linode.com should be redirected to for this client. The receiver of this redirect should be ready to accept an OAuth exchange code and finish the OAuth exchange. 46 RedirectURI string `json:"redirect_uri"` 47 48 // The name of this application. This will be presented to users when they are asked to grant it access to their Account. 49 Label string `json:"label"` 50 51 // If this OAuth Client is public or private. 52 Public bool `json:"public"` 53} 54 55// OAuthClientUpdateOptions fields are those accepted by UpdateOAuthClient 56type OAuthClientUpdateOptions struct { 57 // The location a successful log in from https://login.linode.com should be redirected to for this client. The receiver of this redirect should be ready to accept an OAuth exchange code and finish the OAuth exchange. 58 RedirectURI string `json:"redirect_uri"` 59 60 // The name of this application. This will be presented to users when they are asked to grant it access to their Account. 61 Label string `json:"label"` 62 63 // If this OAuth Client is public or private. 64 Public bool `json:"public"` 65} 66 67// GetCreateOptions converts a OAuthClient to OAuthClientCreateOptions for use in CreateOAuthClient 68func (i OAuthClient) GetCreateOptions() (o OAuthClientCreateOptions) { 69 o.RedirectURI = i.RedirectURI 70 o.Label = i.Label 71 o.Public = i.Public 72 73 return 74} 75 76// GetUpdateOptions converts a OAuthClient to OAuthClientUpdateOptions for use in UpdateOAuthClient 77func (i OAuthClient) GetUpdateOptions() (o OAuthClientUpdateOptions) { 78 o.RedirectURI = i.RedirectURI 79 o.Label = i.Label 80 o.Public = i.Public 81 82 return 83} 84 85// OAuthClientsPagedResponse represents a paginated OAuthClient API response 86type OAuthClientsPagedResponse struct { 87 *PageOptions 88 Data []OAuthClient `json:"data"` 89} 90 91// endpoint gets the endpoint URL for OAuthClient 92func (OAuthClientsPagedResponse) endpoint(c *Client) string { 93 endpoint, err := c.OAuthClients.Endpoint() 94 if err != nil { 95 panic(err) 96 } 97 98 return endpoint 99} 100 101// appendData appends OAuthClients when processing paginated OAuthClient responses 102func (resp *OAuthClientsPagedResponse) appendData(r *OAuthClientsPagedResponse) { 103 resp.Data = append(resp.Data, r.Data...) 104} 105 106// ListOAuthClients lists OAuthClients 107func (c *Client) ListOAuthClients(ctx context.Context, opts *ListOptions) ([]OAuthClient, error) { 108 response := OAuthClientsPagedResponse{} 109 err := c.listHelper(ctx, &response, opts) 110 if err != nil { 111 return nil, err 112 } 113 114 return response.Data, nil 115} 116 117// GetOAuthClient gets the OAuthClient with the provided ID 118func (c *Client) GetOAuthClient(ctx context.Context, id string) (*OAuthClient, error) { 119 e, err := c.OAuthClients.Endpoint() 120 if err != nil { 121 return nil, err 122 } 123 124 e = fmt.Sprintf("%s/%s", e, id) 125 r, err := coupleAPIErrors(c.R(ctx).SetResult(&OAuthClient{}).Get(e)) 126 if err != nil { 127 return nil, err 128 } 129 130 return r.Result().(*OAuthClient), nil 131} 132 133// CreateOAuthClient creates an OAuthClient 134func (c *Client) CreateOAuthClient(ctx context.Context, createOpts OAuthClientCreateOptions) (*OAuthClient, error) { 135 var body string 136 137 e, err := c.OAuthClients.Endpoint() 138 if err != nil { 139 return nil, err 140 } 141 142 req := c.R(ctx).SetResult(&OAuthClient{}) 143 144 if bodyData, err := json.Marshal(createOpts); err == nil { 145 body = string(bodyData) 146 } else { 147 return nil, NewError(err) 148 } 149 150 r, err := coupleAPIErrors(req. 151 SetBody(body). 152 Post(e)) 153 if err != nil { 154 return nil, err 155 } 156 157 return r.Result().(*OAuthClient), nil 158} 159 160// UpdateOAuthClient updates the OAuthClient with the specified id 161func (c *Client) UpdateOAuthClient(ctx context.Context, id string, updateOpts OAuthClientUpdateOptions) (*OAuthClient, error) { 162 var body string 163 164 e, err := c.OAuthClients.Endpoint() 165 if err != nil { 166 return nil, err 167 } 168 169 e = fmt.Sprintf("%s/%s", e, id) 170 171 req := c.R(ctx).SetResult(&OAuthClient{}) 172 173 if bodyData, err := json.Marshal(updateOpts); err == nil { 174 body = string(bodyData) 175 } else { 176 return nil, NewError(err) 177 } 178 179 r, err := coupleAPIErrors(req. 180 SetBody(body). 181 Put(e)) 182 if err != nil { 183 return nil, err 184 } 185 186 return r.Result().(*OAuthClient), nil 187} 188 189// DeleteOAuthClient deletes the OAuthClient with the specified id 190func (c *Client) DeleteOAuthClient(ctx context.Context, id string) error { 191 e, err := c.OAuthClients.Endpoint() 192 if err != nil { 193 return err 194 } 195 196 e = fmt.Sprintf("%s/%s", e, id) 197 198 _, err = coupleAPIErrors(c.R(ctx).Delete(e)) 199 200 return err 201} 202