Use ErrNotAuthenticated

This commit is contained in:
Luke Granger-Brown 2020-06-19 00:35:56 +01:00 committed by Michael Muré
parent e5a316e40c
commit 5f72b04ef8
No known key found for this signature in database
GPG Key ID: A4457C029293126F
5 changed files with 15 additions and 25 deletions

View File

@ -211,13 +211,13 @@ func newGitUploadFileHandler(repo repository.Repo) http.Handler {
} }
func (gufh *gitUploadFileHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { func (gufh *gitUploadFileHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
id, err := graphqlidentity.ForContextUncached(r.Context(), gufh.repo) _, err := graphqlidentity.ForContextUncached(r.Context(), gufh.repo)
if err != nil { if err == graphqlidentity.ErrNotAuthenticated {
http.Error(rw, fmt.Sprintf("loading identity: %v", err), http.StatusInternalServerError)
return
} else if id == nil {
http.Error(rw, fmt.Sprintf("read-only mode or not logged in"), http.StatusForbidden) http.Error(rw, fmt.Sprintf("read-only mode or not logged in"), http.StatusForbidden)
return return
} else if err != nil {
http.Error(rw, fmt.Sprintf("loading identity: %v", err), http.StatusInternalServerError)
return
} }
// 100MB (github limit) // 100MB (github limit)

View File

@ -1,4 +1,4 @@
package resolvers package graphqlidentity
import "errors" import "errors"

View File

@ -18,22 +18,24 @@ func AttachToContext(ctx context.Context, u *identity.Identity) context.Context
return context.WithValue(ctx, identityCtxKey, u.Id()) return context.WithValue(ctx, identityCtxKey, u.Id())
} }
// ForContext retrieves an IdentityCache from the context, or nil if no identity is present. // ForContext retrieves an IdentityCache from the context.
// If there is no identity in the context, ErrNotAuthenticated is returned.
// If an error occurs while resolving the identity (e.g. I/O error), then it will be returned. // If an error occurs while resolving the identity (e.g. I/O error), then it will be returned.
func ForContext(ctx context.Context, r *cache.RepoCache) (*cache.IdentityCache, error) { func ForContext(ctx context.Context, r *cache.RepoCache) (*cache.IdentityCache, error) {
id, ok := ctx.Value(identityCtxKey).(entity.Id) id, ok := ctx.Value(identityCtxKey).(entity.Id)
if !ok { if !ok {
return nil, nil return nil, ErrNotAuthenticated
} }
return r.ResolveIdentity(id) return r.ResolveIdentity(id)
} }
// ForContextUncached retrieves an Identity from the context, or nil if no identity is present. // ForContextUncached retrieves an Identity from the context.
// If there is no identity in the context, ErrNotAuthenticated is returned.
// If an error occurs while resolving the identity (e.g. I/O error), then it will be returned. // If an error occurs while resolving the identity (e.g. I/O error), then it will be returned.
func ForContextUncached(ctx context.Context, repo repository.Repo) (*identity.Identity, error) { func ForContextUncached(ctx context.Context, repo repository.Repo) (*identity.Identity, error) {
id, ok := ctx.Value(identityCtxKey).(entity.Id) id, ok := ctx.Value(identityCtxKey).(entity.Id)
if !ok { if !ok {
return nil, nil return nil, ErrNotAuthenticated
} }
return identity.ReadLocal(repo, id) return identity.ReadLocal(repo, id)
} }

View File

@ -47,8 +47,6 @@ func (r mutationResolver) NewBug(ctx context.Context, input models.NewBugInput)
id, err := graphqlidentity.ForContext(ctx, repo) id, err := graphqlidentity.ForContext(ctx, repo)
if err != nil { if err != nil {
return nil, err return nil, err
} else if id == nil {
return nil, ErrNotAuthenticated
} }
b, op, err := repo.NewBugRaw(id, time.Now().Unix(), input.Title, input.Message, input.Files, nil) b, op, err := repo.NewBugRaw(id, time.Now().Unix(), input.Title, input.Message, input.Files, nil)
@ -72,8 +70,6 @@ func (r mutationResolver) AddComment(ctx context.Context, input models.AddCommen
id, err := graphqlidentity.ForContext(ctx, repo) id, err := graphqlidentity.ForContext(ctx, repo)
if err != nil { if err != nil {
return nil, err return nil, err
} else if id == nil {
return nil, ErrNotAuthenticated
} }
op, err := b.AddCommentRaw(id, time.Now().Unix(), input.Message, input.Files, nil) op, err := b.AddCommentRaw(id, time.Now().Unix(), input.Message, input.Files, nil)
@ -102,8 +98,6 @@ func (r mutationResolver) ChangeLabels(ctx context.Context, input *models.Change
id, err := graphqlidentity.ForContext(ctx, repo) id, err := graphqlidentity.ForContext(ctx, repo)
if err != nil { if err != nil {
return nil, err return nil, err
} else if id == nil {
return nil, ErrNotAuthenticated
} }
results, op, err := b.ChangeLabelsRaw(id, time.Now().Unix(), input.Added, input.Removed, nil) results, op, err := b.ChangeLabelsRaw(id, time.Now().Unix(), input.Added, input.Removed, nil)
@ -138,8 +132,6 @@ func (r mutationResolver) OpenBug(ctx context.Context, input models.OpenBugInput
id, err := graphqlidentity.ForContext(ctx, repo) id, err := graphqlidentity.ForContext(ctx, repo)
if err != nil { if err != nil {
return nil, err return nil, err
} else if id == nil {
return nil, ErrNotAuthenticated
} }
op, err := b.OpenRaw(id, time.Now().Unix(), nil) op, err := b.OpenRaw(id, time.Now().Unix(), nil)
@ -168,8 +160,6 @@ func (r mutationResolver) CloseBug(ctx context.Context, input models.CloseBugInp
id, err := graphqlidentity.ForContext(ctx, repo) id, err := graphqlidentity.ForContext(ctx, repo)
if err != nil { if err != nil {
return nil, err return nil, err
} else if id == nil {
return nil, ErrNotAuthenticated
} }
op, err := b.CloseRaw(id, time.Now().Unix(), nil) op, err := b.CloseRaw(id, time.Now().Unix(), nil)
@ -198,8 +188,6 @@ func (r mutationResolver) SetTitle(ctx context.Context, input models.SetTitleInp
id, err := graphqlidentity.ForContext(ctx, repo) id, err := graphqlidentity.ForContext(ctx, repo)
if err != nil { if err != nil {
return nil, err return nil, err
} else if id == nil {
return nil, ErrNotAuthenticated
} }
op, err := b.SetTitleRaw(id, time.Now().Unix(), input.Title, nil) op, err := b.SetTitleRaw(id, time.Now().Unix(), input.Title, nil)

View File

@ -152,10 +152,10 @@ func (repoResolver) Identity(_ context.Context, obj *models.Repository, prefix s
func (repoResolver) UserIdentity(ctx context.Context, obj *models.Repository) (models.IdentityWrapper, error) { func (repoResolver) UserIdentity(ctx context.Context, obj *models.Repository) (models.IdentityWrapper, error) {
id, err := graphqlidentity.ForContext(ctx, obj.Repo) id, err := graphqlidentity.ForContext(ctx, obj.Repo)
if err != nil { if err == graphqlidentity.ErrNotAuthenticated {
return nil, err
} else if id == nil {
return nil, nil return nil, nil
} else if err != nil {
return nil, err
} }
return models.NewLoadedIdentity(id.Identity), nil return models.NewLoadedIdentity(id.Identity), nil
} }