mirror of
https://gitlab.melroy.org/melroy/fediresolve.git
synced 2025-06-07 20:08:57 +00:00
Clean up code
This commit is contained in:
parent
f0399df34a
commit
b951b920ac
1 changed files with 42 additions and 39 deletions
|
@ -18,7 +18,7 @@ import (
|
|||
// Define common constants
|
||||
const (
|
||||
// UserAgent is the user agent string used for all HTTP requests
|
||||
UserAgent = "FediResolve/1.0 (https://melroy.org)"
|
||||
UserAgent = "FediResolve/1.0 (https://github.com/melroy89/FediResolve)"
|
||||
AcceptHeader = "application/activity+json, application/ld+json"
|
||||
)
|
||||
|
||||
|
@ -33,32 +33,11 @@ func (r *Resolver) fetchActivityPubObjectWithSignature(objectURL string) ([]byte
|
|||
return nil, nil, err
|
||||
}
|
||||
|
||||
var keyID string
|
||||
actorURL, ok := data["attributedTo"].(string)
|
||||
if !ok || actorURL == "" {
|
||||
fmt.Printf("Could not find attributedTo in object\n")
|
||||
// Try to find key in the object itself
|
||||
// Try to catch an error
|
||||
if _, ok := data["publicKey"].(map[string]interface{}); !ok {
|
||||
return nil, nil, fmt.Errorf("could not find public key in object")
|
||||
}
|
||||
if _, ok := data["publicKey"].(map[string]interface{})["id"]; !ok {
|
||||
return nil, nil, fmt.Errorf("could not find public key ID in object")
|
||||
}
|
||||
keyID = data["publicKey"].(map[string]interface{})["id"].(string)
|
||||
} else {
|
||||
// Fetch actor data
|
||||
_, actorData, err := r.fetchActorData(actorURL)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("could not fetch actor data: %v", err)
|
||||
}
|
||||
// Extract the public key ID
|
||||
key, err := r.extractPublicKey(actorData)
|
||||
keyID, err := r.extractPublicKey(data)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("could not extract public key: %v", err)
|
||||
}
|
||||
keyID = key
|
||||
}
|
||||
|
||||
// Create a new private key for signing (in a real app, we would use a persistent key)
|
||||
privateKey, err := generateRSAKey()
|
||||
|
@ -227,8 +206,32 @@ func (r *Resolver) fetchActorData(actorURL string) ([]byte, map[string]interface
|
|||
}
|
||||
|
||||
// extractPublicKey extracts the public key ID from actor data
|
||||
func (r *Resolver) extractPublicKey(actorData map[string]interface{}) (string, error) {
|
||||
func (r *Resolver) extractPublicKey(data map[string]interface{}) (string, error) {
|
||||
// Convert to JSON string for easier parsing with gjson
|
||||
dataJSON, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error marshaling actor data: %v", err)
|
||||
}
|
||||
|
||||
// Try to find the attributedTo URL using dataJSON
|
||||
actorURL := gjson.GetBytes(dataJSON, "attributedTo").String()
|
||||
|
||||
if actorURL == "" {
|
||||
fmt.Printf("Could not find attributedTo in object\n")
|
||||
// Try to find key in the object itself
|
||||
keyID := gjson.GetBytes(dataJSON, "publicKey.id").String()
|
||||
|
||||
if keyID == "" {
|
||||
return "", fmt.Errorf("could not find public key ID in object")
|
||||
}
|
||||
return keyID, nil
|
||||
} else {
|
||||
_, actorData, err := r.fetchActorData(actorURL)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error fetching actor data: %v", err)
|
||||
}
|
||||
|
||||
// Convert actorData to JSON
|
||||
actorJSON, err := json.Marshal(actorData)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error marshaling actor data: %v", err)
|
||||
|
@ -244,9 +247,9 @@ func (r *Resolver) extractPublicKey(actorData map[string]interface{}) (string, e
|
|||
fmt.Printf("could not find public key ID in actor data")
|
||||
return "dummy", nil
|
||||
}
|
||||
|
||||
return keyID, nil
|
||||
}
|
||||
}
|
||||
|
||||
// generateRSAKey generates a new RSA key pair for signing requests
|
||||
func generateRSAKey() (*rsa.PrivateKey, error) {
|
||||
|
|
Loading…
Add table
Reference in a new issue