go-twitch/api/extensions/get_extension_secrets.go

51 lines
1.4 KiB
Go
Raw Permalink Normal View History

2024-03-02 22:45:59 -05:00
package extensions
import (
"context"
"encoding/json"
"fmt"
2024-03-02 22:45:59 -05:00
"net/http"
"net/url"
"go.fifitido.net/twitch/api/endpoint"
2024-03-02 22:45:59 -05:00
)
type GetExtensionSecretsResponse struct {
// The list of shared secrets that the extension created.
Data []ExtensionSecrets `json:"data"`
}
// Gets an extensions list of shared secrets.
//
// Requires a signed JSON Web Token (JWT) created by an EBS. For signing requirements,
// see Signing the JWT: https://dev.twitch.tv/docs/extensions/building/#signing-the-jwt
// The signed JWT must include the role, user_id, and exp fields
// (see JWT Schema: https://dev.twitch.tv/docs/extensions/reference/#jwt-schema).
// The role field must be set to external.
func (e *Extensions) GetExtensionSecrets(ctx context.Context, extensionID string) (*GetExtensionSecretsResponse, error) {
v := url.Values{"extension_id": {extensionID}}
2024-03-02 22:45:59 -05:00
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.Make(e.baseUrl, "extensions/jwt/secrets", v), nil)
2024-03-02 22:45:59 -05:00
if err != nil {
return nil, err
}
res, err := e.client.Do(req)
2024-03-02 22:45:59 -05:00
if err != nil {
return nil, err
}
defer res.Body.Close()
statusOK := res.StatusCode >= 200 && res.StatusCode < 300
if !statusOK {
return nil, fmt.Errorf("failed to get extension secrets (%d)", res.StatusCode)
}
2024-03-02 22:45:59 -05:00
var data GetExtensionSecretsResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}