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.

session.go 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package auth
  4. import (
  5. "net/http"
  6. user_model "code.gitea.io/gitea/models/user"
  7. "code.gitea.io/gitea/modules/log"
  8. )
  9. // Ensure the struct implements the interface.
  10. var (
  11. _ Method = &Session{}
  12. )
  13. // Session checks if there is a user uid stored in the session and returns the user
  14. // object for that uid.
  15. type Session struct{}
  16. // Name represents the name of auth method
  17. func (s *Session) Name() string {
  18. return "session"
  19. }
  20. // Verify checks if there is a user uid stored in the session and returns the user
  21. // object for that uid.
  22. // Returns nil if there is no user uid stored in the session.
  23. func (s *Session) Verify(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) (*user_model.User, error) {
  24. if sess == nil {
  25. return nil, nil
  26. }
  27. // Get user ID
  28. uid := sess.Get("uid")
  29. if uid == nil {
  30. return nil, nil
  31. }
  32. log.Trace("Session Authorization: Found user[%d]", uid)
  33. id, ok := uid.(int64)
  34. if !ok {
  35. return nil, nil
  36. }
  37. // Get user object
  38. user, err := user_model.GetUserByID(req.Context(), id)
  39. if err != nil {
  40. if !user_model.IsErrUserNotExist(err) {
  41. log.Error("GetUserByID: %v", err)
  42. // Return the err as-is to keep current signed-in session, in case the err is something like context.Canceled. Otherwise non-existing user (nil, nil) will make the caller clear the signed-in session.
  43. return nil, err
  44. }
  45. return nil, nil
  46. }
  47. log.Trace("Session Authorization: Logged in user %-v", user)
  48. return user, nil
  49. }