idp.go 5.0 KB

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