@@ 6,7 6,6 @@ import (
"database/sql"
"encoding/hex"
"encoding/json"
- "errors"
"fmt"
"log"
"net"
@@ 24,6 23,7 @@ import (
"git.sr.ht/~sircmpwn/core-go/config"
"git.sr.ht/~sircmpwn/core-go/crypto"
"git.sr.ht/~sircmpwn/core-go/database"
+ "git.sr.ht/~sircmpwn/core-go/errors"
)
var userCtxKey = &contextKey{"user"}
@@ 87,12 87,12 @@ func (authctx *AuthContext) Access(scope, kind string) error {
return nil
case AUTH_WEBHOOK:
if kind != RO {
- return fmt.Errorf("Access to read/write resolver denied for webhook")
+ return errors.Errorf(errors.AccessDenied, "Access to read/write resolver denied for webhook")
}
fallthrough
case AUTH_OAUTH2:
if !authctx.Grants.Has(scope, kind) {
- return fmt.Errorf("Access denied, missing %v:%v grant", scope, kind)
+ return errors.Errorf(errors.AccessDenied, "Access denied, missing %v:%v grant", scope, kind)
}
return nil
default:
@@ 156,7 156,7 @@ func authForOAuthClient(ctx context.Context, clientUUID string) (*AuthContext, e
if err := rows.Err(); err != nil {
panic(err)
}
- return fmt.Errorf("Authenticating for unknown client ID %s", clientUUID)
+ return errors.Errorf(errors.Unauthorized, "Authenticating for unknown client ID %s", clientUUID)
}
if err := rows.Scan(&auth.UserID, &auth.Username, &auth.Created,
&auth.Updated, &auth.Email, &auth.UserType, &auth.URL, &auth.Location,
@@ 168,7 168,7 @@ func authForOAuthClient(ctx context.Context, clientUUID string) (*AuthContext, e
if err := rows.Err(); err != nil {
panic(err)
}
- panic(errors.New("Multiple matching user accounts; invariant broken"))
+ panic(fmt.Errorf("Multiple matching user accounts; invariant broken"))
}
return nil
}); err != nil {
@@ 315,7 315,7 @@ func internalAuth(payload []byte, w http.ResponseWriter, r *http.Request, next h
func FetchMetaProfile(ctx context.Context, username string, user *AuthContext) error {
if config.ServiceName(ctx) == "meta.sr.ht" {
- panic(errors.New("Cannot fetch profile from ourselves"))
+ panic(fmt.Errorf("Cannot fetch profile from ourselves"))
}
query := client.GraphQLQuery{
@@ 392,7 392,7 @@ func FetchMetaProfile(ctx context.Context, username string, user *AuthContext) e
&user.Username, &user.Email, &user.UserType, &user.URL,
&user.Location, &user.Bio, &user.SuspensionNotice); err != nil {
if err == sql.ErrNoRows {
- panic(errors.New("Failed to upsert user record from meta.sr.ht"))
+ panic(fmt.Errorf("Failed to upsert user record from meta.sr.ht"))
}
return err
}
@@ 435,8 435,7 @@ func LookupUser(ctx context.Context, username string, user *AuthContext) error {
return err
}
if config.ServiceName(ctx) == "meta.sr.ht" {
- log.Printf("LookupUser: Unknown user %s", username)
- return fmt.Errorf("Unknown user %s", username)
+ return errors.Errorf(errors.Unauthorized, "Unknown user %s", username)
}
return FetchMetaProfile(ctx, username, user)
}
@@ 460,7 459,7 @@ func LookupUser(ctx context.Context, username string, user *AuthContext) error {
if err = rows.Err(); err != nil {
return err
}
- panic(errors.New("Multiple users of the same username; invariant broken"))
+ panic(fmt.Errorf("Multiple users of the same username; invariant broken"))
}
return nil
})
@@ 643,7 642,7 @@ func Middleware(conf ini.File, apiconf string) func(http.Handler) http.Handler {
func ForContext(ctx context.Context) *AuthContext {
raw, ok := ctx.Value(userCtxKey).(*AuthContext)
if !ok {
- panic(errors.New("Invalid authentication context"))
+ panic(fmt.Errorf("Invalid authentication context"))
}
return raw
}
@@ 35,6 35,7 @@ var (
AccessDenied ErrorCode = "ERR_ACCESS_DENIED"
NotFound ErrorCode = "ERR_NOT_FOUND"
Unsupported ErrorCode = "ERR_UNSUPPORTED"
+ Unauthorized ErrorCode = "ERR_UNAUTHORIZED"
)
// Error codes as Go errors
@@ 42,4 43,5 @@ var (
ErrAccessDenied = New(AccessDenied, "Access denied")
ErrNotFound = New(NotFound, "Resource not found")
ErrUnsupported = New(Unsupported, "Not supported")
+ ErrUnauthorized = New(Unauthorized, "Unauthorized")
)