You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

context_cookie.go 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package context
  4. import (
  5. "encoding/hex"
  6. "net/http"
  7. "strings"
  8. "code.gitea.io/gitea/modules/setting"
  9. "code.gitea.io/gitea/modules/util"
  10. "code.gitea.io/gitea/modules/web/middleware"
  11. "github.com/minio/sha256-simd"
  12. "golang.org/x/crypto/pbkdf2"
  13. )
  14. const CookieNameFlash = "gitea_flash"
  15. func removeSessionCookieHeader(w http.ResponseWriter) {
  16. cookies := w.Header()["Set-Cookie"]
  17. w.Header().Del("Set-Cookie")
  18. for _, cookie := range cookies {
  19. if strings.HasPrefix(cookie, setting.SessionConfig.CookieName+"=") {
  20. continue
  21. }
  22. w.Header().Add("Set-Cookie", cookie)
  23. }
  24. }
  25. // SetSiteCookie convenience function to set most cookies consistently
  26. // CSRF and a few others are the exception here
  27. func (ctx *Context) SetSiteCookie(name, value string, maxAge int) {
  28. middleware.SetSiteCookie(ctx.Resp, name, value, maxAge)
  29. }
  30. // DeleteSiteCookie convenience function to delete most cookies consistently
  31. // CSRF and a few others are the exception here
  32. func (ctx *Context) DeleteSiteCookie(name string) {
  33. middleware.SetSiteCookie(ctx.Resp, name, "", -1)
  34. }
  35. // GetSiteCookie returns given cookie value from request header.
  36. func (ctx *Context) GetSiteCookie(name string) string {
  37. return middleware.GetSiteCookie(ctx.Req, name)
  38. }
  39. // GetSuperSecureCookie returns given cookie value from request header with secret string.
  40. func (ctx *Context) GetSuperSecureCookie(secret, name string) (string, bool) {
  41. val := ctx.GetSiteCookie(name)
  42. return ctx.CookieDecrypt(secret, val)
  43. }
  44. // CookieDecrypt returns given value from with secret string.
  45. func (ctx *Context) CookieDecrypt(secret, val string) (string, bool) {
  46. if val == "" {
  47. return "", false
  48. }
  49. text, err := hex.DecodeString(val)
  50. if err != nil {
  51. return "", false
  52. }
  53. key := pbkdf2.Key([]byte(secret), []byte(secret), 1000, 16, sha256.New)
  54. text, err = util.AESGCMDecrypt(key, text)
  55. return string(text), err == nil
  56. }
  57. // SetSuperSecureCookie sets given cookie value to response header with secret string.
  58. func (ctx *Context) SetSuperSecureCookie(secret, name, value string, maxAge int) {
  59. text := ctx.CookieEncrypt(secret, value)
  60. ctx.SetSiteCookie(name, text, maxAge)
  61. }
  62. // CookieEncrypt encrypts a given value using the provided secret
  63. func (ctx *Context) CookieEncrypt(secret, value string) string {
  64. key := pbkdf2.Key([]byte(secret), []byte(secret), 1000, 16, sha256.New)
  65. text, err := util.AESGCMEncrypt(key, []byte(value))
  66. if err != nil {
  67. panic("error encrypting cookie: " + err.Error())
  68. }
  69. return hex.EncodeToString(text)
  70. }