identity.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package plugin_entities
  2. import (
  3. "errors"
  4. "regexp"
  5. "strings"
  6. "github.com/go-playground/validator/v10"
  7. "github.com/google/uuid"
  8. "github.com/langgenius/dify-plugin-daemon/pkg/entities/manifest_entities"
  9. "github.com/langgenius/dify-plugin-daemon/pkg/validators"
  10. )
  11. type PluginUniqueIdentifier string
  12. var (
  13. // pluginUniqueIdentifierRegexp is a regular expression to validate the plugin unique identifier.
  14. // It must be in the format of "author/plugin_id:version@checksum".
  15. // all lowercase. the length of plugin_id must be less than 256, and for version part, it must be ^\d{1,4}(\.\d{1,4}){1,3}(-\w{1,16})?$
  16. // for checksum, it must be a 32-character hexadecimal string.
  17. // the author part is optional, if not specified, it will be empty.
  18. pluginUniqueIdentifierRegexp = regexp.MustCompile(
  19. `^(?:([a-z0-9_-]{1,64})\/)?([a-z0-9_-]{1,255}):([0-9]{1,4})(\.[0-9]{1,4}){1,3}(-\w{1,16})?@[a-f0-9]{32,64}$`,
  20. )
  21. )
  22. func NewPluginUniqueIdentifier(identifier string) (PluginUniqueIdentifier, error) {
  23. if !pluginUniqueIdentifierRegexp.MatchString(identifier) {
  24. return "", errors.New("plugin_unique_identifier is not valid")
  25. }
  26. return PluginUniqueIdentifier(identifier), nil
  27. }
  28. func (p PluginUniqueIdentifier) PluginID() string {
  29. // try find :
  30. split := strings.Split(p.String(), ":")
  31. if len(split) == 2 {
  32. return split[0]
  33. }
  34. return p.String()
  35. }
  36. func (p PluginUniqueIdentifier) Version() manifest_entities.Version {
  37. // extract version part from the string
  38. split := strings.Split(p.String(), "@")
  39. if len(split) == 2 {
  40. split = strings.Split(split[0], ":")
  41. if len(split) == 2 {
  42. return manifest_entities.Version(split[1])
  43. }
  44. }
  45. return ""
  46. }
  47. func (p PluginUniqueIdentifier) RemoteLike() bool {
  48. // check if the author is a uuid
  49. _, err := uuid.Parse(p.Author())
  50. return err == nil
  51. }
  52. func (p PluginUniqueIdentifier) Author() string {
  53. // extract author part from the string
  54. split := strings.Split(p.String(), ":")
  55. if len(split) == 2 {
  56. split = strings.Split(split[0], "/")
  57. if len(split) == 2 {
  58. return split[0]
  59. }
  60. }
  61. return ""
  62. }
  63. func (p PluginUniqueIdentifier) Checksum() string {
  64. // extract checksum part from the string
  65. split := strings.Split(p.String(), "@")
  66. if len(split) == 2 {
  67. return split[1]
  68. }
  69. return ""
  70. }
  71. func (p PluginUniqueIdentifier) String() string {
  72. return string(p)
  73. }
  74. func (p PluginUniqueIdentifier) Validate() error {
  75. return validators.GlobalEntitiesValidator.Var(p, "plugin_unique_identifier")
  76. }
  77. func isValidPluginUniqueIdentifier(fl validator.FieldLevel) bool {
  78. return pluginUniqueIdentifierRegexp.MatchString(fl.Field().String())
  79. }
  80. func init() {
  81. validators.GlobalEntitiesValidator.RegisterValidation("plugin_unique_identifier", isValidPluginUniqueIdentifier)
  82. }