A More Secure Internet Connection for Your Home https://fen.gg
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.
 
 
 
 
 
 

112 lines
2.8 KiB

  1. package model
  2. //
  3. // Fengg Security Gateway Server Application
  4. // Copyright (C) 2020 Lukas Matt <support@fen.gg>
  5. //
  6. // This program is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. //
  18. import (
  19. "time"
  20. "fmt"
  21. "github.com/jmoiron/sqlx"
  22. _ "github.com/lib/pq"
  23. )
  24. const (
  25. BearerTokenQueryTmpl = `SELECT * FROM bearer_tokens %s;`
  26. BearerTokenNamedInsertTmpl = `
  27. INSERT INTO bearer_tokens (
  28. created_at, user_id, token, level, note
  29. ) VALUES (
  30. now(), :user_id, :token, :level, :note
  31. ) RETURNING id;`
  32. BearerTokenDeleteTmpl = `DELETE FROM bearer_tokens %s;`
  33. )
  34. type BearerLevel int
  35. const (
  36. BearerRootLevel BearerLevel = 0
  37. )
  38. type BearerToken struct {
  39. ID uint `db:"id" json:"id" binding:"-"`
  40. CreatedAt time.Time `db:"created_at" json:"createdAt" binding:"-"`
  41. UserID uint `db:"user_id" json:"userId" binding:"-"`
  42. Token string `db:"token" json:"token" binding:"-"`
  43. Level BearerLevel `db:"level" json:"level" binding:"-"`
  44. Note *string `db:"note" json:"note,omitempty" binding:"-"`
  45. }
  46. type BearerTokens []BearerToken
  47. func NewBearerToken() *BearerToken {
  48. return &BearerToken{CreatedAt: time.Now()}
  49. }
  50. func (token *BearerToken) DeleteByIDAndUserID(id, uid uint) error {
  51. db, err := sqlx.Connect(dbDriver, dbConnect)
  52. if err != nil {
  53. return err
  54. }
  55. defer db.Close()
  56. _, err = db.Exec(
  57. fmt.Sprintf(BearerTokenDeleteTmpl, "WHERE id = $1 AND user_id = $2"), id, uid)
  58. return err
  59. }
  60. func (tokens *BearerTokens) FindByUserID(uid uint) error {
  61. db, err := sqlx.Connect(dbDriver, dbConnect)
  62. if err != nil {
  63. return err
  64. }
  65. defer db.Close()
  66. return db.Select(tokens, fmt.Sprintf(BearerTokenQueryTmpl, "WHERE user_id = $1"), uid)
  67. }
  68. func (token *BearerToken) FindByToken(fbToken string) error {
  69. db, err := sqlx.Connect(dbDriver, dbConnect)
  70. if err != nil {
  71. return err
  72. }
  73. defer db.Close()
  74. return db.Get(token, fmt.Sprintf(BearerTokenQueryTmpl, "WHERE token = $1"), fbToken)
  75. }
  76. func (token *BearerToken) Create() error {
  77. db, err := sqlx.Connect(dbDriver, dbConnect)
  78. if err != nil {
  79. return err
  80. }
  81. defer db.Close()
  82. rows, err := db.NamedQuery(BearerTokenNamedInsertTmpl, token)
  83. if err != nil {
  84. return err
  85. }
  86. defer rows.Close()
  87. for rows.Next() {
  88. err = rows.Scan(&token.ID)
  89. if err != nil {
  90. return err
  91. }
  92. }
  93. return nil
  94. }