idp.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package mysql
  2. import (
  3. "context"
  4. "encoding/json"
  5. "strings"
  6. "github.com/pkg/errors"
  7. "github.com/usememos/memos/store"
  8. )
  9. func (d *DB) CreateIdentityProvider(ctx context.Context, create *store.IdentityProvider) (*store.IdentityProvider, error) {
  10. var configBytes []byte
  11. if create.Type == store.IdentityProviderOAuth2Type {
  12. bytes, err := json.Marshal(create.Config.OAuth2Config)
  13. if err != nil {
  14. return nil, err
  15. }
  16. configBytes = bytes
  17. } else {
  18. return nil, errors.Errorf("unsupported idp type %s", string(create.Type))
  19. }
  20. placeholders := []string{"?", "?", "?", "?"}
  21. fields := []string{"`name`", "`type`", "`identifier_filter`", "`config`"}
  22. args := []any{create.Name, create.Type, create.IdentifierFilter, string(configBytes)}
  23. stmt := "INSERT INTO `idp` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholders, ", ") + ")"
  24. result, err := d.db.ExecContext(ctx, stmt, args...)
  25. if err != nil {
  26. return nil, err
  27. }
  28. id, err := result.LastInsertId()
  29. if err != nil {
  30. return nil, err
  31. }
  32. create.ID = int32(id)
  33. return create, nil
  34. }
  35. func (d *DB) ListIdentityProviders(ctx context.Context, find *store.FindIdentityProvider) ([]*store.IdentityProvider, error) {
  36. where, args := []string{"1 = 1"}, []any{}
  37. if v := find.ID; v != nil {
  38. where, args = append(where, "`id` = ?"), append(args, *v)
  39. }
  40. rows, err := d.db.QueryContext(ctx, "SELECT `id`, `name`, `type`, `identifier_filter`, `config` FROM `idp` WHERE "+strings.Join(where, " AND ")+" ORDER BY `id` ASC",
  41. args...,
  42. )
  43. if err != nil {
  44. return nil, err
  45. }
  46. defer rows.Close()
  47. var identityProviders []*store.IdentityProvider
  48. for rows.Next() {
  49. var identityProvider store.IdentityProvider
  50. var identityProviderConfig string
  51. if err := rows.Scan(
  52. &identityProvider.ID,
  53. &identityProvider.Name,
  54. &identityProvider.Type,
  55. &identityProvider.IdentifierFilter,
  56. &identityProviderConfig,
  57. ); err != nil {
  58. return nil, err
  59. }
  60. if identityProvider.Type == store.IdentityProviderOAuth2Type {
  61. oauth2Config := &store.IdentityProviderOAuth2Config{}
  62. if err := json.Unmarshal([]byte(identityProviderConfig), oauth2Config); err != nil {
  63. return nil, err
  64. }
  65. identityProvider.Config = &store.IdentityProviderConfig{
  66. OAuth2Config: oauth2Config,
  67. }
  68. } else {
  69. return nil, errors.Errorf("unsupported idp type %s", string(identityProvider.Type))
  70. }
  71. identityProviders = append(identityProviders, &identityProvider)
  72. }
  73. if err := rows.Err(); err != nil {
  74. return nil, err
  75. }
  76. return identityProviders, nil
  77. }
  78. func (d *DB) GetIdentityProvider(ctx context.Context, find *store.FindIdentityProvider) (*store.IdentityProvider, error) {
  79. list, err := d.ListIdentityProviders(ctx, find)
  80. if err != nil {
  81. return nil, err
  82. }
  83. if len(list) == 0 {
  84. return nil, nil
  85. }
  86. identityProvider := list[0]
  87. return identityProvider, nil
  88. }
  89. func (d *DB) UpdateIdentityProvider(ctx context.Context, update *store.UpdateIdentityProvider) (*store.IdentityProvider, error) {
  90. set, args := []string{}, []any{}
  91. if v := update.Name; v != nil {
  92. set, args = append(set, "`name` = ?"), append(args, *v)
  93. }
  94. if v := update.IdentifierFilter; v != nil {
  95. set, args = append(set, "`identifier_filter` = ?"), append(args, *v)
  96. }
  97. if v := update.Config; v != nil {
  98. var configBytes []byte
  99. if update.Type == store.IdentityProviderOAuth2Type {
  100. bytes, err := json.Marshal(update.Config.OAuth2Config)
  101. if err != nil {
  102. return nil, err
  103. }
  104. configBytes = bytes
  105. } else {
  106. return nil, errors.Errorf("unsupported idp type %s", string(update.Type))
  107. }
  108. set, args = append(set, "`config` = ?"), append(args, string(configBytes))
  109. }
  110. args = append(args, update.ID)
  111. stmt := "UPDATE `idp` SET " + strings.Join(set, ", ") + " WHERE `id` = ?"
  112. _, err := d.db.ExecContext(ctx, stmt, args...)
  113. if err != nil {
  114. return nil, err
  115. }
  116. identityProvider, err := d.GetIdentityProvider(ctx, &store.FindIdentityProvider{
  117. ID: &update.ID,
  118. })
  119. if err != nil {
  120. return nil, err
  121. }
  122. if identityProvider == nil {
  123. return nil, errors.Errorf("idp %d not found", update.ID)
  124. }
  125. return identityProvider, nil
  126. }
  127. func (d *DB) DeleteIdentityProvider(ctx context.Context, delete *store.DeleteIdentityProvider) error {
  128. where, args := []string{"`id` = ?"}, []any{delete.ID}
  129. stmt := "DELETE FROM `idp` WHERE " + strings.Join(where, " AND ")
  130. result, err := d.db.ExecContext(ctx, stmt, args...)
  131. if err != nil {
  132. return err
  133. }
  134. if _, err = result.RowsAffected(); err != nil {
  135. return err
  136. }
  137. return nil
  138. }