logout.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. package ac
  11. import (
  12. "net/http"
  13. "devt.de/krotik/common/httputil/user"
  14. "devt.de/krotik/eliasdb/api"
  15. )
  16. /*
  17. EndpointLogout is the logout endpoint URL (rooted). Handles logout/
  18. */
  19. const EndpointLogout = api.APIRoot + "/logout/"
  20. /*
  21. LogoutEndpointInst creates a new endpoint handler.
  22. */
  23. func LogoutEndpointInst() api.RestEndpointHandler {
  24. return &logoutEndpoint{}
  25. }
  26. /*
  27. Handler object for logout operations.
  28. */
  29. type logoutEndpoint struct {
  30. *api.DefaultEndpointHandler
  31. }
  32. /*
  33. HandlePOST terminates the current user session.
  34. */
  35. func (lo *logoutEndpoint) HandlePOST(w http.ResponseWriter, r *http.Request, resources []string) {
  36. // Remove all cookies - we don't check for a valid authentication so also
  37. // old (invalid) cookies are removed
  38. AuthHandler.InvalidateAuthCookie(r)
  39. AuthHandler.RemoveAuthCookie(w)
  40. user.UserSessionManager.RemoveSessionCookie(w)
  41. ct := r.Header.Get("Content-Type")
  42. if ct != "application/json" {
  43. // Do a redirect for non-REST clients
  44. http.Redirect(w, r, "/", http.StatusFound)
  45. }
  46. }
  47. /*
  48. SwaggerDefs is used to describe the endpoint in swagger.
  49. */
  50. func (lo *logoutEndpoint) SwaggerDefs(s map[string]interface{}) {
  51. s["paths"].(map[string]interface{})["/logout"] = map[string]interface{}{
  52. "post": map[string]interface{}{
  53. "summary": "Logout the current user.",
  54. "description": "The logout endpoint terminates the current user session.",
  55. "consumes": []string{
  56. "application/json",
  57. },
  58. "produces": []string{
  59. "application/json",
  60. },
  61. "responses": map[string]interface{}{
  62. "302": map[string]interface{}{
  63. "description": "Redirect to /.",
  64. },
  65. "default": map[string]interface{}{
  66. "description": "Error response",
  67. "schema": map[string]interface{}{
  68. "$ref": "#/definitions/Error",
  69. },
  70. },
  71. },
  72. },
  73. }
  74. // Add generic error object to definition
  75. s["definitions"].(map[string]interface{})["Error"] = map[string]interface{}{
  76. "description": "A human readable error mesage.",
  77. "type": "string",
  78. }
  79. }