idp.go 4.7 KB

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