id.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package wire
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strings"
  6. "go.step.sm/crypto/kms/uri"
  7. )
  8. type ID struct {
  9. Name string `json:"name,omitempty"`
  10. Domain string `json:"domain,omitempty"`
  11. ClientID string `json:"client-id,omitempty"`
  12. Handle string `json:"handle,omitempty"`
  13. }
  14. func ParseID(data []byte) (wireID ID, err error) {
  15. err = json.Unmarshal(data, &wireID)
  16. return
  17. }
  18. type ClientID struct {
  19. Username string
  20. DeviceID string
  21. Domain string
  22. }
  23. // ParseClientID parses a Wire clientID. The ClientID format is as follows:
  24. //
  25. // "wireapp://CzbfFjDOQrenCbDxVmgnFw!594930e9d50bb175@wire.com",
  26. //
  27. // where '!' is used as a separator between the user id & device id.
  28. func ParseClientID(clientID string) (ClientID, error) {
  29. clientIDURI, err := uri.Parse(clientID)
  30. if err != nil {
  31. return ClientID{}, fmt.Errorf("invalid clientID URI %q: %w", clientID, err)
  32. }
  33. fullUsername := clientIDURI.User.Username()
  34. parts := strings.SplitN(fullUsername, "!", 2)
  35. if len(parts) != 2 {
  36. return ClientID{}, fmt.Errorf("invalid clientID %q", fullUsername)
  37. }
  38. return ClientID{
  39. Username: parts[0],
  40. DeviceID: parts[1],
  41. Domain: clientIDURI.Host,
  42. }, nil
  43. }