This commit is contained in:
Drew DeVault 2020-09-17 11:20:02 -04:00
parent cda96010d2
commit 1fed0ae18c
8 changed files with 24 additions and 26 deletions

View File

@ -33,7 +33,7 @@ type contextKey struct {
}
var (
oauthBearerRegex = regexp.MustCompile(`^[0-9a-f]{32}$`)
oauthBearerRegex = regexp.MustCompile(`^[0-9a-f]{32}$`)
oauth2BearerRegex = regexp.MustCompile(`^[0-9a-zA-Z_+/]{33,}$`)
)
@ -50,9 +50,9 @@ const (
const (
AUTH_OAUTH_LEGACY = iota
AUTH_OAUTH2 = iota
AUTH_COOKIE = iota
AUTH_INTERNAL = iota
AUTH_OAUTH2 = iota
AUTH_COOKIE = iota
AUTH_INTERNAL = iota
)
// XXX: Rename to AuthContext
@ -171,13 +171,13 @@ func cookieAuth(db *sql.DB, cookie *http.Cookie,
type InternalAuth struct {
// The username of the authenticated user
Name string `json:"name"`
Name string `json:"name"`
// An arbitrary identifier for this internal user, e.g. "git.sr.ht"
ClientID string `json:"client_id"`
// An arbitrary identifier for this internal node, e.g. "us-east-3.git.sr.ht"
NodeID string `json:"node_id"`
NodeID string `json:"node_id"`
}
func internalAuth(internalNet []*net.IPNet, db *sql.DB, payload []byte,
@ -307,8 +307,7 @@ func FetchMetaProfile(ctx context.Context, db *sql.DB,
location,
bio,
suspension_notice
);
`,
);`,
profile.Username, profile.Email, profile.UserType, profile.URL,
profile.Location, profile.Bio, profile.SuspensionNotice)
@ -387,8 +386,8 @@ func LookupTokenRevocation(ctx context.Context,
query RevocationStatus($hash: String!, $clientId: String) {
tokenRevocationStatus(hash: $hash, clientId: $clientId)
}`,
Variables: map[string]interface{} {
"hash": hex.EncodeToString(hash[:]),
Variables: map[string]interface{}{
"hash": hex.EncodeToString(hash[:]),
"clientId": clientID,
},
}
@ -638,7 +637,6 @@ func Middleware(conf ini.File, apiconf string) func(http.Handler) http.Handler {
}
}
func ForContext(ctx context.Context) *User {
raw, ok := ctx.Value(userCtxKey).(*User)
if !ok {

View File

@ -51,8 +51,8 @@ func DecodeToken(token string) *OAuth2Token {
return nil
}
mac := payload[len(payload) - 32:]
payload = payload[:len(payload) - 32]
mac := payload[len(payload)-32:]
payload = payload[:len(payload)-32]
if crypto.HMACVerify(payload, mac) == false {
log.Printf("Invalid bearer token: HMAC verification failed (MAC: [%d]%s; payload: [%d]%s)",
len(mac), hex.EncodeToString(mac), len(payload), hex.EncodeToString(payload))

View File

@ -44,7 +44,7 @@ func Execute(ctx context.Context, username string, svc string,
}
req.Header.Add("Content-Type", "application/json")
auth := InternalAuth{
Name: username,
Name: username,
// TODO: Populate these better
ClientID: "gql.sr.ht",
NodeID: "gql.sr.ht",

View File

@ -15,9 +15,9 @@ type contextKey struct {
name string
}
func Middleware(conf ini.File, service string) func (next http.Handler) http.Handler {
func Middleware(conf ini.File, service string) func(next http.Handler) http.Handler {
svc := service
return func (next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), configCtxKey, conf)
ctx = context.WithValue(ctx, serviceCtxKey, &svc)

View File

@ -2,8 +2,8 @@ package database
import (
"context"
"errors"
"database/sql"
"errors"
"net/http"
)

View File

@ -25,7 +25,7 @@ import (
// Provides a graphql.RecoverFunc which will print the stack trace, and if
// debug mode is not enabled, email it to the administrator.
func EmailRecover(config ini.File, debug bool, srv string) graphql.RecoverFunc {
return func (ctx context.Context, _origErr interface{}) error {
return func(ctx context.Context, _origErr interface{}) error {
var (
ok bool
origErr error

View File

@ -10,9 +10,9 @@ import (
// TODO: Add field to prevent cursor reuse across unrelated resources
type Cursor struct {
Count int `json:"count"`
Next string `json:"next"`
Search string `json:"search"`
Count int `json:"count"`
Next string `json:"next"`
Search string `json:"search"`
}
func (cur *Cursor) UnmarshalGQL(v interface{}) error {
@ -62,8 +62,8 @@ func NewCursor(filter *Filter) *Cursor {
}
}
return &Cursor{
Count: 25,
Next: "",
Search: "",
Count: 25,
Next: "",
Search: "",
}
}

View File

@ -15,12 +15,12 @@ import (
"github.com/99designs/gqlgen/handler"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
goRedis "github.com/go-redis/redis/v8"
_ "github.com/lib/pq"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/vaughan0/go-ini"
_ "github.com/lib/pq"
goRedis "github.com/go-redis/redis/v8"
"git.sr.ht/~sircmpwn/gql.sr.ht/auth"
"git.sr.ht/~sircmpwn/gql.sr.ht/config"