idp.go 4.5 KB

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