From 28aa2eb80230c75c19c4a0ec20f9d87269239c83 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Fri, 16 Jun 2023 16:43:52 +0200 Subject: [PATCH] Migrate to git.sr.ht/~emersion/go-oauth2 --- go.mod | 3 ++- go.sum | 2 ++ srht.go | 27 +++++++++++++++------------ 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index 18784e2..41c0234 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module git.sr.ht/~emersion/hottub go 1.16 require ( + git.sr.ht/~emersion/go-oauth2 v0.0.0-20230606083032-17f0b14608eb git.sr.ht/~emersion/gqlclient v0.0.0-20230510134714-0c53449cf76c github.com/ProtonMail/go-crypto v0.0.0-20230528122434-6f98819771a1 // indirect github.com/bradleyfalzon/ghinstallation/v2 v2.5.0 @@ -12,7 +13,7 @@ require ( github.com/google/go-github/v53 v53.1.0 // indirect github.com/vektah/gqlparser/v2 v2.5.3 // indirect go.etcd.io/bbolt v1.3.7 - golang.org/x/oauth2 v0.9.0 + golang.org/x/oauth2 v0.9.0 // indirect google.golang.org/protobuf v1.30.0 // indirect gopkg.in/yaml.v3 v3.0.1 ) diff --git a/go.sum b/go.sum index d228244..a79067e 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ cloud.google.com/go/compute/metadata v0.2.0/go.mod h1:zFmK7XCadkQkj6TtorcaGlCW1hT1fIilQDwofLpJ20k= +git.sr.ht/~emersion/go-oauth2 v0.0.0-20230606083032-17f0b14608eb h1:8Ws9GArrB8O/UmIFSpjnUdJrFrT0200o+TmfZuu98To= +git.sr.ht/~emersion/go-oauth2 v0.0.0-20230606083032-17f0b14608eb/go.mod h1:VHj0jSCLIkrfEwmOvJ4+ykpoVbD/YLN7BM523oKKBHc= git.sr.ht/~emersion/gqlclient v0.0.0-20230510134714-0c53449cf76c h1:dglqsE2zRwABwqvu9EbYR597bQeF3whk+ppnFocLxwA= git.sr.ht/~emersion/gqlclient v0.0.0-20230510134714-0c53449cf76c/go.mod h1:RYVSvQ9lRVRfj+UUwVd4tygTYCm2/mj0zJjxVaHIjEY= github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8/go.mod h1:I0gYDMZ6Z5GRU7l58bNFSkPTFN6Yl12dsUlAZ8xy98g= diff --git a/srht.go b/srht.go index dfe5232..89c5621 100644 --- a/srht.go +++ b/srht.go @@ -6,8 +6,8 @@ import ( "log" "strings" + "git.sr.ht/~emersion/go-oauth2" "git.sr.ht/~emersion/gqlclient" - "golang.org/x/oauth2" "git.sr.ht/~emersion/hottub/buildssrht" ) @@ -18,8 +18,11 @@ type SrhtClient struct { } func createSrhtClient(endpoint string, installation *Installation) *SrhtClient { - tokenSrc := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: installation.SrhtToken}) - httpClient := oauth2.NewClient(context.Background(), tokenSrc) + client := oauth2.Client{} + httpClient := client.NewHTTPClient(&oauth2.TokenResp{ + AccessToken: installation.SrhtToken, + TokenType: "Bearer", + }) return &SrhtClient{ GQL: gqlclient.New(endpoint+"/query", httpClient), Endpoint: endpoint, @@ -27,23 +30,23 @@ func createSrhtClient(endpoint string, installation *Installation) *SrhtClient { } func exchangeSrhtOAuth2(ctx context.Context, endpoint, code, clientID, clientSecret string) (token string, err error) { - conf := &oauth2.Config{ + client := oauth2.Client{ + Server: &oauth2.ServerMetadata{ + TokenEndpoint: endpoint, + }, ClientID: clientID, ClientSecret: clientSecret, - Endpoint: oauth2.Endpoint{ - TokenURL: endpoint, - AuthStyle: oauth2.AuthStyleInHeader, - }, } - tok, err := conf.Exchange(ctx, code) + // TODO: pass context + resp, err := client.Exchange(code) if err != nil { return "", err } - if t := tok.Type(); !strings.EqualFold(t, "Bearer") { - return "", fmt.Errorf("unsupported OAuth2 token type: %v", t) + if !strings.EqualFold(resp.TokenType, "Bearer") { + return "", fmt.Errorf("unsupported OAuth2 token type: %v", resp.TokenType) } - return tok.AccessToken, nil + return resp.AccessToken, nil } func saveSrhtToken(ctx context.Context, db *DB, srhtEndpoint string, installation *Installation, token string) error { -- 2.45.2