-
Notifications
You must be signed in to change notification settings - Fork 12
/
key.go
65 lines (49 loc) · 1.75 KB
/
key.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package activitypub_user
import (
"net/http"
"github.com/EmissarySocial/emissary/model"
"github.com/EmissarySocial/emissary/server"
"github.com/benpate/derp"
"github.com/benpate/rosetta/mapof"
"github.com/labstack/echo/v4"
"go.mongodb.org/mongo-driver/bson/primitive"
)
func GetPublicKey(serverFactory *server.Factory) echo.HandlerFunc {
const location = "handler.activitypub.ActivityPub_GetPublicKey"
return func(ctx echo.Context) error {
// Parse the UserID parameter
userID, err := primitive.ObjectIDFromHex(ctx.Param("userId"))
if err != nil {
return derp.Wrap(err, location, "UserID must be a valid ObjectID", ctx.Param("userId"))
}
// Try to get the factory for this Domain
factory, err := serverFactory.ByContext(ctx)
if err != nil {
return derp.Wrap(err, location, "Error getting server factory")
}
// Try to load the user (to confirm that it exists)
userService := factory.User()
user := model.NewUser()
if err := userService.LoadByID(userID, &user); err != nil {
return derp.Wrap(err, location, "Error loading user", userID)
}
// RULE: Only public users can be queried
if !user.IsPublic {
return derp.NewNotFoundError(location, "")
}
// Try to load the key from the Datbase
keyService := factory.EncryptionKey()
key := model.NewEncryptionKey()
if err := keyService.LoadByParentID(model.EncryptionKeyTypeUser, userID, &key); err != nil {
return derp.Wrap(err, location, "Error loading encryption key for user", userID)
}
// Return the key as JSON-LD
result := mapof.Any{
"@context": "https://w3id.org/security/v1",
"id": keyService.KeyID(&key),
"owner": keyService.OwnerID(&key),
"publicKeyPem": key.PublicPEM,
}
return ctx.JSON(http.StatusOK, result)
}
}