Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ca1a458

Browse files
committed
Change route /api-keys -> /users/{user}/keys
1 parent dccb009 commit ca1a458

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

coderd/coderd.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,6 @@ func New(options *Options) http.Handler {
3636
})
3737
r.Post("/login", api.postLogin)
3838
r.Post("/logout", api.postLogout)
39-
r.Route("/api-keys", func(r chi.Router) {
40-
r.Use(
41-
httpmw.ExtractAPIKey(options.Database, nil),
42-
)
43-
r.Post("/", api.postAPIKey)
44-
})
4539

4640
// Used for setup.
4741
r.Get("/user", api.user)
@@ -51,10 +45,12 @@ func New(options *Options) http.Handler {
5145
httpmw.ExtractAPIKey(options.Database, nil),
5246
)
5347
r.Post("/", api.postUsers)
54-
r.Group(func(r chi.Router) {
48+
49+
r.Route("/{user}", func(r chi.Router) {
5550
r.Use(httpmw.ExtractUserParam(options.Database))
56-
r.Get("/{user}", api.userByName)
57-
r.Get("/{user}/organizations", api.organizationsByUser)
51+
r.Get("/", api.userByName)
52+
r.Get("/organizations", api.organizationsByUser)
53+
r.Post("/keys", api.postKeyForUser)
5854
})
5955
})
6056
r.Route("/projects", func(r chi.Router) {

coderd/users.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,17 @@ func (api *api) postLogin(rw http.ResponseWriter, r *http.Request) {
317317
})
318318
}
319319

320-
// Creates a new API key, used for logging in via the CLI
321-
func (api *api) postAPIKey(rw http.ResponseWriter, r *http.Request) {
320+
// Creates a new session key, used for logging in via the CLI
321+
func (api *api) postKeyForUser(rw http.ResponseWriter, r *http.Request) {
322+
user := httpmw.UserParam(r)
322323
apiKey := httpmw.APIKey(r)
323-
userID := apiKey.UserID
324+
325+
if user.ID != apiKey.UserID {
326+
httpapi.Write(rw, http.StatusUnauthorized, httpapi.Response{
327+
Message: "Keys can only be generated for the authenticated user",
328+
})
329+
return
330+
}
324331

325332
keyID, keySecret, err := generateAPIKeyIDSecret()
326333
if err != nil {
@@ -333,7 +340,7 @@ func (api *api) postAPIKey(rw http.ResponseWriter, r *http.Request) {
333340

334341
_, err = api.Database.InsertAPIKey(r.Context(), database.InsertAPIKeyParams{
335342
ID: keyID,
336-
UserID: userID,
343+
UserID: apiKey.UserID,
337344
ExpiresAt: database.Now().AddDate(1, 0, 0), // Expire after 1 year (same as v1)
338345
CreatedAt: database.Now(),
339346
UpdatedAt: database.Now(),

coderd/users_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func TestOrganizationsByUser(t *testing.T) {
119119
require.Len(t, orgs, 1)
120120
}
121121

122-
func TestPostAPIKey(t *testing.T) {
122+
func TestPostKey(t *testing.T) {
123123
t.Parallel()
124124
t.Run("InvalidUser", func(t *testing.T) {
125125
t.Parallel()

codersdk/users.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (c *Client) CreateUser(ctx context.Context, req coderd.CreateUserRequest) (
5858

5959
// CreateAPIKey calls the /api-key API
6060
func (c *Client) CreateAPIKey(ctx context.Context) (*coderd.GenerateAPIKeyResponse, error) {
61-
res, err := c.request(ctx, http.MethodPost, "/api/v2/api-keys", nil)
61+
res, err := c.request(ctx, http.MethodPost, "/api/v2/users/me/keys", nil)
6262
if err != nil {
6363
return nil, err
6464
}

0 commit comments

Comments
 (0)