sess_context.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package hhc_telws
  2. import (
  3. "github.com/pascaldekloe/jwt"
  4. "io"
  5. "net"
  6. )
  7. type TelwsSessionContext struct {
  8. tscc *TelwsSessionContextContainer
  9. }
  10. type TelwsSessionContextContainer struct {
  11. tsc *TelwsSessionContext
  12. jwtClaims *jwt.Claims
  13. conn net.Conn
  14. }
  15. func WrapNewTelwsSessionContext() *TelwsSessionContextContainer {
  16. tscc := &TelwsSessionContextContainer{}
  17. tsc := &TelwsSessionContext{
  18. tscc: tscc,
  19. }
  20. tscc.tsc = tsc
  21. return tscc
  22. }
  23. func (tscc *TelwsSessionContextContainer) GetUserContext() *TelwsSessionContext {
  24. return tscc.tsc
  25. }
  26. func (tscc *TelwsSessionContextContainer) SetJwtClaims(clm *jwt.Claims) {
  27. tscc.jwtClaims = clm
  28. }
  29. func (tscc *TelwsSessionContextContainer) SetConn(conn net.Conn) {
  30. tscc.conn = conn
  31. }
  32. func (tsc *TelwsSessionContext) GetAuthInfo() map[string]interface{} {
  33. return tsc.tscc.jwtClaims.Set
  34. }
  35. func (tsc *TelwsSessionContext) GetUsername() string {
  36. t := tsc.tscc.jwtClaims.Set
  37. if t == nil {
  38. return ""
  39. }
  40. un, ok := t["username"]
  41. if ok {
  42. switch un.(type) {
  43. case string:
  44. return un.(string)
  45. default:
  46. return ""
  47. }
  48. }
  49. return ""
  50. }
  51. func (tsc *TelwsSessionContext) GetWriter() io.Writer {
  52. return tsc.tscc.conn
  53. }
  54. func (tsc *TelwsSessionContext) GetReader() io.Reader {
  55. return tsc.tscc.conn
  56. }
  57. func (tsc *TelwsSessionContext) GetReadWriter() io.ReadWriter {
  58. return tsc.tscc.conn
  59. }
  60. func (tsc *TelwsSessionContext) GetConn() net.Conn {
  61. return tsc.tscc.conn
  62. }