From 5b0217e2f0528ed22760dadb45a149bcb0d045e8 Mon Sep 17 00:00:00 2001 From: w-haibara Date: Fri, 6 Aug 2021 00:18:04 +0900 Subject: [PATCH 01/11] add TokenExpiration field --- github/github.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/github/github.go b/github/github.go index 2f7653654ff..b28244ec12d 100644 --- a/github/github.go +++ b/github/github.go @@ -464,6 +464,9 @@ type Response struct { // Explicitly specify the Rate type so Rate's String() receiver doesn't // propagate to Response. Rate Rate + + // token's expiration date + TokenExpiration Timestamp } // newResponse creates a new Response for the provided http.Response. @@ -472,6 +475,7 @@ func newResponse(r *http.Response) *Response { response := &Response{Response: r} response.populatePageValues() response.Rate = parseRate(r) + response.TokenExpiration = parseTokenExpiration(r) return response } @@ -551,6 +555,16 @@ func parseRate(r *http.Response) Rate { return rate } +// parseTokenExpiration parses the TokenExpiration related headers. +func parseTokenExpiration(r *http.Response) Timestamp { + var exp Timestamp + if v := r.Header.Get("GitHub-Authentication-Token-Expiration"); v != "" { + t, _ := time.Parse("20006-15-04", v) + exp = Timestamp{t} + } + return exp +} + type requestContext uint8 const ( From 6a34dc6c1ae59d13b163543402b004826c39b7d0 Mon Sep 17 00:00:00 2001 From: w-haibara Date: Fri, 6 Aug 2021 01:05:40 +0900 Subject: [PATCH 02/11] fix time format --- github/github.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/github.go b/github/github.go index b28244ec12d..4a59b1e816d 100644 --- a/github/github.go +++ b/github/github.go @@ -559,7 +559,7 @@ func parseRate(r *http.Response) Rate { func parseTokenExpiration(r *http.Response) Timestamp { var exp Timestamp if v := r.Header.Get("GitHub-Authentication-Token-Expiration"); v != "" { - t, _ := time.Parse("20006-15-04", v) + t, _ := time.Parse("2006-01-02 03:04:05 MST", v) exp = Timestamp{t} } return exp From 8ab553bbfdcc4e5c58c5845dbd7459cc820db0c9 Mon Sep 17 00:00:00 2001 From: w-haibara Date: Fri, 6 Aug 2021 01:09:49 +0900 Subject: [PATCH 03/11] use local time --- github/github.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/github.go b/github/github.go index 4a59b1e816d..21921bc8a91 100644 --- a/github/github.go +++ b/github/github.go @@ -560,7 +560,7 @@ func parseTokenExpiration(r *http.Response) Timestamp { var exp Timestamp if v := r.Header.Get("GitHub-Authentication-Token-Expiration"); v != "" { t, _ := time.Parse("2006-01-02 03:04:05 MST", v) - exp = Timestamp{t} + exp = Timestamp{t.Local()} } return exp } From 265434bab8605a1ed41ce0d955e9fac9baffa64a Mon Sep 17 00:00:00 2001 From: w-haibara Date: Fri, 6 Aug 2021 01:32:38 +0900 Subject: [PATCH 04/11] update example for TokenExpiration --- example/tokenauth/main.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/example/tokenauth/main.go b/example/tokenauth/main.go index 7f172965f3b..c74511a8c8c 100644 --- a/example/tokenauth/main.go +++ b/example/tokenauth/main.go @@ -22,6 +22,7 @@ import ( func main() { fmt.Print("GitHub Token: ") byteToken, _ := terminal.ReadPassword(int(syscall.Stdin)) + println() token := string(byteToken) ctx := context.Background() @@ -39,7 +40,12 @@ func main() { } // Rate.Limit should most likely be 5000 when authorized. - log.Printf("Rate: %#v", resp.Rate) - + log.Printf("Rate: %#v\n", resp.Rate) + + // If a Token Expiration has been set, it will be displayed. + if !resp.TokenExpiration.IsZero() { + log.Printf("Token Expiration: %v\n", resp.TokenExpiration.String()) + } + fmt.Printf("\n%v\n", github.Stringify(user)) } From 940a3d322e02159123164f4f9e2289c74fb87c1e Mon Sep 17 00:00:00 2001 From: w-haibara Date: Fri, 6 Aug 2021 01:44:47 +0900 Subject: [PATCH 05/11] use const --- github/github.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/github/github.go b/github/github.go index 21921bc8a91..e3ff55966b4 100644 --- a/github/github.go +++ b/github/github.go @@ -37,6 +37,8 @@ const ( headerRateReset = "X-RateLimit-Reset" headerOTP = "X-GitHub-OTP" + headerTokenExpiration = "GitHub-Authentication-Token-Expiration" + mediaTypeV3 = "application/vnd.github.v3+json" defaultMediaType = "application/octet-stream" mediaTypeV3SHA = "application/vnd.github.v3.sha" @@ -558,7 +560,7 @@ func parseRate(r *http.Response) Rate { // parseTokenExpiration parses the TokenExpiration related headers. func parseTokenExpiration(r *http.Response) Timestamp { var exp Timestamp - if v := r.Header.Get("GitHub-Authentication-Token-Expiration"); v != "" { + if v := r.Header.Get(headerTokenExpiration); v != "" { t, _ := time.Parse("2006-01-02 03:04:05 MST", v) exp = Timestamp{t.Local()} } From 5c8bf76a47a6690d54f83ccd6ca35e04766ef027 Mon Sep 17 00:00:00 2001 From: w-haibara Date: Fri, 6 Aug 2021 02:28:19 +0900 Subject: [PATCH 06/11] remove redundant String() --- example/tokenauth/main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/example/tokenauth/main.go b/example/tokenauth/main.go index c74511a8c8c..67064fa3616 100644 --- a/example/tokenauth/main.go +++ b/example/tokenauth/main.go @@ -41,11 +41,11 @@ func main() { // Rate.Limit should most likely be 5000 when authorized. log.Printf("Rate: %#v\n", resp.Rate) - + // If a Token Expiration has been set, it will be displayed. if !resp.TokenExpiration.IsZero() { - log.Printf("Token Expiration: %v\n", resp.TokenExpiration.String()) + log.Printf("Token Expiration: %v\n", resp.TokenExpiration) } - + fmt.Printf("\n%v\n", github.Stringify(user)) } From 6913685cfd236023788d8a20545590b2cb29e5bc Mon Sep 17 00:00:00 2001 From: w-haibara Date: Fri, 6 Aug 2021 02:30:06 +0900 Subject: [PATCH 07/11] add err check --- github/github.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/github/github.go b/github/github.go index e3ff55966b4..fe686857ad9 100644 --- a/github/github.go +++ b/github/github.go @@ -561,8 +561,9 @@ func parseRate(r *http.Response) Rate { func parseTokenExpiration(r *http.Response) Timestamp { var exp Timestamp if v := r.Header.Get(headerTokenExpiration); v != "" { - t, _ := time.Parse("2006-01-02 03:04:05 MST", v) - exp = Timestamp{t.Local()} + if t, err := time.Parse("2006-01-02 03:04:05 MST", v); err == nil { + exp = Timestamp{t.Local()} + } } return exp } From e892ec6234399c281156d6575cdc3b4ee5657bd7 Mon Sep 17 00:00:00 2001 From: w-haibara Date: Fri, 6 Aug 2021 11:42:19 +0900 Subject: [PATCH 08/11] add test for parseTokenExpiration --- github/github_test.go | 33 ++++++++++++++++++++++++++++ main.go | 51 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 main.go diff --git a/github/github_test.go b/github/github_test.go index d2240558da5..1676e438577 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -2206,3 +2206,36 @@ func TestRateLimits_Marshal(t *testing.T) { testJSONMarshal(t, u, want) } + +func TestParseTokenExpiration(t *testing.T) { + tests := []struct { + res *http.Response + header string + want Timestamp + }{ + { + res: &http.Response{ + Request: &http.Request{}, + Header: http.Header{}, + }, + header: "", + want: Timestamp{}, + }, + { + res: &http.Response{ + Request: &http.Request{}, + Header: http.Header{}, + }, + header: "2021-09-03 02:34:04 UTC", + want: Timestamp{time.Date(2021, time.September, 3, 2, 34, 4, 0, time.UTC)}, + }, + } + + for _, tt := range tests { + tt.res.Header.Set(headerTokenExpiration, tt.header) + exp := parseTokenExpiration(tt.res) + if !exp.Equal(tt.want) { + t.Errorf("parseTokenExpiration returned %#v, want %#v", exp, tt.want) + } + } +} diff --git a/main.go b/main.go new file mode 100644 index 00000000000..67064fa3616 --- /dev/null +++ b/main.go @@ -0,0 +1,51 @@ +// Copyright 2020 The go-github AUTHORS. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// The tokenauth command demonstrates using the oauth2.StaticTokenSource. +// You can test out a GitHub Personal Access Token using this simple example. +// You can generate them here: https://github.com/settings/tokens +package main + +import ( + "context" + "fmt" + "log" + "syscall" + + "github.com/google/go-github/v37/github" + "golang.org/x/crypto/ssh/terminal" + "golang.org/x/oauth2" +) + +func main() { + fmt.Print("GitHub Token: ") + byteToken, _ := terminal.ReadPassword(int(syscall.Stdin)) + println() + token := string(byteToken) + + ctx := context.Background() + ts := oauth2.StaticTokenSource( + &oauth2.Token{AccessToken: token}, + ) + tc := oauth2.NewClient(ctx, ts) + + client := github.NewClient(tc) + + user, resp, err := client.Users.Get(ctx, "") + if err != nil { + fmt.Printf("\nerror: %v\n", err) + return + } + + // Rate.Limit should most likely be 5000 when authorized. + log.Printf("Rate: %#v\n", resp.Rate) + + // If a Token Expiration has been set, it will be displayed. + if !resp.TokenExpiration.IsZero() { + log.Printf("Token Expiration: %v\n", resp.TokenExpiration) + } + + fmt.Printf("\n%v\n", github.Stringify(user)) +} From 52fb6889e915397ef3f95cfc1e2fcadecc83fcce Mon Sep 17 00:00:00 2001 From: w-haibara Date: Fri, 6 Aug 2021 12:51:06 +0900 Subject: [PATCH 09/11] remove files added by mistake --- main.go | 51 --------------------------------------------------- 1 file changed, 51 deletions(-) delete mode 100644 main.go diff --git a/main.go b/main.go deleted file mode 100644 index 67064fa3616..00000000000 --- a/main.go +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright 2020 The go-github AUTHORS. All rights reserved. -// -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// The tokenauth command demonstrates using the oauth2.StaticTokenSource. -// You can test out a GitHub Personal Access Token using this simple example. -// You can generate them here: https://github.com/settings/tokens -package main - -import ( - "context" - "fmt" - "log" - "syscall" - - "github.com/google/go-github/v37/github" - "golang.org/x/crypto/ssh/terminal" - "golang.org/x/oauth2" -) - -func main() { - fmt.Print("GitHub Token: ") - byteToken, _ := terminal.ReadPassword(int(syscall.Stdin)) - println() - token := string(byteToken) - - ctx := context.Background() - ts := oauth2.StaticTokenSource( - &oauth2.Token{AccessToken: token}, - ) - tc := oauth2.NewClient(ctx, ts) - - client := github.NewClient(tc) - - user, resp, err := client.Users.Get(ctx, "") - if err != nil { - fmt.Printf("\nerror: %v\n", err) - return - } - - // Rate.Limit should most likely be 5000 when authorized. - log.Printf("Rate: %#v\n", resp.Rate) - - // If a Token Expiration has been set, it will be displayed. - if !resp.TokenExpiration.IsZero() { - log.Printf("Token Expiration: %v\n", resp.TokenExpiration) - } - - fmt.Printf("\n%v\n", github.Stringify(user)) -} From f2b0e4da42bac9686644c7b7d712a8ec5fb1def7 Mon Sep 17 00:00:00 2001 From: w-haibara Date: Fri, 6 Aug 2021 22:08:09 +0900 Subject: [PATCH 10/11] improved a test case --- github/github_test.go | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/github/github_test.go b/github/github_test.go index 1676e438577..52e8e666532 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -2209,31 +2209,27 @@ func TestRateLimits_Marshal(t *testing.T) { func TestParseTokenExpiration(t *testing.T) { tests := []struct { - res *http.Response header string want Timestamp }{ { - res: &http.Response{ - Request: &http.Request{}, - Header: http.Header{}, - }, header: "", want: Timestamp{}, }, { - res: &http.Response{ - Request: &http.Request{}, - Header: http.Header{}, - }, header: "2021-09-03 02:34:04 UTC", want: Timestamp{time.Date(2021, time.September, 3, 2, 34, 4, 0, time.UTC)}, }, } for _, tt := range tests { - tt.res.Header.Set(headerTokenExpiration, tt.header) - exp := parseTokenExpiration(tt.res) + res := &http.Response{ + Request: &http.Request{}, + Header: http.Header{}, + } + + res.Header.Set(headerTokenExpiration, tt.header) + exp := parseTokenExpiration(res) if !exp.Equal(tt.want) { t.Errorf("parseTokenExpiration returned %#v, want %#v", exp, tt.want) } From d6596263194f31136382de824c2c9f58e5d2f951 Mon Sep 17 00:00:00 2001 From: w-haibara Date: Fri, 6 Aug 2021 22:16:22 +0900 Subject: [PATCH 11/11] add a test case for garbage header --- github/github_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/github/github_test.go b/github/github_test.go index 52e8e666532..666bb45ca18 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -2216,6 +2216,10 @@ func TestParseTokenExpiration(t *testing.T) { header: "", want: Timestamp{}, }, + { + header: "this is a garbage", + want: Timestamp{}, + }, { header: "2021-09-03 02:34:04 UTC", want: Timestamp{time.Date(2021, time.September, 3, 2, 34, 4, 0, time.UTC)},