auth_subject.go 660 B

12345678910111213141516171819202122232425262728
  1. package middleware
  2. import "github.com/gin-gonic/gin"
  3. // AuthSubject is the minimal authenticated identity stored in gin context.
  4. // Decision: {UserID int64, Concurrency int}
  5. type AuthSubject struct {
  6. UserID int64
  7. Concurrency int
  8. }
  9. func GetAuthSubjectFromContext(c *gin.Context) (AuthSubject, bool) {
  10. value, exists := c.Get(string(ContextKeyUser))
  11. if !exists {
  12. return AuthSubject{}, false
  13. }
  14. subject, ok := value.(AuthSubject)
  15. return subject, ok
  16. }
  17. func GetUserRoleFromContext(c *gin.Context) (string, bool) {
  18. value, exists := c.Get(string(ContextKeyUserRole))
  19. if !exists {
  20. return "", false
  21. }
  22. role, ok := value.(string)
  23. return role, ok
  24. }