diff --git a/auth/middleware.go b/auth/middleware.go index 03321ee2cb1f484d8a59c9efa6c1e0d2fc279ed1..508c4beea2542b6b606b98e95cbaffb1df5b4de7 100644 --- a/auth/middleware.go +++ b/auth/middleware.go @@ -320,23 +320,6 @@ if config.ServiceName(ctx) == "meta.sr.ht" { panic(errors.New("Cannot fetch profile from ourselves")) } - type GraphQLProfile struct { - ID int `json:"id"` - Username string `json:"username"` - Email string `json:"email"` - URL *string `json:"url"` - Location *string `json:"location"` - Bio *string `json:"bio"` - UserType string `json:"userType"` - SuspensionNotice string `json:"suspensionNotice"` - } - - type GraphQLResponse struct { - Data struct { - Me GraphQLProfile `json:"me"` - } `json:"data"` - } - query := client.GraphQLQuery{ Query: ` query { @@ -352,13 +335,24 @@ } }`, } - var result GraphQLResponse - if err := client.Execute(ctx, username, + var result struct { + Me struct { + ID int `json:"id"` + Username string `json:"username"` + Email string `json:"email"` + URL *string `json:"url"` + Location *string `json:"location"` + Bio *string `json:"bio"` + UserType string `json:"userType"` + SuspensionNotice string `json:"suspensionNotice"` + } `json:"me"` + } + if err := client.Do(ctx, username, "meta.sr.ht", query, &result); err != nil { return err } - profile := result.Data.Me + profile := result.Me return database.WithTx(ctx, nil, func(tx *sql.Tx) error { // TODO: Make the database representation consistent with this ut := strings.ToLower(profile.UserType) @@ -474,11 +468,6 @@ // Returns true if this token or client ID has been revoked (and therefore // should not be trusted) func LookupTokenRevocation(ctx context.Context, username string, hash [64]byte, clientID string) (bool, error) { - type GraphQLResponse struct { - Data struct { - RevocationStatus bool `json:"tokenRevocationStatus"` - } `json:"data"` - } query := client.GraphQLQuery{ Query: ` @@ -491,12 +480,14 @@ "clientId": clientID, }, } - var result GraphQLResponse - if err := client.Execute(ctx, username, + var result struct { + RevocationStatus bool `json:"tokenRevocationStatus"` + } + if err := client.Do(ctx, username, "meta.sr.ht", query, &result); err != nil { return true, err } - return result.Data.RevocationStatus, nil + return result.RevocationStatus, nil } func OAuth2(token string, hash [64]byte, w http.ResponseWriter, diff --git a/client/graphql.go b/client/graphql.go index 0b986057ce1dd434808677fd52b78f633b86eeed..59ce62e8dfeba275c3786b8f1abe2e8ef4aeb250 100644 --- a/client/graphql.go +++ b/client/graphql.go @@ -25,7 +25,7 @@ ClientID string `json:"client_id"` NodeID string `json:"node_id"` } -func Execute(ctx context.Context, username string, svc string, +func Do(ctx context.Context, username string, svc string, query GraphQLQuery, result interface{}) error { body, err := json.Marshal(query)