aboutsummaryrefslogtreecommitdiff
path: root/internal/saasproxytoken
diff options
context:
space:
mode:
authorXe Iaso <me@xeiaso.net>2024-02-12 04:32:43 -0800
committerXe Iaso <me@xeiaso.net>2024-02-12 04:32:43 -0800
commite02b9ef3ee4394db1a387b146aa5faeea95efa3a (patch)
treee5b6f2c7c21dda6a5e4ea1617e10e3437c93979c /internal/saasproxytoken
parent0eb26108f65b68aa4e99786f7f7a0b28cd356f72 (diff)
downloadxesite-e02b9ef3ee4394db1a387b146aa5faeea95efa3a.tar.xz
xesite-e02b9ef3ee4394db1a387b146aa5faeea95efa3a.zip
cmd/patreon-saasproxy: use twirp/protobuf instead of yolo json
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'internal/saasproxytoken')
-rw-r--r--internal/saasproxytoken/tokensource.go24
1 files changed, 13 insertions, 11 deletions
diff --git a/internal/saasproxytoken/tokensource.go b/internal/saasproxytoken/tokensource.go
index 4c3935e..a319d4b 100644
--- a/internal/saasproxytoken/tokensource.go
+++ b/internal/saasproxytoken/tokensource.go
@@ -1,13 +1,14 @@
package saasproxytoken
import (
- "encoding/json"
+ "context"
"net/http"
"sync"
"time"
"golang.org/x/oauth2"
- "within.website/x/web"
+ "google.golang.org/protobuf/types/known/emptypb"
+ "xeiaso.net/v4/internal/adminpb"
)
type remoteTokenSource struct {
@@ -15,23 +16,23 @@ type remoteTokenSource struct {
lock sync.Mutex
remoteURL string
httpClient *http.Client
+ ptc adminpb.Patreon
}
func (r *remoteTokenSource) fetchToken() (*oauth2.Token, error) {
- resp, err := r.httpClient.Get(r.remoteURL)
+ ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
+ defer cancel()
+
+ resp, err := r.ptc.GetToken(ctx, &emptypb.Empty{})
if err != nil {
return nil, err
}
- defer resp.Body.Close()
-
- if resp.StatusCode != http.StatusOK {
- return nil, web.NewError(http.StatusOK, resp)
- }
var tok oauth2.Token
- if err := json.NewDecoder(resp.Body).Decode(&tok); err != nil {
- return nil, err
- }
+ tok.AccessToken = resp.AccessToken
+ tok.TokenType = resp.TokenType
+ tok.RefreshToken = resp.RefreshToken
+ tok.Expiry = resp.Expiry.AsTime()
return &tok, nil
}
@@ -64,5 +65,6 @@ func RemoteTokenSource(remoteURL string, httpClient *http.Client) oauth2.TokenSo
return &remoteTokenSource{
remoteURL: remoteURL,
httpClient: httpClient,
+ ptc: adminpb.NewPatreonProtobufClient(remoteURL, httpClient),
}
}