Skip to content

Commit d761112

Browse files
committed
remove unused valid property, add id to session response
1 parent 6edf8d6 commit d761112

File tree

4 files changed

+48
-20
lines changed

4 files changed

+48
-20
lines changed

api/openapi.yaml

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,21 +107,7 @@ paths:
107107
content:
108108
application/json:
109109
schema:
110-
type: object
111-
properties:
112-
valid:
113-
type: boolean
114-
description: Indicates if the current session is valid
115-
role:
116-
type: string
117-
example: "admin"
118-
description: The role of the current user
119-
nullable: true
120-
name:
121-
type: string
122-
example: "r00t"
123-
description: The name of the current user
124-
nullable: true
110+
$ref: '#/components/schemas/SessionResponse'
125111
/api/v1/users:
126112
get:
127113
tags:
@@ -1137,6 +1123,23 @@ components:
11371123
type: array
11381124
items:
11391125
$ref: '#/components/schemas/UniversityResponse'
1126+
SessionResponse:
1127+
type: object
1128+
properties:
1129+
role:
1130+
type: string
1131+
example: "admin"
1132+
description: The role of the current user
1133+
nullable: true
1134+
name:
1135+
type: string
1136+
example: "r00t"
1137+
description: The name of the current user
1138+
nullable: true
1139+
id:
1140+
type: string
1141+
example: "a8a5d938-1bc7-4b69-942d-9c00fc166fb6"
1142+
format: uuid
11401143
links: {}
11411144
callbacks: {}
11421145
security: []

internal/app/handlers/sessions.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"ctf01d/internal/app/repository"
99
"ctf01d/internal/app/server"
1010
api_helpers "ctf01d/internal/app/utils"
11+
"ctf01d/internal/app/view"
1112

1213
openapi_types "github.com/oapi-codegen/runtime/types"
1314
)
@@ -96,9 +97,5 @@ func (h *Handlers) ValidateSession(w http.ResponseWriter, r *http.Request) {
9697
api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Could not find user by user id"})
9798
return
9899
}
99-
var res = make(map[string]string)
100-
res["name"] = user.DisplayName
101-
res["role"] = api_helpers.ConvertUserRequestRoleToString(user.Role)
102-
103-
api_helpers.RespondWithJSON(w, http.StatusOK, res)
100+
api_helpers.RespondWithJSON(w, http.StatusOK, view.NewSessionFromModel(user))
104101
}

internal/app/server/server.gen.go

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

internal/app/view/session.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package view
2+
3+
import (
4+
"ctf01d/internal/app/server"
5+
6+
"ctf01d/internal/app/db"
7+
helpers "ctf01d/internal/app/utils"
8+
)
9+
10+
func NewSessionFromModel(u *db.User) *server.SessionResponse {
11+
userRole := helpers.ConvertUserRequestRoleToString(u.Role)
12+
return &server.SessionResponse{
13+
Id: &u.Id,
14+
Name: &u.Username,
15+
Role: &userRole,
16+
}
17+
}

0 commit comments

Comments
 (0)