Skip to content

Commit 0c4c28b

Browse files
authored
Add Swift login endpoint (go-gitea#32693) (go-gitea#32701)
Backport of go-gitea#32693 Fix go-gitea#32683 This PR adds the login endpoint and fixes the documentation links.
1 parent d8ad922 commit 0c4c28b

File tree

3 files changed

+77
-43
lines changed

3 files changed

+77
-43
lines changed

routers/api/packages/api.go

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -608,40 +608,46 @@ func CommonRoutes() *web.Route {
608608
}, reqPackageAccess(perm.AccessModeWrite))
609609
}, reqPackageAccess(perm.AccessModeRead))
610610
r.Group("/swift", func() {
611-
r.Group("/{scope}/{name}", func() {
612-
r.Group("", func() {
613-
r.Get("", swift.EnumeratePackageVersions)
614-
r.Get(".json", swift.EnumeratePackageVersions)
615-
}, swift.CheckAcceptMediaType(swift.AcceptJSON))
616-
r.Group("/{version}", func() {
617-
r.Get("/Package.swift", swift.CheckAcceptMediaType(swift.AcceptSwift), swift.DownloadManifest)
618-
r.Put("", reqPackageAccess(perm.AccessModeWrite), swift.CheckAcceptMediaType(swift.AcceptJSON), swift.UploadPackageFile)
619-
r.Get("", func(ctx *context.Context) {
620-
// Can't use normal routes here: https://github.com/go-chi/chi/issues/781
621-
622-
version := ctx.Params("version")
623-
if strings.HasSuffix(version, ".zip") {
624-
swift.CheckAcceptMediaType(swift.AcceptZip)(ctx)
625-
if ctx.Written() {
626-
return
627-
}
628-
ctx.SetParams("version", version[:len(version)-4])
629-
swift.DownloadPackageFile(ctx)
630-
} else {
631-
swift.CheckAcceptMediaType(swift.AcceptJSON)(ctx)
632-
if ctx.Written() {
633-
return
634-
}
635-
if strings.HasSuffix(version, ".json") {
636-
ctx.SetParams("version", version[:len(version)-5])
611+
r.Group("", func() { // Needs to be unauthenticated.
612+
r.Post("", swift.CheckAuthenticate)
613+
r.Post("/login", swift.CheckAuthenticate)
614+
})
615+
r.Group("", func() {
616+
r.Group("/{scope}/{name}", func() {
617+
r.Group("", func() {
618+
r.Get("", swift.EnumeratePackageVersions)
619+
r.Get(".json", swift.EnumeratePackageVersions)
620+
}, swift.CheckAcceptMediaType(swift.AcceptJSON))
621+
r.Group("/{version}", func() {
622+
r.Get("/Package.swift", swift.CheckAcceptMediaType(swift.AcceptSwift), swift.DownloadManifest)
623+
r.Put("", reqPackageAccess(perm.AccessModeWrite), swift.CheckAcceptMediaType(swift.AcceptJSON), swift.UploadPackageFile)
624+
r.Get("", func(ctx *context.Context) {
625+
// Can't use normal routes here: https://github.com/go-chi/chi/issues/781
626+
627+
version := ctx.Params("version")
628+
if strings.HasSuffix(version, ".zip") {
629+
swift.CheckAcceptMediaType(swift.AcceptZip)(ctx)
630+
if ctx.Written() {
631+
return
632+
}
633+
ctx.SetParams("version", version[:len(version)-4])
634+
swift.DownloadPackageFile(ctx)
635+
} else {
636+
swift.CheckAcceptMediaType(swift.AcceptJSON)(ctx)
637+
if ctx.Written() {
638+
return
639+
}
640+
if strings.HasSuffix(version, ".json") {
641+
ctx.SetParams("version", version[:len(version)-5])
642+
}
643+
swift.PackageVersionMetadata(ctx)
637644
}
638-
swift.PackageVersionMetadata(ctx)
639-
}
645+
})
640646
})
641647
})
642-
})
643-
r.Get("/identifiers", swift.CheckAcceptMediaType(swift.AcceptJSON), swift.LookupPackageIdentifiers)
644-
}, reqPackageAccess(perm.AccessModeRead))
648+
r.Get("/identifiers", swift.CheckAcceptMediaType(swift.AcceptJSON), swift.LookupPackageIdentifiers)
649+
}, reqPackageAccess(perm.AccessModeRead))
650+
})
645651
r.Group("/vagrant", func() {
646652
r.Group("/authenticate", func() {
647653
r.Get("", vagrant.CheckAuthenticate)

routers/api/packages/swift/swift.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ import (
2727
"github.com/hashicorp/go-version"
2828
)
2929

30-
// https://github.com/apple/swift-package-manager/blob/main/Documentation/Registry.md#35-api-versioning
30+
// https://github.com/swiftlang/swift-package-manager/blob/main/Documentation/PackageRegistry/Registry.md#35-api-versioning
3131
const (
3232
AcceptJSON = "application/vnd.swift.registry.v1+json"
3333
AcceptSwift = "application/vnd.swift.registry.v1+swift"
3434
AcceptZip = "application/vnd.swift.registry.v1+zip"
3535
)
3636

3737
var (
38-
// https://github.com/apple/swift-package-manager/blob/main/Documentation/Registry.md#361-package-scope
38+
// https://github.com/swiftlang/swift-package-manager/blob/main/Documentation/PackageRegistry/Registry.md#361-package-scope
3939
scopePattern = regexp.MustCompile(`\A[a-zA-Z0-9][a-zA-Z0-9-]{0,38}\z`)
40-
// https://github.com/apple/swift-package-manager/blob/main/Documentation/Registry.md#362-package-name
40+
// https://github.com/swiftlang/swift-package-manager/blob/main/Documentation/PackageRegistry/Registry.md#362-package-name
4141
namePattern = regexp.MustCompile(`\A[a-zA-Z0-9][a-zA-Z0-9-_]{0,99}\z`)
4242
)
4343

@@ -49,7 +49,7 @@ type headers struct {
4949
Link string
5050
}
5151

52-
// https://github.com/apple/swift-package-manager/blob/main/Documentation/Registry.md#35-api-versioning
52+
// https://github.com/swiftlang/swift-package-manager/blob/main/Documentation/PackageRegistry/Registry.md#35-api-versioning
5353
func setResponseHeaders(resp http.ResponseWriter, h *headers) {
5454
if h.ContentType != "" {
5555
resp.Header().Set("Content-Type", h.ContentType)
@@ -69,7 +69,7 @@ func setResponseHeaders(resp http.ResponseWriter, h *headers) {
6969
}
7070
}
7171

72-
// https://github.com/apple/swift-package-manager/blob/main/Documentation/Registry.md#33-error-handling
72+
// https://github.com/swiftlang/swift-package-manager/blob/main/Documentation/PackageRegistry/Registry.md#33-error-handling
7373
func apiError(ctx *context.Context, status int, obj any) {
7474
// https://www.rfc-editor.org/rfc/rfc7807
7575
type Problem struct {
@@ -91,7 +91,7 @@ func apiError(ctx *context.Context, status int, obj any) {
9191
})
9292
}
9393

94-
// https://github.com/apple/swift-package-manager/blob/main/Documentation/Registry.md#35-api-versioning
94+
// https://github.com/swiftlang/swift-package-manager/blob/main/Documentation/PackageRegistry/Registry.md#35-api-versioning
9595
func CheckAcceptMediaType(requiredAcceptHeader string) func(ctx *context.Context) {
9696
return func(ctx *context.Context) {
9797
accept := ctx.Req.Header.Get("Accept")
@@ -101,6 +101,16 @@ func CheckAcceptMediaType(requiredAcceptHeader string) func(ctx *context.Context
101101
}
102102
}
103103

104+
// https://github.com/swiftlang/swift-package-manager/blob/main/Documentation/PackageRegistry/PackageRegistryUsage.md#registry-authentication
105+
func CheckAuthenticate(ctx *context.Context) {
106+
if ctx.Doer == nil {
107+
apiError(ctx, http.StatusUnauthorized, nil)
108+
return
109+
}
110+
111+
ctx.Status(http.StatusOK)
112+
}
113+
104114
func buildPackageID(scope, name string) string {
105115
return scope + "." + name
106116
}
@@ -113,7 +123,7 @@ type EnumeratePackageVersionsResponse struct {
113123
Releases map[string]Release `json:"releases"`
114124
}
115125

116-
// https://github.com/apple/swift-package-manager/blob/main/Documentation/Registry.md#41-list-package-releases
126+
// https://github.com/swiftlang/swift-package-manager/blob/main/Documentation/PackageRegistry/Registry.md#41-list-package-releases
117127
func EnumeratePackageVersions(ctx *context.Context) {
118128
packageScope := ctx.Params("scope")
119129
packageName := ctx.Params("name")
@@ -170,7 +180,7 @@ type PackageVersionMetadataResponse struct {
170180
Metadata *swift_module.SoftwareSourceCode `json:"metadata"`
171181
}
172182

173-
// https://github.com/apple/swift-package-manager/blob/main/Documentation/Registry.md#endpoint-2
183+
// https://github.com/swiftlang/swift-package-manager/blob/main/Documentation/PackageRegistry/Registry.md#endpoint-2
174184
func PackageVersionMetadata(ctx *context.Context) {
175185
id := buildPackageID(ctx.Params("scope"), ctx.Params("name"))
176186

@@ -228,7 +238,7 @@ func PackageVersionMetadata(ctx *context.Context) {
228238
})
229239
}
230240

231-
// https://github.com/apple/swift-package-manager/blob/main/Documentation/Registry.md#43-fetch-manifest-for-a-package-release
241+
// https://github.com/swiftlang/swift-package-manager/blob/main/Documentation/PackageRegistry/Registry.md#43-fetch-manifest-for-a-package-release
232242
func DownloadManifest(ctx *context.Context) {
233243
packageScope := ctx.Params("scope")
234244
packageName := ctx.Params("name")
@@ -280,7 +290,7 @@ func DownloadManifest(ctx *context.Context) {
280290
})
281291
}
282292

283-
// https://github.com/apple/swift-package-manager/blob/main/Documentation/Registry.md#endpoint-6
293+
// https://github.com/swiftlang/swift-package-manager/blob/main/Documentation/PackageRegistry/Registry.md#endpoint-6
284294
func UploadPackageFile(ctx *context.Context) {
285295
packageScope := ctx.Params("scope")
286296
packageName := ctx.Params("name")
@@ -379,7 +389,7 @@ func UploadPackageFile(ctx *context.Context) {
379389
ctx.Status(http.StatusCreated)
380390
}
381391

382-
// https://github.com/apple/swift-package-manager/blob/main/Documentation/Registry.md#endpoint-4
392+
// https://github.com/swiftlang/swift-package-manager/blob/main/Documentation/PackageRegistry/Registry.md#endpoint-4
383393
func DownloadPackageFile(ctx *context.Context) {
384394
pv, err := packages_model.GetVersionByNameAndVersion(ctx, ctx.Package.Owner.ID, packages_model.TypeSwift, buildPackageID(ctx.Params("scope"), ctx.Params("name")), ctx.Params("version"))
385395
if err != nil {
@@ -420,7 +430,7 @@ type LookupPackageIdentifiersResponse struct {
420430
Identifiers []string `json:"identifiers"`
421431
}
422432

423-
// https://github.com/apple/swift-package-manager/blob/main/Documentation/Registry.md#endpoint-5
433+
// https://github.com/swiftlang/swift-package-manager/blob/main/Documentation/PackageRegistry/Registry.md#endpoint-5
424434
func LookupPackageIdentifiers(ctx *context.Context) {
425435
url := ctx.FormTrim("url")
426436
if url == "" {

tests/integration/api_packages_swift_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,24 @@ func TestPackageSwift(t *testing.T) {
4242

4343
url := fmt.Sprintf("/api/packages/%s/swift", user.Name)
4444

45+
t.Run("CheckLogin", func(t *testing.T) {
46+
defer tests.PrintCurrentTest(t)()
47+
48+
req := NewRequestWithBody(t, "POST", url, strings.NewReader(""))
49+
MakeRequest(t, req, http.StatusUnauthorized)
50+
51+
req = NewRequestWithBody(t, "POST", url, strings.NewReader("")).
52+
AddBasicAuth(user.Name)
53+
MakeRequest(t, req, http.StatusOK)
54+
55+
req = NewRequestWithBody(t, "POST", url+"/login", strings.NewReader(""))
56+
MakeRequest(t, req, http.StatusUnauthorized)
57+
58+
req = NewRequestWithBody(t, "POST", url+"/login", strings.NewReader("")).
59+
AddBasicAuth(user.Name)
60+
MakeRequest(t, req, http.StatusOK)
61+
})
62+
4563
t.Run("CheckAcceptMediaType", func(t *testing.T) {
4664
defer tests.PrintCurrentTest(t)()
4765

0 commit comments

Comments
 (0)