Skip to content

Commit 3fa75ea

Browse files
authored
chore(api): generated docs - login (#2199)
1 parent 217ec38 commit 3fa75ea

File tree

10 files changed

+95
-38
lines changed

10 files changed

+95
-38
lines changed

api/auth.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,6 @@ import (
1010
"github.com/replicatedhq/embedded-cluster/api/types"
1111
)
1212

13-
type AuthRequest struct {
14-
Password string `json:"password"`
15-
}
16-
17-
type AuthResponse struct {
18-
Token string `json:"token"`
19-
}
20-
2113
func (a *API) authMiddleware(next http.Handler) http.Handler {
2214
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2315
token := r.Header.Get("Authorization")
@@ -48,8 +40,19 @@ func (a *API) authMiddleware(next http.Handler) http.Handler {
4840
})
4941
}
5042

43+
// postAuthLogin handler to authenticate a user
44+
//
45+
// @Summary Authenticate a user
46+
// @Description Authenticate a user
47+
// @Tags auth
48+
// @Accept json
49+
// @Produce json
50+
// @Param request body types.AuthRequest true "Auth Request"
51+
// @Success 200 {object} types.AuthResponse
52+
// @Failure 401 {object} types.APIError
53+
// @Router /auth/login [post]
5154
func (a *API) postAuthLogin(w http.ResponseWriter, r *http.Request) {
52-
var request AuthRequest
55+
var request types.AuthRequest
5356
err := json.NewDecoder(r.Body).Decode(&request)
5457
if err != nil {
5558
a.logError(r, err, "failed to decode auth request")
@@ -69,7 +72,7 @@ func (a *API) postAuthLogin(w http.ResponseWriter, r *http.Request) {
6972
return
7073
}
7174

72-
response := AuthResponse{
75+
response := types.AuthResponse{
7376
Token: token,
7477
}
7578

api/client/auth.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@ import (
44
"bytes"
55
"encoding/json"
66
"net/http"
7-
)
87

9-
// Login sends a login request to the API server with the provided password and retrieves a session token. The token is stored in the client struct for subsequent requests.
10-
func (c *client) Login(password string) error {
11-
loginReq := struct {
12-
Password string `json:"password"`
13-
}{
14-
Password: password,
15-
}
8+
"github.com/replicatedhq/embedded-cluster/api/types"
9+
)
1610

17-
b, err := json.Marshal(loginReq)
11+
// Authenticate sends a login request to the API server with the provided password and retrieves a
12+
// session token. The token is stored in the client struct for subsequent requests.
13+
func (c *client) Authenticate(password string) error {
14+
b, err := json.Marshal(types.AuthRequest{Password: password})
1815
if err != nil {
1916
return err
2017
}
@@ -35,14 +32,12 @@ func (c *client) Login(password string) error {
3532
return errorFromResponse(resp)
3633
}
3734

38-
var loginResp struct {
39-
Token string `json:"token"`
40-
}
41-
err = json.NewDecoder(resp.Body).Decode(&loginResp)
35+
var authResponse types.AuthResponse
36+
err = json.NewDecoder(resp.Body).Decode(&authResponse)
4237
if err != nil {
4338
return err
4439
}
4540

46-
c.token = loginResp.Token
41+
c.token = authResponse.Token
4742
return nil
4843
}

api/client/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
type Client interface {
13-
Login(password string) error
13+
Authenticate(password string) error
1414
GetInstall() (*types.Install, error)
1515
SetInstallConfig(config types.InstallationConfig) (*types.Install, error)
1616
SetInstallStatus(status types.InstallationStatus) (*types.Install, error)

api/client/client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func TestLogin(t *testing.T) {
7979

8080
// Test successful login
8181
c := New(server.URL)
82-
err := c.Login("correct-password")
82+
err := c.Authenticate("correct-password")
8383
assert.NoError(t, err)
8484

8585
// Check that token was set
@@ -89,7 +89,7 @@ func TestLogin(t *testing.T) {
8989

9090
// Test failed login
9191
c = New(server.URL)
92-
err = c.Login("wrong-password")
92+
err = c.Authenticate("wrong-password")
9393
assert.Error(t, err)
9494

9595
// Check that error is of type APIError

api/docs/docs.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/docs/swagger.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
"components": {"schemas":{"types.Health":{"properties":{"status":{"type":"string"}},"type":"object"},"types.Install":{"properties":{"config":{"$ref":"#/components/schemas/types.InstallationConfig"},"status":{"$ref":"#/components/schemas/types.InstallationStatus"}},"type":"object"},"types.InstallationConfig":{"properties":{"adminConsolePort":{"type":"integer"},"dataDirectory":{"type":"string"},"globalCidr":{"type":"string"},"httpProxy":{"type":"string"},"httpsProxy":{"type":"string"},"localArtifactMirrorPort":{"type":"integer"},"networkInterface":{"type":"string"},"noProxy":{"type":"string"},"podCidr":{"type":"string"},"serviceCidr":{"type":"string"}},"type":"object"},"types.InstallationState":{"type":"string","x-enum-varnames":["InstallationStatePending","InstallationStateRunning","InstallationStateSucceeded","InstallationStateFailed"]},"types.InstallationStatus":{"properties":{"description":{"type":"string"},"lastUpdated":{"type":"string"},"state":{"$ref":"#/components/schemas/types.InstallationState"}},"type":"object"}},"securitySchemes":{"bearerauth":{"bearerFormat":"JWT","scheme":"bearer","type":"http"}}},
2+
"components": {"schemas":{"types.APIError":{"properties":{"errors":{"items":{"$ref":"#/components/schemas/types.APIError"},"type":"array","uniqueItems":false},"field":{"type":"string"},"message":{"type":"string"},"status_code":{"type":"integer"}},"type":"object"},"types.AuthRequest":{"properties":{"password":{"type":"string"}},"type":"object"},"types.AuthResponse":{"properties":{"token":{"type":"string"}},"type":"object"},"types.Health":{"properties":{"status":{"type":"string"}},"type":"object"},"types.Install":{"properties":{"config":{"$ref":"#/components/schemas/types.InstallationConfig"},"status":{"$ref":"#/components/schemas/types.InstallationStatus"}},"type":"object"},"types.InstallationConfig":{"properties":{"adminConsolePort":{"type":"integer"},"dataDirectory":{"type":"string"},"globalCidr":{"type":"string"},"httpProxy":{"type":"string"},"httpsProxy":{"type":"string"},"localArtifactMirrorPort":{"type":"integer"},"networkInterface":{"type":"string"},"noProxy":{"type":"string"},"podCidr":{"type":"string"},"serviceCidr":{"type":"string"}},"type":"object"},"types.InstallationState":{"type":"string","x-enum-varnames":["InstallationStatePending","InstallationStateRunning","InstallationStateSucceeded","InstallationStateFailed"]},"types.InstallationStatus":{"properties":{"description":{"type":"string"},"lastUpdated":{"type":"string"},"state":{"$ref":"#/components/schemas/types.InstallationState"}},"type":"object"}},"securitySchemes":{"bearerauth":{"bearerFormat":"JWT","scheme":"bearer","type":"http"}}},
33
"info": {"contact":{"email":"[email protected]","name":"API Support","url":"https://github.com/replicatedhq/embedded-cluster/issues"},"description":"This is the API for the Embedded Cluster project.","license":{"name":"Apache 2.0","url":"http://www.apache.org/licenses/LICENSE-2.0.html"},"termsOfService":"http://swagger.io/terms/","title":"Embedded Cluster API","version":"0.1"},
44
"externalDocs": {"description":"OpenAPI","url":"https://swagger.io/resources/open-api/"},
5-
"paths": {"/health":{"get":{"description":"get the health of the API","responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/types.Health"}}},"description":"OK"}},"summary":"Get the health of the API","tags":["health"]}},"/install":{"get":{"description":"get the install object","responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/types.Install"}}},"description":"OK"}},"security":[{"bearerauth":[]}],"summary":"Get the install object","tags":["install"]}}},
5+
"paths": {"/auth/login":{"post":{"description":"Authenticate a user","requestBody":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/types.AuthRequest"}}},"description":"Auth Request","required":true},"responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/types.AuthResponse"}}},"description":"OK"},"401":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/types.APIError"}}},"description":"Unauthorized"}},"summary":"Authenticate a user","tags":["auth"]}},"/health":{"get":{"description":"get the health of the API","responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/types.Health"}}},"description":"OK"}},"summary":"Get the health of the API","tags":["health"]}},"/install":{"get":{"description":"get the install object","responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/types.Install"}}},"description":"OK"}},"security":[{"bearerauth":[]}],"summary":"Get the install object","tags":["install"]}}},
66
"openapi": "3.1.0",
77
"servers": [
88
{"url":"/api"}

api/docs/swagger.yaml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
components:
22
schemas:
3+
types.APIError:
4+
properties:
5+
errors:
6+
items:
7+
$ref: '#/components/schemas/types.APIError'
8+
type: array
9+
uniqueItems: false
10+
field:
11+
type: string
12+
message:
13+
type: string
14+
status_code:
15+
type: integer
16+
type: object
17+
types.AuthRequest:
18+
properties:
19+
password:
20+
type: string
21+
type: object
22+
types.AuthResponse:
23+
properties:
24+
token:
25+
type: string
26+
type: object
327
types.Health:
428
properties:
529
status:
@@ -73,6 +97,32 @@ info:
7397
version: "0.1"
7498
openapi: 3.1.0
7599
paths:
100+
/auth/login:
101+
post:
102+
description: Authenticate a user
103+
requestBody:
104+
content:
105+
application/json:
106+
schema:
107+
$ref: '#/components/schemas/types.AuthRequest'
108+
description: Auth Request
109+
required: true
110+
responses:
111+
"200":
112+
content:
113+
application/json:
114+
schema:
115+
$ref: '#/components/schemas/types.AuthResponse'
116+
description: OK
117+
"401":
118+
content:
119+
application/json:
120+
schema:
121+
$ref: '#/components/schemas/types.APIError'
122+
description: Unauthorized
123+
summary: Authenticate a user
124+
tags:
125+
- auth
76126
/health:
77127
get:
78128
description: get the health of the API

api/integration/auth_controller_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func TestAuthLoginAndTokenValidation(t *testing.T) {
4949
// Test successful login
5050
t.Run("successful login", func(t *testing.T) {
5151
// Create login request with correct password
52-
loginReq := api.AuthRequest{
52+
loginReq := types.AuthRequest{
5353
Password: password,
5454
}
5555
loginReqJSON, err := json.Marshal(loginReq)
@@ -66,7 +66,7 @@ func TestAuthLoginAndTokenValidation(t *testing.T) {
6666
// Check the login response
6767
assert.Equal(t, http.StatusOK, rec.Code)
6868

69-
var loginResponse api.AuthResponse
69+
var loginResponse types.AuthResponse
7070
err = json.NewDecoder(rec.Body).Decode(&loginResponse)
7171
require.NoError(t, err)
7272

@@ -88,7 +88,7 @@ func TestAuthLoginAndTokenValidation(t *testing.T) {
8888
// Test failed login with incorrect password
8989
t.Run("failed login with incorrect password", func(t *testing.T) {
9090
// Create login request with incorrect password
91-
loginReq := api.AuthRequest{
91+
loginReq := types.AuthRequest{
9292
Password: "wrong-password",
9393
}
9494
loginReqJSON, err := json.Marshal(loginReq)
@@ -168,7 +168,7 @@ func TestAPIClientLogin(t *testing.T) {
168168
c := client.New(server.URL)
169169

170170
// Login with the client
171-
err := c.Login(password)
171+
err := c.Authenticate(password)
172172
require.NoError(t, err, "API client login should succeed with correct password")
173173

174174
// Verify we can make authenticated requests after login
@@ -183,7 +183,7 @@ func TestAPIClientLogin(t *testing.T) {
183183
c := client.New(server.URL)
184184

185185
// Attempt to login with wrong password
186-
err := c.Login("wrong-password")
186+
err := c.Authenticate("wrong-password")
187187
require.Error(t, err, "API client login should fail with wrong password")
188188

189189
// Check that the error is of correct type

api/types/auth.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package types
2+
3+
type AuthRequest struct {
4+
Password string `json:"password"`
5+
}
6+
7+
type AuthResponse struct {
8+
Token string `json:"token"`
9+
}

cmd/installer/cli/install.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,8 +662,8 @@ func markUIInstallComplete(password string, managerPort int) error {
662662
fmt.Sprintf("https://localhost:%d", managerPort),
663663
apiclient.WithHTTPClient(httpClient),
664664
)
665-
if err := apiClient.Login(password); err != nil {
666-
return fmt.Errorf("unable to login: %w", err)
665+
if err := apiClient.Authenticate(password); err != nil {
666+
return fmt.Errorf("unable to authenticate: %w", err)
667667
}
668668

669669
_, err := apiClient.SetInstallStatus(apitypes.InstallationStatus{

0 commit comments

Comments
 (0)