Skip to content

Commit

Permalink
Merge pull request #25 from sea-kg/feature/add_api_v1_auth_session
Browse files Browse the repository at this point in the history
update UI, add /api/v1/auth/session #16
  • Loading branch information
IgorPolyakov committed Jun 19, 2024
2 parents a047df1 + 3302de4 commit 64b0a3f
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 3 deletions.
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.

0 comments on commit 64b0a3f

Please sign in to comment.