Pārlūkot izejas kodu

api: Allow unauthenticated access to user's SSH keys

This patch relaxes constraints on getting user's SSH keys via the JSON
API. The same has been allowed by both GitHub and Gitlab and the output
is already readable via http://domain/user.keys endpoint.

The benefit of allowing it via the API are twofold: first this is
a structured output and second it can be CORS-enabled.

As a privacy precaution the `Title` property is set to an empty string
if the request is unauthenticated.

Fixes: https://github.com/go-gitea/gitea/issues/30681
undefined
Wiktor Kwapisiewicz pirms 1 mēnesi
vecāks
revīzija
69afd9dc39
Neizdevās izgūt parakstu
2 mainītis faili ar 15 papildinājumiem un 3 dzēšanām
  1. 7
    1
      routers/api/v1/api.go
  2. 8
    2
      routers/api/v1/user/key.go

+ 7
- 1
routers/api/v1/api.go Parādīt failu

// Users (requires user scope) // Users (requires user scope)
m.Group("/users", func() { m.Group("/users", func() {
m.Group("/{username}", func() { m.Group("/{username}", func() {
m.Get("/keys", user.ListPublicKeys)
m.Get("/gpg_keys", user.ListGPGKeys) m.Get("/gpg_keys", user.ListGPGKeys)


m.Get("/followers", user.ListFollowers) m.Get("/followers", user.ListFollowers)
}, context.UserAssignmentAPI()) }, context.UserAssignmentAPI())
}, tokenRequiresScopes(auth_model.AccessTokenScopeCategoryUser), reqToken()) }, tokenRequiresScopes(auth_model.AccessTokenScopeCategoryUser), reqToken())


// Users SSH keys (publicly readable)
m.Group("/users", func() {
m.Group("/{username}", func() {
m.Get("/keys", user.ListPublicKeys)
}, context.UserAssignmentAPI())
})

// Users (requires user scope) // Users (requires user scope)
m.Group("/user", func() { m.Group("/user", func() {
m.Get("", user.GetAuthenticatedUser) m.Get("", user.GetAuthenticatedUser)

+ 8
- 2
routers/api/v1/user/key.go Parādīt failu

apiKeys := make([]*api.PublicKey, len(keys)) apiKeys := make([]*api.PublicKey, len(keys))
for i := range keys { for i := range keys {
apiKeys[i] = convert.ToPublicKey(apiLink, keys[i]) apiKeys[i] = convert.ToPublicKey(apiLink, keys[i])
if ctx.Doer.IsAdmin || ctx.Doer.ID == keys[i].OwnerID {
apiKeys[i], _ = appendPrivateInformation(ctx, apiKeys[i], keys[i], user)
if ctx.Doer != nil {
if ctx.Doer.IsAdmin || ctx.Doer.ID == keys[i].OwnerID {
apiKeys[i], _ = appendPrivateInformation(ctx, apiKeys[i], keys[i], user)
}
} else {
// unauthenticated requests will not receive the title property
// to preserve privacy
apiKeys[i].Title = ""
} }
} }



Notiek ielāde…
Atcelt
Saglabāt