Skip to content

Commit 2fc4ec2

Browse files
authored
Merge pull request #38 from arran4/codex/recognize-expired-token-as-sign-out-error
Handle GitLab token expiration
2 parents d282bbe + 6b6c1b8 commit 2fc4ec2

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

cmd/gobookmarks/main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,17 @@ func runHandlerChain(chain ...any) func(http.ResponseWriter, *http.Request) {
353353
if errors.Is(err, ErrHandled) {
354354
return
355355
}
356+
if errors.Is(err, ErrSignedOut) {
357+
if logoutErr := UserLogoutAction(w, r); logoutErr != nil {
358+
log.Printf("logout error: %v", logoutErr)
359+
}
360+
type Data struct{ *CoreData }
361+
if err := GetCompiledTemplates(NewFuncs(r)).ExecuteTemplate(w, "logoutPage.gohtml", Data{r.Context().Value(ContextValues("coreData")).(*CoreData)}); err != nil {
362+
log.Printf("Logout Template Error: %s", err)
363+
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
364+
}
365+
return
366+
}
356367
type ErrorData struct {
357368
*CoreData
358369
Error string

errors.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ var ErrRepoNotFound = errors.New("repository not found")
88
// ErrHandled is returned by handlers when they have already written
99
// a response and no further handlers should run.
1010
var ErrHandled = errors.New("handled")
11+
12+
// ErrSignedOut indicates that the OAuth token is no longer valid and
13+
// the user must authenticate again.
14+
var ErrSignedOut = errors.New("signed out")

provider_gitlab.go

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ import (
2121
// defined in settings.go.
2222
type GitLabProvider struct{}
2323

24+
func gitlabUnauthorized(err error) bool {
25+
var respErr *gitlab.ErrorResponse
26+
return errors.As(err, &respErr) && respErr.Response != nil && respErr.Response.StatusCode == http.StatusUnauthorized
27+
}
28+
2429
func init() { RegisterProvider(GitLabProvider{}) }
2530

2631
func (GitLabProvider) Name() string { return "gitlab" }
@@ -74,6 +79,9 @@ func (GitLabProvider) GetTags(ctx context.Context, user string, token *oauth2.To
7479
}
7580
tags, _, err := c.Tags.ListTags(user+"/"+RepoName, &gitlab.ListTagsOptions{})
7681
if err != nil {
82+
if gitlabUnauthorized(err) {
83+
return nil, ErrSignedOut
84+
}
7785
log.Printf("gitlab GetTags: %v", err)
7886
return nil, fmt.Errorf("ListTags: %w", err)
7987
}
@@ -92,6 +100,9 @@ func (GitLabProvider) GetBranches(ctx context.Context, user string, token *oauth
92100
}
93101
bs, _, err := c.Branches.ListBranches(user+"/"+RepoName, &gitlab.ListBranchesOptions{})
94102
if err != nil {
103+
if gitlabUnauthorized(err) {
104+
return nil, ErrSignedOut
105+
}
95106
log.Printf("gitlab GetBranches: %v", err)
96107
return nil, fmt.Errorf("ListBranches: %w", err)
97108
}
@@ -110,6 +121,9 @@ func (GitLabProvider) GetCommits(ctx context.Context, user string, token *oauth2
110121
}
111122
cs, _, err := c.Commits.ListCommits(user+"/"+RepoName, &gitlab.ListCommitsOptions{})
112123
if err != nil {
124+
if gitlabUnauthorized(err) {
125+
return nil, ErrSignedOut
126+
}
113127
log.Printf("gitlab GetCommits: %v", err)
114128
return nil, fmt.Errorf("ListCommits: %w", err)
115129
}
@@ -144,9 +158,15 @@ func (GitLabProvider) GetBookmarks(ctx context.Context, user, ref string, token
144158
if respErr.Response != nil && respErr.Response.StatusCode == http.StatusNotFound {
145159
return "", "", nil
146160
}
161+
if gitlabUnauthorized(err) {
162+
return "", "", ErrSignedOut
163+
}
147164
log.Printf("gitlab GetBookmarks get file: %v", err)
148165
return "", "", nil
149166
}
167+
if gitlabUnauthorized(err) {
168+
return "", "", ErrSignedOut
169+
}
150170
log.Printf("gitlab GetBookmarks: %v", err)
151171
return "", "", err
152172
}
@@ -161,8 +181,16 @@ func (GitLabProvider) GetBookmarks(ctx context.Context, user, ref string, token
161181
func (GitLabProvider) getDefaultBranch(ctx context.Context, user string, client *gitlab.Client, branch string) (string, error) {
162182
p, _, err := client.Projects.GetProject(user+"/"+RepoName, nil)
163183
if err != nil {
164-
if respErr, ok := err.(*gitlab.ErrorResponse); ok && respErr.Response != nil && respErr.Response.StatusCode == http.StatusNotFound {
165-
return "", ErrRepoNotFound
184+
if respErr, ok := err.(*gitlab.ErrorResponse); ok {
185+
if respErr.Response != nil && respErr.Response.StatusCode == http.StatusNotFound {
186+
return "", ErrRepoNotFound
187+
}
188+
if gitlabUnauthorized(err) {
189+
return "", ErrSignedOut
190+
}
191+
}
192+
if gitlabUnauthorized(err) {
193+
return "", ErrSignedOut
166194
}
167195
log.Printf("gitlab getDefaultBranch: %v", err)
168196
return "", err
@@ -202,9 +230,15 @@ func (GitLabProvider) UpdateBookmarks(ctx context.Context, user string, token *o
202230
if respErr.Response != nil && respErr.Response.StatusCode == http.StatusNotFound {
203231
return ErrRepoNotFound
204232
}
233+
if gitlabUnauthorized(err) {
234+
return ErrSignedOut
235+
}
205236
log.Printf("gitlab UpdateBookmarks update file: %v", err)
206237
return err
207238
}
239+
if gitlabUnauthorized(err) {
240+
return ErrSignedOut
241+
}
208242
if err.Error() == "404 Not Found" {
209243
return ErrRepoNotFound
210244
}
@@ -240,9 +274,15 @@ func (GitLabProvider) CreateBookmarks(ctx context.Context, user string, token *o
240274
if respErr.Response != nil && respErr.Response.StatusCode == http.StatusNotFound {
241275
return ErrRepoNotFound
242276
}
277+
if gitlabUnauthorized(err) {
278+
return ErrSignedOut
279+
}
243280
log.Printf("gitlab CreateBookmarks create file: %v", err)
244281
return err
245282
}
283+
if gitlabUnauthorized(err) {
284+
return ErrSignedOut
285+
}
246286
log.Printf("gitlab CreateBookmarks: %v", err)
247287
return err
248288
}
@@ -261,5 +301,8 @@ func (p GitLabProvider) CreateRepo(ctx context.Context, user string, token *oaut
261301
Visibility: gitlab.Ptr(gitlab.PrivateVisibility),
262302
InitializeWithReadme: gitlab.Ptr(true),
263303
})
304+
if err != nil && gitlabUnauthorized(err) {
305+
return ErrSignedOut
306+
}
264307
return err
265308
}

0 commit comments

Comments
 (0)