Skip to content

Commit fdbdee0

Browse files
implementation of the client for the POST /v2/auth/exchange endpoint to exchange an apikey with a new JWT
1 parent 21b9e19 commit fdbdee0

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

auth.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package civogo
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
)
7+
8+
// ExchangeAuthTokenRequest contains data that can be passed to ExchangeAuthToken
9+
type ExchangeAuthTokenRequest struct {
10+
Scope string `json:"scope"`
11+
}
12+
13+
// ExchangeAuthTokenResponse is the response returned by a successful ExchangeAuthToken
14+
type ExchangeAuthTokenResponse struct {
15+
// The access token issued by the authorization server
16+
AccessToken string `json:"access_token"`
17+
// The type of the token issued
18+
TokenType string `json:"token_type"`
19+
// The refresh token, which can be used to obtain new access tokens
20+
RefreshToken string `json:"refresh_token"`
21+
// The lifetime in seconds of the access token
22+
ExpiresIn int32 `json:"expires_in"`
23+
// The ID token, which is a JWT that contains user profile information
24+
IDToken string `json:"id_token"`
25+
// The id of the account associated with the provided API Key
26+
AccountID string `json:"account_id"`
27+
}
28+
29+
// ExchangeAuthToken exchanges an apikey with a new civo JWT
30+
func (c *Client) ExchangeAuthToken(er *ExchangeAuthTokenRequest) (*ExchangeAuthTokenResponse, error) {
31+
body, err := c.SendPostRequest("/v2/auth/exchange", er)
32+
if err != nil {
33+
return nil, decodeError(err)
34+
}
35+
36+
result := &ExchangeAuthTokenResponse{}
37+
if err := json.NewDecoder(bytes.NewReader(body)).Decode(result); err != nil {
38+
return nil, err
39+
}
40+
41+
return result, nil
42+
}

0 commit comments

Comments
 (0)