Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update UI, add /api/v1/auth/session #16 #25

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,34 @@ paths:
'500':
description: Internal Server Error
parameters: []
/api/v1/auth/session:
get:
summary: Validate current session and return user role
description: Check if the current session is valid and return the user's role.
operationId: validateSession
tags:
- Sessions
responses:
'200':
description: Session validation result
content:
application/json:
schema:
type: object
properties:
valid:
type: boolean
description: Indicates if the current session is valid
role:
type: string
example: "admin"
description: The role of the current user
nullable: true
name:
type: string
example: "r00t"
description: The name of the current user
nullable: true
/api/v1/users:
get:
tags:
Expand Down
3 changes: 2 additions & 1 deletion html/assets/js/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ window.ctf01d_tp_api.auth_signin = function(auth_data) {
});
}

window.ctf01d_tp_api.auth_session = function() {
window.ctf01d_tp_api.auth_session = function (auth_data) {
return $.ajax({
url: '/api/v1/auth/session',
method: 'GET',
contentType: 'application/json',
data: JSON.stringify(auth_data),
});
}
4 changes: 2 additions & 2 deletions html/assets/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,6 @@ $(document).ready(function () {
$('#btn_signin').css({"display": "none"});
$('#btn_signout').css({"display": "inline-block"});
$('#btn_profile').css({"display": "inline-block"});
$('#btn_profile').html(res.username + "(" + res.userrole + ")");
$('#btn_profile').html(res.name + " (" + res.role + ")");
})
})
})
4 changes: 4 additions & 0 deletions internal/app/handlers/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func (siw *ServerInterfaceWrapper) PostApiV1AuthSignout(w http.ResponseWriter, r
siw.handlers.PostApiV1AuthSignout(w, r)
}

func (siw *ServerInterfaceWrapper) ValidateSession(w http.ResponseWriter, r *http.Request) {
siw.handlers.ValidateSession(w, r)
}

func (siw *ServerInterfaceWrapper) ListResults(w http.ResponseWriter, r *http.Request) {
siw.handlers.ListResults(w, r)
}
Expand Down
5 changes: 5 additions & 0 deletions internal/app/handlers/sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,8 @@ func (h *Handlers) PostApiV1AuthSignout(w http.ResponseWriter, r *http.Request)
})
api_helpers.RespondWithJSON(w, http.StatusOK, map[string]string{"data": "User logout successful"})
}

func (h *Handlers) ValidateSession(w http.ResponseWriter, r *http.Request) {
// implement me
api_helpers.RespondWithJSON(w, http.StatusOK, map[string]string{"role": "Admin", "name": "R00t"})
}
27 changes: 27 additions & 0 deletions internal/app/server/server.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading