Skip to content

Commit 1364083

Browse files
authored
Add Swift login endpoint (#32693)
Fix #32683 This PR adds the login endpoint and fixes the documentation links.
1 parent 829c341 commit 1364083

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
@@ -610,40 +610,46 @@ func CommonRoutes() *web.Router {
610610
}, reqPackageAccess(perm.AccessModeWrite))
611611
}, reqPackageAccess(perm.AccessModeRead))
612612
r.Group("/swift", func() {
613-
r.Group("/{scope}/{name}", func() {
614-
r.Group("", func() {
615-
r.Get("", swift.EnumeratePackageVersions)
616-
r.Get(".json", swift.EnumeratePackageVersions)
617-
}, swift.CheckAcceptMediaType(swift.AcceptJSON))
618-
r.Group("/{version}", func() {
619-
r.Get("/Package.swift", swift.CheckAcceptMediaType(swift.AcceptSwift), swift.DownloadManifest)
620-
r.Put("", reqPackageAccess(perm.AccessModeWrite), swift.CheckAcceptMediaType(swift.AcceptJSON), swift.UploadPackageFile)
621-
r.Get("", func(ctx *context.Context) {
622-
// Can't use normal routes here: https://github.com/go-chi/chi/issues/781
623-
624-
version := ctx.PathParam("version")
625-
if strings.HasSuffix(version, ".zip") {
626-
swift.CheckAcceptMediaType(swift.AcceptZip)(ctx)
627-
if ctx.Written() {
628-
return
629-
}
630-
ctx.SetPathParam("version", version[:len(version)-4])
631-
swift.DownloadPackageFile(ctx)
632-
} else {
633-
swift.CheckAcceptMediaType(swift.AcceptJSON)(ctx)
634-
if ctx.Written() {
635-
return
636-
}
637-
if strings.HasSuffix(version, ".json") {
638-
ctx.SetPathParam("version", version[:len(version)-5])
613+
r.Group("", func() { // Needs to be unauthenticated.
614+
r.Post("", swift.CheckAuthenticate)
615+
r.Post("/login", swift.CheckAuthenticate)
616+
})
617+
r.Group("", func() {
618+
r.Group("/{scope}/{name}", func() {
619+
r.Group("", func() {
620+
r.Get("", swift.EnumeratePackageVersions)
621+
r.Get(".json", swift.EnumeratePackageVersions)
622+
}, swift.CheckAcceptMediaType(swift.AcceptJSON))
623+
r.Group("/{version}", func() {
624+
r.Get("/Package.swift", swift.CheckAcceptMediaType(swift.AcceptSwift), swift.DownloadManifest)
625+
r.Put("", reqPackageAccess(perm.AccessModeWrite), swift.CheckAcceptMediaType(swift.AcceptJSON), swift.UploadPackageFile)
626+
r.Get("", func(ctx *context.Context) {
627+
// Can't use normal routes here: https://github.com/go-chi/chi/issues/781
628+
629+
version := ctx.PathParam("version")
630+
if strings.HasSuffix(version, ".zip") {
631+
swift.CheckAcceptMediaType(swift.AcceptZip)(ctx)
632+
if ctx.Written() {
633+
return
634+
}
635+
ctx.SetPathParam("version", version[:len(version)-4])
636+
swift.DownloadPackageFile(ctx)
637+
} else {
638+
swift.CheckAcceptMediaType(swift.AcceptJSON)(ctx)
639+
if ctx.Written() {
640+
return
641+
}
642+
if strings.HasSuffix(version, ".json") {
643+
ctx.SetPathParam("version", version[:len(version)-5])
644+
}
645+
swift.PackageVersionMetadata(ctx)
639646
}
640-
swift.PackageVersionMetadata(ctx)
641-
}
647+
})
642648
})
643649
})
644-
})
645-
r.Get("/identifiers", swift.CheckAcceptMediaType(swift.AcceptJSON), swift.LookupPackageIdentifiers)
646-
}, reqPackageAccess(perm.AccessModeRead))
650+
r.Get("/identifiers", swift.CheckAcceptMediaType(swift.AcceptJSON), swift.LookupPackageIdentifiers)
651+
}, reqPackageAccess(perm.AccessModeRead))
652+
})
647653
r.Group("/vagrant", func() {
648654
r.Group("/authenticate", func() {
649655
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.PathParam("scope")
119129
packageName := ctx.PathParam("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.PathParam("scope"), ctx.PathParam("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.PathParam("scope")
234244
packageName := ctx.PathParam("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.PathParam("scope")
286296
packageName := ctx.PathParam("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.PathParam("scope"), ctx.PathParam("name")), ctx.PathParam("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)